我有一个类似的小问题,但如果有人能为我提供解决方案,那对我来说将是一个很大的帮助。我希望将希腊字母lambda作为使用knitr编译的* .Rnw文件中的绘图图例的标题。没有选项dev =' tikz'在codejunk中,使用选项title = expression(lambda)编写希腊字母lambda是没有问题的。如果我添加dev =' tikz'作为codejunk中的一个选项,这不再起作用了。
到目前为止,我只能写字符串" lambda"但如果我使用dev =' tikz'而不是希腊字母作为图表的图例标题。在代码垃圾中。 请下载以下* .Rnw文件并使用knitr进行编译,以便更好地理解所描述的问题。
非常感谢您的帮助。
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}
答案 0 :(得分:2)
正确答案是写"$\\lambda$"
。