R: Latex in Title Names of Charts, Plots, Histograms, etc

Viewed 581

I am trying to title a histogram in R Markdown with a title involving Latex. However, I can't get it to work. Here is my format in code chunk for R Mark down:

hist(X, freq = FALSE,  breaks = 115, main="Blah Blah $MATH$")
hist(X, freq = FALSE, breaks = 100, main="Blah Blah $MATH$")

Any recommendations ?

2 Answers

In the context of R Markdown documents, I would actually strongly urge you to use figure captions rather than plot titles, like so:

---
title: "Stack Overflow Answer"
author: "duckmayr"
output: pdf_document
---

```{r histogram, fig.cap="Blah Blah $\\mathcal{MATH}$"}
hist(islands, main = "")
```

enter image description here

Update: Multiple histograms

You can use subfigures, which is the normal LaTeX way. Then you'll use the fig.subcap chunk option rather than fig.cap:

title: "Stack Overflow Answer"
author: "duckmayr"
header-includes:
    - \usepackage{subcaption}
output: pdf_document
---

```{r histogram, fig.cap="Histograms", fig.subcap=c("Blah Blah $\\mathcal{MATH}$ (islands)", "Blah Blah $\\mathcal{MATH}$ (linx)"), out.width="45%"}
hist(islands, main = "")
hist(lynx, main = "")
```

enter image description here

You can add (limited) latex with latex2exp

 install.packages("latex2exp")

You could use it like this:

library("latex2exp")
hist(islands, main =TeX('$\\alpha^5 - \\beta$'))

Which would look like: enter image description here

Best is you take a look at the documentation page what kind of Latex formatting is possible with the package.

Related