[Tex/LaTex] How to use a macro inside \includegraphics options

graphicsmacrosoptional arguments

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}
  \caption{Captura de pantalla de ActividadPrincipal.java}
\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}
  \caption{Captura de pantalla de ActividadFicheros.java.}
\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}
  \caption{Captura de pantalla de ActividadFicheroDetalles.java.}
\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.

Best Answer

There are several ways to do this:

1. define a new command

(as carsten said already)

\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}