How to add a LaTeX symbol to fig.cap in R markdown?

Viewed 3539

Is there a way to add LaTeX symbols, like infinity \infty to figure captions within R markdown?

I can get LaTeX text formatting to work, but I can't figure out a way to use symbols.

Here is a working example using LaTeX text formatting:

---
output: 
  pdf_document:
    fig_caption: yes
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(
  echo=FALSE,
  message=FALSE,
  warning=FALSE,
  fig.align = 'center'
  )
```

## Testing LaTeX syntax within captions

```{r pressure, fig.cap="\\label{testPlot}This caption has LaTeX with 
{\\small small text} and some {\\footnotesize footnote text and 
\\textbf{bold footnote}} and \\textit{italic}"}
plot(pressure)
```

I have tried every combination I can think of an none have worked. For instance, double escape \\infty, brackets {\infty}, double escape within brackets {\\infty}, using the inline LaTeX $\infty$, inline with escape, etc. etc.

Any advice will be greatly appreciated.

1 Answers

$\\infty$ works. You need to escape the \ in \infty and IIRC this is a math mode symbol, so you need to invoke that too.

```{r pressure, fig.cap="\\label{testPlot}This caption has LaTeX with {\\small small text} and some {\\footnotesize footnote text and \\textbf{bold footnote}} and \\textit{italic} and $\\infty$"}
plot(1:10)
```
Related