Internal links in rmarkdown don't work

You're not setting the anchors correctly.

Try the following:

# 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}

There are some tricky rules for internal links in Rmarkdown PDF output, not well documented on cheat sheets, etc.

Rules:

  • Only one # for anchor, even if you are using a header with multiple #.

    Example: ###header becomes #header when linking.

  • No spaces between # and anchor text.

    Example: #header, not # header.

  • Multi-word anchors should be separated by dashes.

    Example: #this is a header needs to become #this-is-a-header in the link.

  • Anchor links need to be lowercase, even if the header you are linking to has capitalization.

    Example: #Section becomes #section in the link.


@rPirate has a good list, but it was missing a case that was befuddling me. I had a section header like ### 1.1.1 My Section Title that needs to be #my-section-title in the link.

After some searching, I found an official list of transformation rules in the 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