当两者都可用时,在knitr中选择png或pdf

时间:2017-09-01 10:17:47

标签: r latex knitr pdflatex

我喜欢在使用knitr创建(乳胶)文档时生成pdf和png图像文件。这可以使用dev = c(“pdf”,“png”)来完成。 但是,我似乎无法选择(在每个数字的基础上)在我的乳胶图环境中选择哪一个。目前,我可以得到的唯一方法是,图1的png输入文件和图2的pdf输入文件只生成所需的格式(使用dev =“png”,fig.ext =“ PNG“)。

有没有办法可以生成两者,但在乳胶级别可以选择显示哪一个?我想这可以通过在\ includegraphics命令中允许扩展来轻松解决。

任何意见赞赏...

罗恩

最小例子:

\documentclass[a4paper,12pt]{article}
%\VignetteEngine{knitr::knitr}

\DeclareGraphicsExtensions{.pdf,.png}

\begin{document}

%\maketitle
<<knitrInitialization,echo=FALSE>>=
require("knitr", quietly=TRUE)
opts_chunk$set(comment=NA,background='transparent',size='small',fig.width=6,fig.height=6,out.width='\\textwidth',dev=c('pdf','png'))  
@ 

%% this one generates two figures, and the pdf version is shown
%% because of the order in DeclareGraphicsExtensions
\begin{figure}[tb]
\centering
<<testPDF,echo=FALSE>>=
plot(1:10)
@
\caption{PDF figure}
\label{fig:pdf}
\end{figure}

%% if I want to show a png (e.g., because the pdf is too large) I can
%% only do that by not generating a pdf in the first place
\begin{figure}[tb]
\centering
<<testPNG,echo=FALSE,dev='png',fig.ext='png'>>=
plot(1:10)
@
\caption{PNG figure}
\label{fig:png}
\end{figure}


\end{document}

1 个答案:

答案 0 :(得分:0)

你可以简单地把

\DeclareGraphicsExtensions{.png,.pdf}
当您希望PNG具有优先权时,在文档中

,然后

\DeclareGraphicsExtensions{.pdf,.png}
如果您想返回PDF首选项,请稍后

。这适用于LaTeX文档的正文,而不仅仅是在标题中。

相关问题