I asked this question on Stack Overflow 16 days ago but got no help so I am asking here!
I have a knitr Rnw file (LaTeX with embedded R) which imports the siunitx package. I declare units with \DeclareSIUnit and I would like to reference those units in axis labels in my ggplot() figures. I have tried the master branch of latex2enc but it does not do what I need. Help?
Minimum working example:
\documentclass{article}
\usepackage{siunitx}
\DeclareSIUnit{\dunit}{\SIUnitSymbolDegree Du}
\begin{document}
<<setup, include=TRUE>>=
library(ggplot2)
@
This first plot with a changed Y label should work.
<<new-y-label, echo=TRUE>>=
ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_line(aes(linetype=Species)) + ylab("Petal length")
@
I can change the label to include basic \LaTeX\ commands using the latex2exp package, which is not formally released for R version 3.4.4.
<<latex-y-label, echo=TRUE>>=
# devtools::install_github('stefano-meschiari/latex2exp')
library(latex2exp)
ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_line(aes(linetype=Species)) + ylab(TeX("$\\alpha$"))
@
But how do I change the Y label to reference \si{\dunit} ?
<<does-not-work, echo=TRUE>>=
ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_line(aes(linetype=Species)) + ylab(TeX("$\\si{\\dunit}$"))
@
This does not work. Help!
\end{document}