Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In knitr, how to set figure width with \textwidth from Latex

I have tried the following to set figure width with knitr and LaTeX:

\documentclass{paper}
\begin{document}
<<fig.width=\textwidth>>
x = runif(1000)
plot(x)
@
\end{document}

However, I get the following error:

<to be read again> 
                >
l.54 <<fig.width=\textwidth>
                        >

What am I doing wrong?

like image 855
Marc Kees Avatar asked May 29 '17 17:05

Marc Kees


1 Answers

The fig.width option should be numeric for setting the size of the graphic itself. For controlling how the figure is displayed by LaTeX use the out.width and out.height options. These options need to be strings, and you'll need to escape backslashes.

The chunk

<<plot1, fig.width = 5, fig.height = 5, out.width = "0.48\\textwidth">>=
@

will generate a five inch by five inch graphic that will take up 48% of the textwidth in the final document. That is, the file plot1-1.pdf is a five inch by five inch graphic, and the LaTeX code

\includegraphics[width=0.48\textwidth]{figure/plot1-1}

will be placed in the resulting .tex file.

like image 151
Peter Avatar answered Oct 22 '22 21:10

Peter