knitr 的子图或子标题? [英] Subfigures or Subcaptions with knitr?
问题描述
是否可以使用 knitr 生成子图(带有关联的子标题)?这是一个最小的工作示例:
Is it possible to produce subfigures (with associated subcaptions) using knitr? Here is a minimal working example:
documentclass{article}
egin{document}
<<echo = FALSE, fig.cap = c("Some numbers.", "Some more numbers."), out.width = "0.5\textwidth", fig.align = "center">>=
plot(1:10)
plot(30:100)
@
end{document}
这会产生两个标记为图 1 和图 2 的图,并带有定义的标题(分别).但我希望它们被标记为图 1a"和图 1b",就像您可以使用 subcaption LaTeX 包一样.
This results in two figures labelled Figure 1 and Figure 2 with captions as defined (respectively). But I want them to be labelled "Figure 1a" and "Figure 1b", as you can do with the subcaption LaTeX package.
我知道有一个 knitr 选项fig.env",但这并不能解决它(至少不使用,例如,fig.env = 'subfigure'").这里有一篇关于 Sweave 的类似帖子,但解决方案是不优雅的 hack:http://texblog.org/2011/12/01/sweave-subfig-controlling-figure-size-and-placement/
I know there is a knitr options "fig.env", but this doesn't solve it (at least not using, for example, "fig.env = 'subfigure'"). There is a similar post here regarding Sweave, but the solution is an inelegant hack: http://texblog.org/2011/12/01/sweave-subfig-controlling-figure-size-and-placement/
推荐答案
knitr
(>= v1.5) 支持子图.您可以使用块选项 fig.subcap
.这是一个最小的例子.
knitr
(>= v1.5) supports subfigures. You can use the chunk option fig.subcap
. Here is a minimal example.
documentclass{article}
usepackage{subfig}
egin{document}
<<fig-sub, fig.cap='two plots', fig.subcap=c('one plot', 'the other one'), out.width='.49\linewidth'>>=
plot(1:10)
plot(rnorm(10), pch=19)
@
end{document}
这篇关于knitr 的子图或子标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!