# How to use a macro inside \includegraphics options?

I have to do several figures like this:

\begin{figure}[h!]
\centering
\includegraphics[trim = 9.55cm 1.8cm 10.25cm 4.05cm, clip,width=1\textwidth]{./../ficherosComunes/images/capturaapp/01-actividadprincipal.png}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[trim = 9.55cm 1.8cm 10.25cm 4.05cm, clip,width=1\textwidth]{./../ficherosComunes/images/capturaapp/02-listadoficheros.png}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[trim = 9.55cm 1.8cm 10.25cm 4.05cm, clip,width=1\textwidth]{./../ficherosComunes/images/capturaapp/03-listadoficherodetalles.png}
\end{figure}


As it can be seen there's all times repeated trim = 9.55cm 1.8cm 10.25cm 4.05cm, clip,width=1\textwidth Would it be possible to write it in a variable? I have tried to define a \newcommand{\texttrim}{trim = 9.55cm 1.8cm 10.25cm 4.05cm, clip,width=1\textwidth} but it doesn't work if I put it as an option. I have also tried to define lengths (to have at least the trimming lengts defined) but they don't work either.

• A hint: You can use \graphicspath{{./../ficherosComunes/images/capturaapp/}} to add your image folder to the search scope and then use \includegraphics{01-actividadprincipal}. Furthermore you can omit the suffix .png since graphicx finds the right extension automatically. – Tobi Aug 12 '13 at 22:13
• Thanks, that file was just a testing one before inserting the code in the final document (another .tex) for quick compilation. In the final one I use the graphicspath already. And about the extension I got used to use it as in some images I have some .svg that I converted into .pdf, as pdflatex doesn't support .svg, with the same name. But yes, maybe I should omit the extension except in those especial cases – Álvaro Aug 13 '13 at 18:14

There are several ways to do this:

## 1. define a new command

\newcommand{\trimmedgraphic}[2][]{%
\includegraphics[trim = 1cm 2cm 1cm 2cm,clip,width=1\textwidth,#1]%
{#2}%
}


You can define the command with an optional argument to be able to pass additional options to \includegraphics. You may add the {figure} stuff if you like.

## 2. define a command to preset the keys

\newcommand{\settrimming}{%
\setkeys{Gin}{%
trim = 1cm 2cm 1cm 2cm,clip=true,
width=\textwidth,
}
\presetkeys{Gin}{clip}{}
}


If you use \settrimming all following graphics will be trimmed. You can use it in the preamble (then you can use the definition directly and there's no need to wrap it in \settrimming) or you use it inside a group (like {figure} env) to scope its effect. Note that you'll need to load xkeyval.sty for this way.

## 3. using \edef and \expandafter

\edef\trimoptions{trim = 1cm 2cm 1cm 2cm, clip,width=1\textwidth}


You can use \edef to store the options an then make it usable in the option list by adding two \expandafter:

\expandafter\includegraphics\expandafter[\trimoptions]{example-image}


## full MWE example

\documentclass{article}

\usepackage{xkeyval}% necessary for \presetkeys
\usepackage{graphicx}

\newcommand{\trimmedgraphic}[2][]{%
\includegraphics[trim = 1cm 2cm 1cm 2cm,clip,width=1\textwidth,#1]%
{#2}%
}

\newcommand{\settrimming}{%
\setkeys{Gin}{%
trim = 1cm 2cm 1cm 2cm,clip=true,
width=\textwidth,
}
\presetkeys{Gin}{clip}{}
}

\edef\trimoptions{trim = 1cm 2cm 1cm 2cm, clip,width=1\textwidth}

\begin{document}
\begin{figure}
\includegraphics[trim = 1cm 2cm 1cm 2cm, clip,width=1\textwidth]%
{example-image}
\caption{Normal figure}
\end{figure}

\begin{figure}
\trimmedgraphic{example-image-a}
\caption{Figure using trimmedgraphic}
\end{figure}

\begin{figure}
\trimmedgraphic[width=0.5\textwidth]{example-image-b}
\caption{Figure using trimmedgraphic}
\end{figure}

\begin{figure}
\settrimming
\includegraphics{example-image-c}
\caption{Figure using settrimming}
\end{figure}

\begin{figure}
\expandafter\includegraphics\expandafter[\trimoptions]{example-image}
\caption{Figure using edef and expandafter}
\end{figure}
\end{document}


You could define the whole thing as one command, taking the figure details as arguments, like this

\newcommand\myfigure[2]{%
\begin{figure}[h!]
\centering
\includegraphics[trim = 9.55cm 1.8cm 10.25cm 4.05cm, clip,width=1\textwidth]%
{#1}
\caption{#2}
\end{figure}%
}


and then simply call it like this

\myfigure{./../ficherosComunes/images/capturaapp/01-actividadprincipal.png}{Captura de pantalla de ActividadPrincipal.java}