2016-07-28 51 views
0

我想在我的.Rnw beamer演示文稿中使用自定义字体。再现误差的最小工作的例子是:用sweave和beamer自定义ggplot字体

\documentclass{beamer} 
\begin{document} 
\begin{frame}[plain] 
<<echo=FALSE>>= 
library(ggplot2) 
library(extrafont) 
@ 
\begin{figure} 
\centering 
<<label = test, fig=TRUE, include=FALSE, echo=FALSE, message=FALSE>>= 
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + 
    ggtitle("Fuel Efficiency of 32 Cars") + 
    xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") + 
    theme_bw() + 
    theme(text=element_text(family="Garamond", size=14)) 
@ 
\includegraphics[width=\textwidth]{test} 
\end{figure} 
\end{frame} 
\end{document} 

和错误消息是这样的:

Writing to file test.tex 
Processing code chunks with options ... 
1 : keep.source term verbatim (test.Rnw:6) 
Registering fonts with R 
2 : keep.source term verbatim pdf (label = test, test.Rnw:12) 
Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, : 
    invalid font type 
Calls: <Anonymous> ... drawDetails -> drawDetails.text -> grid.Call.graphics 
In addition: There were 50 or more warnings (use warnings() to see the first 50) 
Execution halted 

但是,当我在控制台中使用ggplot命令,一切都看起来不错。

任何帮助将不胜感激。

+0

的唯一方法我可以重现你的错误,使用.Rnw文件,是,如果我尝试使用不是已经安装的字体。在控制台中,如果我尝试使用卸载的字体,R将恢复为默认字体并发出警告。 –

+0

这很奇怪。如果我查看'fonts()',我清楚地看到字体已经安装好了,我想可以通过'extrafont'访问。 – Pascal

回答

0

好吧,我的问题确实没有安装字体。但是因为我只使用了Garamond作为例子,并且真的想要使用OTF字体Fira Sans,所以我使用了showtext包。

下面是工作代码:

\documentclass{beamer} 
\begin{document} 
\begin{frame}[plain] 
<<test, fig=TRUE, echo=FALSE, width=6, height=4>>= 
library(ggplot2) 
library(showtext) 
font.add.google("Fira Sans", "fira") 
showtext.auto() 
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + 
    ggtitle("Fuel Efficiency of 32 Cars") + 
    xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") + 
    theme_bw() + 
    theme(text = element_text(family = "fira")) 
@ 
\end{frame} 
\end{document}