2

我有一个类似的小问题,但如果有人可以为我提供解决方案,这对我来说将是一个很大的帮助。我想在用 knitr 编译的 *.Rnw 文件中将希腊字母 lambda 作为图例的标题。在 codejunk 中没有选项 dev='tikz',用选项 title=expression(lambda) 写希腊字母 lambda 是没有问题的。如果我在 codejunk 中添加 dev='tikz' 作为选项,这将不再起作用。

到目前为止,如果我在代码垃圾中使用 dev='tikz',我只能写字符串“lambda”而不是希腊字母作为图的图例标题。请下载以下 *.Rnw 文件并使用 knitr 进行编译,以便更好地理解所描述的问题。

非常感谢您的帮助。

布罗塞尔

\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}
4

1 回答 1

2

正确答案是写"$\\lambda$"

于 2014-05-14T20:27:38.890 回答