[Tex/LaTex] Special characters and math-mode in tikz graphics using knitr

knitrrtikz-pgf

This question closely relates to this one: Using '$' in a tikz plot generated by knitr

I am able to get tikz graphics using knitr working with math mode as in this post:
Math mode in knitr

What I can't work out how to do is get both math mode working and special characters like '%' and '$' to show up as text.

Any help would be very much appreciated.

Best Answer

I have worked it out. In the character objects in R, the escape character also needs to be escaped.

For example, using sanitize=FALSE (or omitted) in the knitr chunk, and using "\\$" works:

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