# TeX("$\lambda$") doesn't work in R

Im using the latex2exp package to make some plots in R with latex expressions in the labels. TeX("$\lambda$") on the xlabel doesn't print what it needs to. The documentation said to use TeX(r'($\lambda$)') which doesnt work, and TeX("$\\lambda$") doesnt work either. For example, plot(1:5, 1:5, xlab = Tex(r'($\lambda$)')) will not have the lambda character on the xlabel. How should I fix this?

With knitr and the tikz device there are no need of latex2exp and the TeX() function, only escape the backslash.

\documentclass{article}
\begin{document}
<<foo,echo=F,dev='tikz',fig.width=3, fig.height=3, fig.align='center'>>=
plot(1:5, 1:5, xlab = '$\\lambda$ with \\LaTeX')
@
\end{document}


• I just installed those packages and tried it. It prints: $\lambda$ on the xlabel Apr 4, 2021 at 1:53
• @RobertFerydouni Have you tested just the above R Sweave file (save it with .Rnw extension) "as is" or only the R chunk? I have included the expected output in the PDF.
– Fran
Apr 4, 2021 at 9:06
• Yes. It seems I have the same problem as my original comment. I do not have a latex compiler on my machine, could this be the problem? I try to import that library and it says "No Latex compiler could be found". So maybe it isnt noticing the $symbols because of that Apr 4, 2021 at 17:47 • Of course, you need a latex compiler. Using R one way is installing tinytex – Fran Apr 5, 2021 at 2:01 • I just installed tinytex, and I tried running the the R chunk only, as well as running the the file as is, and it still prints the characters '$\lambda$with \LaTeX' on the xlabel. What else could this be attributed to Apr 5, 2021 at 2:58 The documentation of latex2exp mentions the following: Before R 4.0, it is necessary to escape the backslash within the string literal: therefore, one writes '$\\gamma$' rather than '$\gamma$' (the latter will cause an error). After R 4.0, it is recommended to use the new raw string literal syntax (see ?Quotes). The syntax looks like r"(...)", where ... can contain any character sequence, including \: You can find your R version when you start R, the version number is printed in the terminal window. Note that the R command is TeX() (so upper case T, lower case e, upper case X, which is the standard capitalization of TeX, and not Tex as written in the final snippet in the question - but I guess that is a typo. The following works in R 3.6: library(latex2exp) plot(1:5, 1:5, xlab = TeX("$\\lambda$"))  Result: For R 4.0 and above you can use the new syntax. Note that you have to explicitly include the delimiter within the string. In the example above the delimiters are '( and )'. The following works when tested in R 4.0.3: library(latex2exp) plot(1:5, 1:5, xlab = TeX(r'($\lambda$)'))  This does not work if you have the closing sequence )' somewhere inside of your string. In that case you can extend the delimiter with dashes, for example: plot(1:5, 1:5, xlab = TeX(r'---($\lambda\cdot(a+b)'$)---')) # more readable: plot(1:5, 1:5, xlab = TeX( r'---($\lambda\cdot(a+b)'$)---' ))  • I ave R version 4.0.4. I ran it with what you posted for R 4 and above, and I get a Error: malformed raw string literal Apr 3, 2021 at 21:20 • @RobertFerydouni strange. I'll try later if I can access a newer version of R to try out the syntax. Apr 3, 2021 at 21:38 • @RobertFerydouni I didn't put the correct delimiters in my 4.0+ example, now corrected. Apr 5, 2021 at 13:28 • This doesnt seem to work. It prints: paste("", "", lambda, , , , "") on the xlabel Apr 5, 2021 at 22:02 • @RobertFerydouni Maybe some of your packages need to be updated? I tried on rdrr.io/snippets with the code library(latex2exp) print(R.version) plot(1:5, 1:5, xlab = TeX(r'($\lambda\$)')) which gives the expected plot (that website uses R 4.0.3). Or maybe there is an issue in how you store and run the code - do you type it directly in Rstudio, or do you run it from the command line/terminal, or through a file? Maybe there are some issues with interpreting/escaping special characters there, especially if it goes through the terminal. Apr 6, 2021 at 7:47