Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subfigures or Subcaptions with knitr?

Tags:

Is it possible to produce subfigures (with associated subcaptions) using knitr? Here is a minimal working example:

\documentclass{article}  \begin{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} 

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.

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/

like image 904
dynamo Avatar asked Sep 22 '12 18:09

dynamo


People also ask

What are Subfigures?

Noun. 1. subfigure - a figure that is a part of another figure. figure - a combination of points and lines and planes that form a visible palpable shape. flank - a subfigure consisting of a side of something.

How do you make Subfigures in latex?

To create subfigure in latex, you can use both \begin{minipage}... \end{minipage} and \begin{subfigure}... \end{subfigure} block to insert subfigures or sub-images. Subfigurs are generally inserted horizontally in one or multiple rows.


1 Answers

knitr (>= v1.5) supports subfigures. You can use the chunk option fig.subcap. Here is a minimal example.

\documentclass{article} \usepackage{subfig} \begin{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} 

subfigures in knitr

like image 83
Yihui Xie Avatar answered Mar 22 '23 10:03

Yihui Xie