首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用子图形时,编码器中的意外输出

使用子图形时,编码器中的意外输出
EN

Stack Overflow用户
提问于 2018-01-14 23:33:49
回答 1查看 155关注 0票数 2

我试着把我的一些脚本从sweave改成了knitr,我发现subfigure在使用found时无法正确渲染,而使用sweave则是正确的。我很清楚的知道,像在这个post中一样,code提供了一种在code标题选项中生成子图形的方法,但是我的问题是,我有几个很长的报告,并且希望以最小的更改重用这些代码。此外,我还想了解一下为什么在使用addition时,一个简单的子图示例会失败。

使用sweave的示例

在sweave (1)中,为了遵守标准的编织输出,我将图形保存在了figure子目录中,并创建了两个pdf图形。这是要使用sweave()处理的.Rnw文件的代码。

代码语言:javascript
复制
\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}

输出是这样的:

针织的示例

使用knitr时,输出是直接从R代码块产生的,但是在使用subfigure命令时会出现问题。

代码语言:javascript
复制
\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}

这个问题与图像的大小无关,我可以使用第一个(sweave)代码块生成的图形fig1和fig2来重现图像。我想可能是因为某些软件包加载了cause,这对于这个问题的解决方案来说是非常有意义的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-17 04:26:30

我想我明白为什么了。

首先,我意识到如果我将命令\usepackage{Sweave}添加到knitr输出中,我就不再有这个图形问题了。在sweave代码中搜索,我发现它包含以下命令:

代码语言:javascript
复制
\ifthenelse{\boolean{Sweave@gin}}{\setkeys{Gin}{width=0.8\textwidth}}{}%

在这篇SO文章中,David Calisle解释说,使用

代码语言:javascript
复制
 \setkeys{Gin}{width=0.8\textwidth,height=0.8\textheight,keepaspectratio}

将该命令应用于以下所有\ command图形。

因此,当我加载Sweave时,我的图形宽度有一个限制,但我没有意识到这一点,当我尝试使用longer时,它不再起作用。也许这对其他有同样问题的人很有用。我只需要将\setkeys{Gin}{width=0.8\textwidth}添加到我的脚本中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48251169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档