I tried to change some of my scripts from sweave
to knitr
and have found that subfigure failed to render properly when using knitr while they were correct with sweave. I am well aware that knitr offers a way to produce subfigure in knitr header options like in this post, but my question I have several long reports and would like to re-use these code with minimal change. In addition I would like to understand why when using knitr, a simple subfigure example fails.
我试图将我的一些脚本从sweave更改为knitr,并且发现当使用knitr时子图未能正确呈现,而它们与sweave一致。我很清楚knitr提供了一种在knitr header选项中生成子图的方法,就像在这篇文章中一样,但是我的问题是我有几个长报告,并希望以最小的改变重用这些代码。另外我想了解为什么在使用knitr时,一个简单的子图示例失败了。
Example with sweave
In sweave (1) to respect standard knitr output I save the figure in the figure subdirectory, and I create two pdf figures. This is the code for the .Rnw file to be processed using sweave()
.
在sweave(1)中尊重标准knitr输出我将图保存在图子目录中,并创建两个pdf数字。这是使用sweave()处理的.Rnw文件的代码。
\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{float}
\graphicspath{{figure/}}
\title{test for sweave}
\date{}
\begin{document}
\maketitle
<<multiplefig, echo= FALSE, results=hide>>=
dir.create("figure",showWarnings = FALSE)
mapply(function(X){
pdf(paste0(getwd(),"/figure/fig-",X,".pdf"),height=6,width=6)
plot(X,main=X)
dev.off()
},c(1:2))
@
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics{fig-1}
\caption{subcaption1}
\end{subfigure}%
~
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics{fig-2}
\caption{subcaption2}
\end{subfigure}
\caption{Subfigures properly placed side by side in sweave using
the subfigure command.}
\end{figure}
\end{document}
输出是这样的:
Example with knitr
With knitr, the output is produced straight from the R code chunk, but when using the subfigure command there is a problem.
使用knitr,输出直接从R代码块生成,但是在使用子图命令时会出现问题。
\documentclass[a4paper]{article}
\usepackage{subcaption}
\usepackage{float}
\graphicspath{{figure/}}
\title{problem when using knitr}
\date{}
\begin{document}
\maketitle
<<init, include=FALSE>>=
library(knitr)
@
<<knitrfig, fig.height=6, fig.with=6, include=FALSE>>=
mapply(function(X)plot(X,main=X),c(1:2))
@
This is where the problem lies
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics{knitrfig-1}
\caption{subcaption1}
\end{subfigure}%
~
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics{knitrfig-2}
\caption{subcaption2}
\end{subfigure}
\caption{With knitr the figure is not rendered properly}
\end{figure}
\end{document}
This problem is not linked with the size of the image, I can reproduce it by using the figures fig1 and fig2 produced by the first (sweave) code chunk. I think that some of the packages loaded with knitr might be the cause, and would be much gratefull for a solution to this problem.
这个问题与图像的大小无关,我可以通过使用第一个(sweave)代码块产生的数字fig1和fig2来重现它。我认为一些装有knitr的软件包可能是原因,并且非常感谢解决这个问题。
1 个解决方案
#1
0
I think I understand why.
我想我理解为什么。
First I realized that if I add the command \usepackage{Sweave}
to the knitr tex output, I no longer had that figure problem. Searching within the sweave code, I've found that it includes the following command :
首先我意识到,如果我将命令\ usepackage {Sweave}添加到knitr tex输出,我就不再有这个数字问题。在sweave代码中搜索,我发现它包含以下命令:
\ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%
In this SO post, David Calisle explains that using
在这篇SO帖子中,David Calisle解释说使用
\setkeys{Gin}{width=0.8\textwidth,height=0.8\textheight,keepaspectratio}
will apply the command to all following \includegraphics.
将命令应用于以下所有\ includegraphics。
So I had a restriction on the width of my figures when loading Sweave but I wasn't aware of that, and it no longer worked when I tried to use knitr. Maybe this will be usefull to someone else with the same problems. I will just have to add \setkeys{Gin}{width=0.8\textwidth}
to my scripts.
因此我在加载Sweave时对我的数字宽度有限制,但我不知道这一点,当我尝试使用knitr时它不再有效。也许这会对遇到同样问题的其他人有用。我只需要在我的脚本中添加\ setkeys {Gin} {width = 0.8 \ textwidth}。
#1
0
I think I understand why.
我想我理解为什么。
First I realized that if I add the command \usepackage{Sweave}
to the knitr tex output, I no longer had that figure problem. Searching within the sweave code, I've found that it includes the following command :
首先我意识到,如果我将命令\ usepackage {Sweave}添加到knitr tex输出,我就不再有这个数字问题。在sweave代码中搜索,我发现它包含以下命令:
\ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%
In this SO post, David Calisle explains that using
在这篇SO帖子中,David Calisle解释说使用
\setkeys{Gin}{width=0.8\textwidth,height=0.8\textheight,keepaspectratio}
will apply the command to all following \includegraphics.
将命令应用于以下所有\ includegraphics。
So I had a restriction on the width of my figures when loading Sweave but I wasn't aware of that, and it no longer worked when I tried to use knitr. Maybe this will be usefull to someone else with the same problems. I will just have to add \setkeys{Gin}{width=0.8\textwidth}
to my scripts.
因此我在加载Sweave时对我的数字宽度有限制,但我不知道这一点,当我尝试使用knitr时它不再有效。也许这会对遇到同样问题的其他人有用。我只需要在我的脚本中添加\ setkeys {Gin} {width = 0.8 \ textwidth}。