Nov 18, 2022

# Solution 1 :

You have to link the anchor to a heading, as far as I know. One workaround to link to a plot would be to add an empty heading below it.

Create an anchor next to an empty heading below the plot, like this:

`# {#YourAnchorNextToTheHeading}`.

Wrap the word/sentence you want the link into in square brackets `[]`, followed by your anchor wrapped around round brackets.

Here is an example:

``````# page 1
R code that was used to perform the regression analysis can be found by clicking [HERE](#page2).

pagebreak

# page 2

```{r echo = FALSE}
plot(cars)

```

# {#page2}
``````

I found this on the the R Cookbook tutorial::

Create a R function to write raw HTML or LaTeX code:

`````````{r echo=FALSE, include=FALSE}
colorize <- function(x, color) {
if (knitr::is_latex_output()) {
sprintf("\textcolor{%s}{%s}", color, x)
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>", color,
x)
} else x
}

```
``````

Then add it to the text (make sure to wrap `r colorize("HERE", "blue")` with back ticks (`))

# page 1

R code that was used to perform the regression analysis can be found by clicking `r colorize("HERE", "blue")`.

# Problem :

Let’s say I have an R markdown pdf document that have 2 pages.

``````# page 1
R code that was used to perform the regression analysis can be found by clicking HERE.
``````

What I want is when I click on “HERE” it refers/takes me to the code in the page two. Suppose the page 2 contains the following code.

``````# page 2
model = lm(y~x, data = data)
``````

Any ideas?

### Comment posted by bird

Thanks a lot! It does work! ðŸ™‚ Just a small note: the word “HERE” does not change the way it looks and a reader of my text would not really know if they can click on it if it was another word (in my specific case, I want the word Appendix to take the reader to the appendix in the last page). I would like the word to look differently than others so the reader can see that they have an option to click and go to the appendix rather than scrolling down manually.

### Comment posted by here

You’re welcome :). I found a way to color the text