Question

I have a comparable minor problem, but it would be a great help for me, if anybody could provide me a solution. I would like to have the greek letter lambda as the title of the legend of a plot within a *.Rnw file compiled with knitr. Without the option dev='tikz' in the codejunk, it is no problem to write the greek letter lambda with the option title=expression(lambda). If I add dev='tikz' as an option in the codejunk, this does not work anymore.

So far I only can write the string "lambda" but not the greek letter as the legend title of the graph, if I use dev='tikz' in the code junk. Please download the following *.Rnw file and compile it using knitr for a better understanding of the described problem.

Thank you very much in advance for your help.

broesel

\documentclass{article}

\title{How to write greek letters in R graphs using the tikz environment with \textbf{knitr}?}
\author{broesel}
\date{\today}

\begin{document}

\maketitle

In Figure \ref{fig:barplot} a simple graph is plotted using \textbf{knitr}. 
In order to print the legend of the graph \texttt{legend()} was used. 
The option \texttt{title=expression(lambda)} was used in order to write the greek letter $\lambda$ as the title of the legend.

<<barplot, fig.cap="A simple barplot having $\\lambda$ as legend title">>=
a <- c(10, 9, 6)
b <- c(8, 7, 4)
c <- rbind(a,b)
barplot(c, beside=T, ylim=c(0,12), col=c("grey", "black"))
legend("topright", c("yes", "no"), 
   fill=c("grey", "black"), title=expression(lambda))
@

In Figure \ref{fig:plot_tikz} the option \texttt{dev='tikz'} was used in the code chunk, in order to use the same font in the graphs as in the rest of the document, which you can see especially if you compare the y-axes and the legends of Figures \ref{fig:barplot} and \ref{fig:plot_tikz}. 
Unfortunately when I use the option \texttt{title=expression(lambda)} in Figure \ref{fig:plot_tikz}, to show the greek letter $\lambda$ as the legend title, I get an error message. 
So far I was only able to write "lambda" as the legend title. 

So here is my question: How can I write the greek letter $\lambda$ and not "lambda" as the legend title of Figure \ref{fig:plot_tikz}?

<<plot_tikz, fig.cap="A barplot with tikz having ''lambda'' as legend title", dev='tikz'>>=
a <- c(10, 9, 6)
b <- c(8, 7, 4)
c <- rbind(a,b)
barplot(c, beside=T, ylim=c(0,12), col=c("grey", "black"))
legend("topright", c("yes", "no"), 
   fill=c("grey", "black"), title="lambda")
@

\end{document}
Was it helpful?

Solution

The correct answer is to write "$\\lambda$".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top