1

I would like to crop an image that is not inserted (which has been searched a lot), but an image that is generated in R code.

\documentclass{article}
\setlength{\parindent}{0pt}
\setcounter{secnumdepth}{0}
\usepackage[round,sort]{natbib}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{float}
\usepackage{subfig}
\usepackage{verbatim}
\usepackage[colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}
\usepackage{amssymb,amsbsy,amsmath}
\usepackage{epsfig}
\usepackage[left=3cm,top=3cm,bottom=3.5cm,right=3cm]{geometry}
\usepackage{fancyhdr}
\numberwithin{equation}{section}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<label=plotName, include=FALSE>>=
someFunctionInR(someParameter)
@

\setkeys{Gin}{width=4in, height=4in}
\begin{figure}
  \begin{center}
<<label=plotName,fig=TRUE,echo=FALSE>>= 
<<plotName>>
@ 
\end{center}
\caption{Some caption.}
\label{fig:plotName}
\end{figure}

I tried to insert a statement as follows:

\includegraphics[clip=true,trim=0cm 1cm 0cm 1cm]

I did this in several locations, but for instance, immediately in the line after \begin{center}, I get an error ("Undefined control sequence").

Any suggestions would be greatly appreciated!

  • You will have better success with trimming the figure inside the Sweave block using the R plot options commands. – R. Schumacher Feb 25 '15 at 1:19
1

The adjustbox package provides the adjustbox environment which you should be able to use for this purpose. Your setup should resemble:

% ... preamble content
\usepackage{adjustbox}

% ... main document content

\begin{figure}[htb]
  \centering
  \begin{adjustbox}{width=4in,height=4in,clip,trim=0cm 1cm 0cm 1cm}
  % ...sweave content
  \end{adjustbox}
  \caption{...}\label{..}
\end{figure}

% ... other main document content

\end{document}

Note the use of \centering rather than the center environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.