[Tex/LaTex] Math mode in knitr

knitrmath-mode

How do one use a math-mode in knitr? For example as a label for X-Y axis:

<<fe, echo=FALSE, sanitize=TRUE, dev="tikz">>=
    plot(1:10, main="${}^{57}Fe$", xlab="$\\\\lambda$", ylab="\\\\$I\\\\$", pch=20, family="CM Roman")
@

This produces the $I$ and $lambda$ as a labels.

Best Answer

To use latex (such as math mode) directly in the images, you will need to install the R package tikzDevice, and also the Latex package tikz for Latex (and you will need to explicitly put \usepackage{tikz} in your document).

<<fe, dev="tikz", fig.cap = "${}^{57}Fe spectrum$", echo=FALSE>>=
    plot(1:10, xlab="$\\lambda$", ylab="$I$", pch=20, family="CM Roman")
@