2

I just found myself having figures that include multiple image files with the exact same options. Is there a way to avoid this repetition? (That would also make it less cumbersome to change these options should the need arise.)

\begin{figure}
\centering
\includegraphics[trim=1cm 2cm 3cm 4cm,clip]{imagefile1.pdf}
\includegraphics[trim=1cm 2cm 3cm 4cm,clip]{imagefile2.pdf}
\includegraphics[trim=1cm 2cm 3cm 4cm,clip]{imagefile3.pdf}
\end{figure}

EDIT: For example in tikz images you could use \tikzstyle{my style}=[<tikz options>] (see e.g here), is there something similar in plain latex?

flawr
  • 1,123
  • 1
  • 9
  • 19
  • \foreach \x in {1,...,3} {\includegraphics[trim=1cm 2cm 3cm 4cm,clip]{imagefile\x.pdf}, with \usepackage{pgffor} in the preamble, for example –  Mar 01 '18 at 16:47

2 Answers2

2

You can set the trim key; however, clip seems to be reset at each call, so you need *:

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\begin{figure}
\centering
\setkeys{Gin}{trim=1cm 2cm 3cm 4cm}

\includegraphics*{example-image-a} \\
\includegraphics*{example-image-b} \\
\includegraphics*{example-image-c}

\end{figure}

\end{document}

enter image description here

egreg
  • 1,121,712
1

This doesn't eliminate the separate invocations, but simplifies each invocation, and makes it easy to change the optional parameters for each invocation all at once.

While the MWE does not eliminate separate invocations, that might be done, if the filenames follow a methodical naming convention. In that case, a \foreach look could be set up, as Christian suggested in a comment.

EDITED to provide macro \settrim (usage example, \settrim{1cm 2cm 3cm 4cm}).

\documentclass{article}
\usepackage{graphicx}
\let\trimparms\relax
\newcommand\xincludegraphics[1]{%
  \expandafter\includegraphics\expandafter[\trimparms]{#1}}
\newcommand\settrim[1]{\def\trimparms{trim=#1,clip}}
\begin{document}
\begin{figure}
\settrim{1cm 2cm 3cm 4cm}
\centering
\xincludegraphics{example-image-a}\\
\xincludegraphics{example-image-b}\\
\xincludegraphics{example-image-c}
\end{figure}
\begin{figure}
\settrim{2cm 2cm 4cm 5cm}
\centering
\xincludegraphics{example-image-a}\\
\xincludegraphics{example-image-b}\\
\xincludegraphics{example-image-c}
\end{figure}
\end{document}

enter image description here