Apparently \includegraphics
does only one step of expansion for its argument, so you end up with \includegraphics[width=0.5\linewidth]{\ImageOne}
and this is not what TeX expects.
Use \let
instead of \renewcommand
:
\documentclass{memoir}
\usepackage{graphicx}
\newcommand{\ImageOne}{image1.png}
\newcommand{\ImageTwo}{image2.png}
\newcommand{\MyFigure}{%
\begin{figure}
\centering
\includegraphics[width=0.5\linewidth]{\currentimage}
\end{figure}}
\begin{document}
\let\currentimage\ImageOne
\MyFigure
\let\currentimage\ImageTwo
\MyFigure
\end{document}
However, I don't think this is a good way to go.
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}
Best Answer
The example in the question reveals quite some show stoppers:
Package
graphics
needs to know the file name extension and calls LaTeX's\filename@parse
to split the path specification.\filename@parse
expands the first token once. Thus it is possible to put the starting path in a macro. But after the expansion step, further macros or nested macros are not expanded. They will break the file name parser, if they hide important path syntax elements. Nested macros can be avoided by expanding them by defining a macro with\edef
and then passing the macro with the expanded path to\includegraphics
.\unskip
and\ignorespaces
are unexpandable tokens, they will appear as path component. Check out packagetrimspaces
or similar packages to strip spaces at the begin and end from a string.Also LaTeX's
\input
is not expandable. Even the primitive version\@@input
is quite tricky to use:\endlinechar=-1
.\everyeof{\noexpand}
with e-TeX.Package
catchfile
provides means to put files into macros.If you want to use
\includegraphics{\imagepath}
, then you can inspect\imagepath
:will print the following to the console and log output, assuming
\imagepath
containsx/y/z.png
:The shown path should be free of TeX commands or unwanted spaces.