4

Is there a way to keep the code and the figure together in knitr when using a figure environment in LaTeX?

This style:

\documentclass{article}
\begin{document}
writing
<<cars, fig.cap="this is a figure.">>=
with(mtcars, plot(mpg, disp))
@
writing
\end{document}

will result in the figure being in a float in LaTeX -- which is what I want, except that the figure can float away from the code.

My current workaround is writing the figure environment in LaTeX manually:

\begin{figure}
<<cars2>>=
with(mtcars, plot(mpg, disp))
@
\caption{this is a figure.}
\end{figure}

Is there a more elegant way that I can do this in knitr? Adding the fig.pos='h' option is not enough. fig.show='asis' also does not get it done.

Do I have to write my own hook? Can this be done easily?

Community
  • 1
  • 1
beanumber
  • 767
  • 5
  • 13

0 Answers0