2013-10-18 73 views
0

我有一個數百萬的數據點的情節,所以我期待先製作一個png然後包含它。但是,當我編譯時,我遇到了不能包含png的問題。如何創建然後包括在Sweave中的png圖

\documentclass{article} 
\usepackage{graphicx} 

\begin{document} 

\begin{figure}[htb] 
<<fig=TRUE,echo=FALSE>>= 
png('test.png') 
plot(rnorm(100)) 
dev.off() 
@ 
\includegraphics{test} 
\end{figure} 

\end{document} 

調用上述MWE後,我便會去我R控制檯就像往常一樣撥打:

Sweave("report.Rnw") 
texi2pdf("report.tex") 

其中一期工程,除非我在Rnw文件上面的代碼中所有的時間。錯誤信息:

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, : 
    Running 'texi2dvi' on 'report.tex' failed. 
LaTeX errors: 
!pdfTeX error: pdflatex (file ./report-019.pdf): PDF inclusion: requir 
ed page does not exist <0> 
==> Fatal error occurred, no output PDF file produced! 
> Sweave("report.Rnw") ; texi2pdf("report.tex") 
Writing to file report.tex 

回答

1

首先,創建一個函數來輸出你的.png(或.pdf,或其他)文件。我想 爲此創建一個單獨的文件夾(下面的images_plot)。

for (i in x) { 
    # set a real filename here, instead of 'i' 
    pdf(paste('images_plot/', i, '.pdf', sep = ''), width = 10, height = 5) 
    plot(x) 
    dev.off() 
} 

然後用特克斯表現出來:

<<echo = FALSE, results=tex>>= 
for (i in x) 
{ 
    cat('\\begin{figure}[h]\n') 
    file = paste('images_plot/', i, '.pdf', sep = '') 
    cat('\\includegraphics{', file, '}\n', sep = '') 
    cat('\\end{figure}\n') 
} 
@ 

這就是我如何做到這一點,希望它能幫助!

1

這是錯誤的,因爲你的塊有fig=TRUE,但不會在那裏產生任何Sweave圖。

0

如果您可以選擇使用knitr而不是Sweave,那麼您只需在塊選項中指定dev="png"即可。它甚至會建立數字環境,爲你,如果你指定fig.cap參數:

\documentclass{article} 

\begin{document} 

<<test,dev="png",fig.cap="My figure",fig.pos="htb">>= 
plot(rnorm(100)) 
@ 

\end{document} 
-1

對不起,這麼晚了,我會做的僅僅是使用這個回聲選項:

\documentclass[11pt]{article} 
\usepackage{graphicx, verbatim} 

\begin{document} 

<<fig=TRUE,echo=FALSE>>= 
boxplot(rnorm(100)) 
@ 

\end{document} 
+0

但OP有相同的參數。你的回答並沒有闡明它的作用,也沒有充分解釋任何人尋求幫助(未經證實)的變化。 –

+0

是啊,沒有澄清任何事情,只是想讓它工作在一個整潔的方式。 –

相關問題