5

I would like to make use of a variable that I can use multiple times to make sure all my figures are sized the same way.

At the moment I have multiple images with the following code:

\documentclass[a4paper, 11pt]{report}
\usepackage{graphicx}

\begin{document}
\begin{figure}[h!]
    \centering
    \frame{\includegraphics[width=\textwidth,trim={4.0cm 4.0cm 4.5cm 4.5cm},clip]{SomeImage.png}}
    \caption{Some caption describing image.}
    \label{fig: SomeImage}
\end{figure}
\end{document}

I would now like to make a variable for the trim settings, so that I can reuse it for all figures that are similar to this one. I therefore proceed as follows:

\documentclass[a4paper, 11pt]{report}
\usepackage{graphicx}
\newcommand{\ncTrimmed}{{4.0cm 4.0cm 4.5cm 4.5cm}}

\begin{document}
\begin{figure}[h!]
    \centering
    \frame{\includegraphics[width=\textwidth,trim=\ncTrimmed,clip]{SomeImage.png}}
    \caption{Some caption describing image.}
    \label{fig: SomeImage}
\end{figure}
\end{document}

Whatever I do to the new command, I cannot make it work. Latex keeps giving me errors like "missing } inserted" and similar. What am I doing wrong?

4

You'll have to expand \ncTrimmed which you can do in the following way:

\begingroup
\edef\x{\endgroup
  \noexpand\frame{\noexpand\includegraphics
    [width=\textwidth,trim=\ncTrimmed,clip]{example-image}}}\x

In short, the use-case here represents

\begingroup
\edef\x{\endgroup
  <stuff>}\x

\edef\x{<def>} expands everything contained within <def>. Since \endgroup is not expandable, the definition of \x is temporary (within a \begingroup and \endgroup, once \x is used). <stuff> here contains whatever you want expanded, while \noexpand<cmd> leaves <cmd> not expanded. So, after \x will be expanded to

\endgroup
  \frame{\includegraphics[width=\textwidth,trim={4.0cm 4.0cm 4.5cm 4.5cm},clip]{example-image}}

(since \textwidth isn't expandable). You can see this is the case when using

\begingroup
\edef\x{\endgroup
  <stuff>}\show\x

\show\x will show you what \x is defined as. You'll see everything remains the same, apart from an expanded \ncTrimmed.

| improve this answer | |
  • Can you explain a bit more what is happening? Seems like a lot of extra code has come together. – user3604362 Sep 22 '19 at 19:23
  • 2
    @user3604362 \edef will expand everything inside the braces and store the result in \x. The expansion will act on everything except non-expandable tokens (such as \textwidth and tokens preceded by \noexpand, then \x will contain \frame{\includegraphics[width=\textwidth,trim=<expansion-of-\ncTrimmed>,clip]{example-image}}, which is what you want to do. Finally you use \x. The \begingroup and \endgroup are there only to ensure that the meaning of \x doesn't change in this process. – Phelype Oleinik Sep 22 '19 at 20:06
  • 1
    Thanks @PhelypeOleinik. I was on the road. – Werner Sep 22 '19 at 20:21
  • I am still a bit unsure why I suddenly have to expand something. Could you maybe elaborate on that or offer a good reference? – user3604362 Sep 23 '19 at 15:18
  • @user3604362: It all depends on the functionality implemented by \includegraphics when reading its key-value arguments. In some cases, subsequent processing of information is expanded anyway, but in other cases, the argument is used as-is. And when TeX tokenizes content (or consumes it), it would consume \ncTrimmed as a single element, rather than <lx> <ly> <ux> <uy> (four separate elements). It's not that things suddenly required it; it's always been like that. You've just never had to use it in this way. – Werner Sep 23 '19 at 16:23

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.