Julian
Julian

Reputation: 9330

Link part of equation inside RMarkdown

I would like to add a link (to a section) to a part of an equation in RMarkdown. I hope it becomes more clear in the following MWE:

MWE

---
title: "Link Test"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Equation

$$
a^2 + b^2 = c^2 
$$


## Description {#description}

Here we talk a lot about $c^2$. I can use `r kableExtra::text_spec("$c^2$", link = "#description")` to reference. 
Now I would like to link on part of an equation, i.e. something like 

$$
a^2 + b^2 = `r kableExtra::text_spec("$c^2$", link = "#description")` 
$$
such that only $c^2$ is linked to the section inside the equation. 

The attempt obviously does not work, maybe someone has an idea :)

Upvotes: 2

Views: 200

Answers (1)

Carlos Luis Rivera
Carlos Luis Rivera

Reputation: 3693

Inside the math block, LaTeX syntax, \href{#description}{c^2}, should be used to refer to a certain section with a custom text. As you can see from the second argument of \href, you must not enclose c^2 in $ when you put \href{#description}{c^2} into a math block, as \href{#description}{c^2} has already been placed in a math environment.

By the way, using Pandoc syntax, [$c^2$](#description), is safer to refer to a certain section in a (non-math) paragraph. This syntax should be rendered correctly in all output formats.

HTML output

enter image description here

PDF output

enter image description here

MWE

---
title: "Link Test"
output:
  html_document: default
  pdf_document: default
# To knit all formats simultaneously
knit: (function(inputFile, encoding) {
  rmarkdown::render(inputFile, encoding = encoding, output_format = "all") })
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Equation

$$
a^2 + b^2 = c^2 
$$


## Description {#description}

Here we talk a lot about $c^2$.
I can use `r kableExtra::text_spec("$c^2$", link = "#description")` to reference,
**but this is rendered properly only when knitting an html document**.
**Using Pandoc syntax, `[$c^2$](#description)`, is safer to refer to [$c^2$](#description)**. 
Now I would like to link on part of an equation, i.e. something like 

$$
a^2 + b^2 = \href{#description}{c^2}
$$

such that only $c^2$ is linked to the section inside the equation.
Inside the math block, LaTeX syntax, `\href{#description}{c^2}`, should be used.

Upvotes: 1

Related Questions