rmarkdown 中的内部链接不起作用
Internal links in rmarkdown don't work
我使用 rmarkdown 渲染 pdf 文档。现在我想在文本中添加内部 links。
在 rmarkdown 的帮助页面中,它说内部 link 定义为:
See the [Introduction](#introduction).
当我使用例如下一个代码应该有两个内部 links:link1 和 link2。两者都没有 link。有什么明显的我做错了吗?非常感谢!
---
title: "Test"
author: "test test"
output:
pdf_document:
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
html_document:
css: tables.css
number_sections: yes
theme: cerulean
toc: yes
toc_depth: 3
subtitle: test test test
mainfont: Calibri Light
fontsize: 12pt
header-includes:
- \usepackage[dutch]{babel}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyfoot[LE,RO]{this is a fancy foot}
- \usepackage{dcolumn}
- \usepackage{here}
- \usepackage{longtable}
- \usepackage{caption}
- \captionsetup{skip=2pt,labelsep=space,justification=justified,singlelinecheck=off}
---
# start
```{r results="asis",tidy=FALSE,eval=TRUE,echo=FALSE,message=FALSE, error=FALSE, warning=FALSE, comment = NA}
cat("click here: [link1](#test 1)")
```
click here: [link2](#test 1)
\pagebreak
#test 3
\pagebreak
#test 2
\pagebreak
#test 1
您没有正确设置锚点。
尝试以下操作:
# start
```{r results="asis",tidy=FALSE,eval=TRUE}
cat("click here: [link1](#test1)")
```
click here: [link2](#test1)
\pagebreak
# test 3 {#test3}
\pagebreak
#test 2 {#test2}
\pagebreak
#test 1 {#test1}
Josh Paulson 在 Rstudio 网站上有一篇文章进行了详细介绍。这是他的解决方案
Slide 1
====================================
id: slide1
Slide 2
====================================
[Go to slide 1](#/slide1)
这对我有用。 A Handcart And Mohair 的代码不适用于 RHTML
Rmarkdown PDF 输出中的内部 links 有一些棘手的规则,在备忘单等方面没有很好的记录。
规则:
只有一个 #
作为锚点,即使您使用一个 header 和多个 #
.
示例:当 linking.
时,###header
变为 #header
# 和锚文本之间没有空格。
示例:#header
,而不是 # header
。
Multi-word 锚点应该用破折号分隔。
示例:#this is a header
需要在 link 中变为 #this-is-a-header
。
锚点 links 需要小写,即使 header 您link要使用大写。
示例:#Section
在 link 中变为 #section
。
@rPirate 有一个很好的列表,但它缺少一个让我感到困惑的案例。我有一个像 ### 1.1.1 My Section Title
这样的部分 header 需要在 link.
中是 #my-section-title
经过一番搜索,我在 Pandoc manual:
中找到了官方的转换规则列表
The default algorithm used to derive the identifier from the header text is:
- Remove all formatting, links, etc.
- Remove all footnotes.
- Remove all non-alphanumeric characters, except underscores, hyphens, and periods.
- Replace all spaces and newlines with hyphens.
- Convert all alphabetic characters to lowercase.
- Remove everything up to the first letter (identifiers may not begin with a number or punctuation mark).
- If nothing is left after this, use the identifier section.
Thus, for example,
Header | Identifier
--------------------------------------------------------
Header identifiers in HTML | header-identifiers-in-html
Maître d'hôtel | maître-dhôtel
*Dogs*?--in *my* house? | dogs--in-my-house
[HTML], [S5], or [RTF]? | html-s5-or-rtf
3. Applications | applications
33 | section
这是在 RMarkdown PDF 文档中进行内部 links/references 的简单方法:
A) 创建一个部分 header:
## Homework Assignments{#hwks}
B) 创建部分后 header。我可以在我的文本中这样引用它:
If you are having a hard time with your [homework](#hwks), we have some tutors who can help you.
这会创建一个 link 回到标题为“家庭作业”的 header 部分。
注意我设置 link 的方式中的一些事情:
“作业”==> 我用来称呼 header 的单词(或短语)在方括号中
[homework] 后面紧跟着我在上面创建的标识符“#hwks”,但标识符现在位于括号中,如:(#hwks)
这对我来说总是有效的!
(这是我第一次 post 来这里;我希望它有意义。:) )
我试图将多个不同帖子中的大量信息编译成一个 single-answer,工作 .Rmd 文件示例如下:
---
title: "internal link testing"
output: html_document
---
Create some headings that we want to link to later:
# Heading 1
## Heading 2
### Heading 3
Improperly created headings will not work because there must be a space between the hash (#) and the heading text:
#Heading1a
##Heading2a
###Heading3a
The following links work as expected regardless of capitalization:
[Heading 1] [heading 1]
[Heading 2] [heading 2]
[Heading 3] [heading 3]
But, do not replace space(s) with dash(es)... these links will not work:
[heading-1] [Heading-1]
[heading-2] [Heading-2]
[heading--3] [Heading--3]
*HOWEVER... *
If using an explicit link, you MUST:
- replace spaces with dash
- use lower case
- and there must be no leading space between the hash and the text:
[explicit heading 1 link](#heading-1)
Leaving in space(s) creates a broken link leading to something that doesn't exist:
[broken heading 1 link](#heading 1)
[broken heading 1 link](# heading-1)
You can create links within a code chunk as well by `cat`ting or pasting the text and setting results='asis':
```{r testchunk, results='asis', message=FALSE, warning=FALSE}
# These all work:
cat("[Heading 1] [heading 1] [explicit heading 1 link](#heading-1) ")
paste("[Heading 1] [heading 1] [explicit heading 1 link](#heading-1) ")
cat('<a href="#heading-1">heading 1 href</a>')
library(DT)
library(dplyr)
for(car in unique(rownames(mtcars))) {
# You can programmatically create section headers within a for loop, but make sure to include some extra newlines:
cat("\n")
cat(paste0("## ", car))
cat("\n")
# within a kable, make sure to replace spaces, use lowercase, and set escape=FALSE
# and remember to print the table within the loop
mtcars %>%
tibble::rownames_to_column(var="vname") %>%
mutate(link = paste0('<a href="#', tolower(gsub(" ", "-", car)), '">', car, '</a>')) %>%
filter(vname == car) %>%
knitr::kable(escape=FALSE) %>%
print()
}
# if using DT::datatable, make sure to specify escape=F to allow html within the table:
mtcars %>%
tibble::rownames_to_column(var="vname") %>%
mutate(link = paste0('<a href="#', tolower(gsub(" ", "-", vname)), '">', vname, '</a>')) %>%
datatable(escape=FALSE)
我使用 rmarkdown 渲染 pdf 文档。现在我想在文本中添加内部 links。
在 rmarkdown 的帮助页面中,它说内部 link 定义为:
See the [Introduction](#introduction).
当我使用例如下一个代码应该有两个内部 links:link1 和 link2。两者都没有 link。有什么明显的我做错了吗?非常感谢!
---
title: "Test"
author: "test test"
output:
pdf_document:
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
html_document:
css: tables.css
number_sections: yes
theme: cerulean
toc: yes
toc_depth: 3
subtitle: test test test
mainfont: Calibri Light
fontsize: 12pt
header-includes:
- \usepackage[dutch]{babel}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyfoot[LE,RO]{this is a fancy foot}
- \usepackage{dcolumn}
- \usepackage{here}
- \usepackage{longtable}
- \usepackage{caption}
- \captionsetup{skip=2pt,labelsep=space,justification=justified,singlelinecheck=off}
---
# start
```{r results="asis",tidy=FALSE,eval=TRUE,echo=FALSE,message=FALSE, error=FALSE, warning=FALSE, comment = NA}
cat("click here: [link1](#test 1)")
```
click here: [link2](#test 1)
\pagebreak
#test 3
\pagebreak
#test 2
\pagebreak
#test 1
您没有正确设置锚点。
尝试以下操作:
# start
```{r results="asis",tidy=FALSE,eval=TRUE}
cat("click here: [link1](#test1)")
```
click here: [link2](#test1)
\pagebreak
# test 3 {#test3}
\pagebreak
#test 2 {#test2}
\pagebreak
#test 1 {#test1}
Josh Paulson 在 Rstudio 网站上有一篇文章进行了详细介绍。这是他的解决方案
Slide 1
====================================
id: slide1
Slide 2
====================================
[Go to slide 1](#/slide1)
这对我有用。 A Handcart And Mohair 的代码不适用于 RHTML
Rmarkdown PDF 输出中的内部 links 有一些棘手的规则,在备忘单等方面没有很好的记录。
规则:
只有一个
#
作为锚点,即使您使用一个 header 和多个#
.示例:当 linking.
时,###header
变为#header
# 和锚文本之间没有空格。
示例:
#header
,而不是# header
。Multi-word 锚点应该用破折号分隔。
示例:
#this is a header
需要在 link 中变为#this-is-a-header
。锚点 links 需要小写,即使 header 您link要使用大写。
示例:
#Section
在 link 中变为#section
。
@rPirate 有一个很好的列表,但它缺少一个让我感到困惑的案例。我有一个像 ### 1.1.1 My Section Title
这样的部分 header 需要在 link.
#my-section-title
经过一番搜索,我在 Pandoc manual:
中找到了官方的转换规则列表The default algorithm used to derive the identifier from the header text is:
- Remove all formatting, links, etc.
- Remove all footnotes.
- Remove all non-alphanumeric characters, except underscores, hyphens, and periods.
- Replace all spaces and newlines with hyphens.
- Convert all alphabetic characters to lowercase.
- Remove everything up to the first letter (identifiers may not begin with a number or punctuation mark).
- If nothing is left after this, use the identifier section.
Thus, for example,
Header | Identifier -------------------------------------------------------- Header identifiers in HTML | header-identifiers-in-html Maître d'hôtel | maître-dhôtel *Dogs*?--in *my* house? | dogs--in-my-house [HTML], [S5], or [RTF]? | html-s5-or-rtf 3. Applications | applications 33 | section
这是在 RMarkdown PDF 文档中进行内部 links/references 的简单方法:
A) 创建一个部分 header:
## Homework Assignments{#hwks}
B) 创建部分后 header。我可以在我的文本中这样引用它:
If you are having a hard time with your [homework](#hwks), we have some tutors who can help you.
这会创建一个 link 回到标题为“家庭作业”的 header 部分。
注意我设置 link 的方式中的一些事情:
“作业”==> 我用来称呼 header 的单词(或短语)在方括号中
[homework] 后面紧跟着我在上面创建的标识符“#hwks”,但标识符现在位于括号中,如:(#hwks)
这对我来说总是有效的!
(这是我第一次 post 来这里;我希望它有意义。:) )
我试图将多个不同帖子中的大量信息编译成一个 single-answer,工作 .Rmd 文件示例如下:
---
title: "internal link testing"
output: html_document
---
Create some headings that we want to link to later:
# Heading 1
## Heading 2
### Heading 3
Improperly created headings will not work because there must be a space between the hash (#) and the heading text:
#Heading1a
##Heading2a
###Heading3a
The following links work as expected regardless of capitalization:
[Heading 1] [heading 1]
[Heading 2] [heading 2]
[Heading 3] [heading 3]
But, do not replace space(s) with dash(es)... these links will not work:
[heading-1] [Heading-1]
[heading-2] [Heading-2]
[heading--3] [Heading--3]
*HOWEVER... *
If using an explicit link, you MUST:
- replace spaces with dash
- use lower case
- and there must be no leading space between the hash and the text:
[explicit heading 1 link](#heading-1)
Leaving in space(s) creates a broken link leading to something that doesn't exist:
[broken heading 1 link](#heading 1)
[broken heading 1 link](# heading-1)
You can create links within a code chunk as well by `cat`ting or pasting the text and setting results='asis':
```{r testchunk, results='asis', message=FALSE, warning=FALSE}
# These all work:
cat("[Heading 1] [heading 1] [explicit heading 1 link](#heading-1) ")
paste("[Heading 1] [heading 1] [explicit heading 1 link](#heading-1) ")
cat('<a href="#heading-1">heading 1 href</a>')
library(DT)
library(dplyr)
for(car in unique(rownames(mtcars))) {
# You can programmatically create section headers within a for loop, but make sure to include some extra newlines:
cat("\n")
cat(paste0("## ", car))
cat("\n")
# within a kable, make sure to replace spaces, use lowercase, and set escape=FALSE
# and remember to print the table within the loop
mtcars %>%
tibble::rownames_to_column(var="vname") %>%
mutate(link = paste0('<a href="#', tolower(gsub(" ", "-", car)), '">', car, '</a>')) %>%
filter(vname == car) %>%
knitr::kable(escape=FALSE) %>%
print()
}
# if using DT::datatable, make sure to specify escape=F to allow html within the table:
mtcars %>%
tibble::rownames_to_column(var="vname") %>%
mutate(link = paste0('<a href="#', tolower(gsub(" ", "-", vname)), '">', vname, '</a>')) %>%
datatable(escape=FALSE)