Adding options [keepaspectratio=true, scale = 0.75] to \includegraphics{} in Sweave

时光毁灭记忆、已成空白 提交于 2019-12-19 08:03:26

问题


I have the following R code

<<Q1b1, fig=TRUE>>=  
qqnorm(resid(model1), main=NULL)
@

and I would like to add the option [keepaspectratio=true, scale = 0.75] to the \includegraphics{} call so that the above R code chunk generates

\begin{figure}[ht]
\caption{}
\begin{center}
\includegraphics[keepaspectratio=true, scale = 0.75]{filename.pdf}
\label{fig:1}
\end{center}
\end{figure}

I know you can specify the width and height using \SweaveOpt{width=x, height=y} but I want to specify the scale and have it keep the aspect ratio of the figure when it's generated.

Is there an easy way to do this?

Thanks


回答1:


You can set the width for every figure in your Sweave document with \setkeys{Gin} instruction. For example, the following in the preamble of your document makes every figure width 80% of the text width on your page :

\setkeys{Gin}{width=0.8\textwidth}

Otherwise you can add the include=FALSE argument to your code chunk and then create the \includegraphics instruction manually. By default, if your Sweave file is called test.Rnw, then the name of the pdf file generated by the code chunk will be test-Q1b1.pdf. So you can do something like :

<<Q1b1,fig=true,include=false,echo=false>>=  
qqnorm(rnorm(100))
@

\begin{figure}[ht]
\caption{}
\begin{center}
\includegraphics[keepaspectratio=true,scale=0.1]{test-Q1b1.pdf}
\label{fig:1}
\end{center}
\end{figure}

However, I've made some tests on your example but the scale parameter doesn't seem to have any effect on the figure here. Others like angle do, though.




回答2:


If you do not have \usepackage{Sweave} in your Sweave file preamble, it will be inserted automatically as the last line before the \begin{document} statement. Therefore you need to put

\setkeys{Gin}{width=0.8\textwidth}

, which is actually the default of Sweave style file as specified in \usepackage{Sweave}.

AFTER the \begin{document} statement. See Sweave document (http://www.statistik.lmu.de/~leisch/Sweave/Sweave-manual.pdf) section 4.1.2 for detail.



来源:https://stackoverflow.com/questions/5258901/adding-options-keepaspectratio-true-scale-0-75-to-includegraphics-in-swe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!