在knitr中,如何使用Latex中的\ textwidth设置图形宽度

Mar*_*ees 6 latex width figure knitr

我尝试了以下设置knitr和LaTeX的图宽度:

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

但是,我收到以下错误:

<to be read again> 
                >
l.54 <<fig.width=\textwidth>
                        >
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Pet*_*ter 8

fig.width选项应为数字,用于设置图形本身的大小.要控制LaTeX显示图形的方式,请使用out.widthout.height选项.这些选项需要是字符串,您需要转义反斜杠.

<<plot1, fig.width = 5, fig.height = 5, out.width = "0.48\\textwidth">>=
@
Run Code Online (Sandbox Code Playgroud)

将生成一个5英寸乘5英寸的图形,占用最终文档中48%的文本宽度.也就是说,文件plot1-1.pdf是一个五英寸乘五英寸的图形,以及LaTeX代码

\includegraphics[width=0.48\textwidth]{figure/plot1-1}
Run Code Online (Sandbox Code Playgroud)

将放在生成的.tex文件中.