I am trying to include an image in my document:
\documentclass[a4paper,10pt,twoside]{book}
\usepackage{graphicx}
\begin{document}
\def\foo{scale=0.1}
\includegraphics[\foo]{fig1.eps}
\end{document}
gives me the error
ERROR: Package xkeyval Error: `scale=0.1' undefined in families `Gin'.
It has something to do with macro expansion, but I was not able to solve it. Then I saw this question with the following answer
Defining a private
\includegraphicsallows you to call macros as optional arguments:\protected\def\newincludegraphics{\@testopt\new@includegraphics{}} \def\new@includegraphics[#1]{% \begingroup \@protected@edef\x{\endgroup \noexpand\includegraphics \if\relax\detokenize{#1}\relax\else[#1]\fi }\x } \newincludegraphics[\scalefactor]{image}
where the new macro \newincludegraphics was defined.
MWE
\documentclass[a4paper,10pt,twoside]{book}
\usepackage{graphicx}
\begin{document}
\protected\def\newincludegraphics{\@testopt\new@includegraphics{}}
\def\new@includegraphics[#1]{%
\begingroup
\@protected@edef\x{\endgroup
\noexpand\includegraphics
\if\relax\detokenize{#1}\relax\else[#1]\fi
}\x
}
\def\foo{scale=0.1}
\newincludegraphics[\foo]{fig1.eps}
\end{document}
Nevertheless I still get this error:
ERROR: You can't use `\spacefactor' in vertical mode.
--- TeX said ---
\@->\spacefactor
\@m
l.62 \newincludegraphics
[scale=0.1]{fig1.eps}
What could be the cause of this error, and is there a workaround without defining another macro to include graphics?
\def\foo{scale=0.1}the intended usage is that you use\setkeys{Gin}{scale=0.1}...\includegraphics{fg1}– David Carlisle Jul 06 '14 at 09:50\makeatletterand\makeatotherbut cannot seem to make it work, would you care to post an answer? – osolmaz Jul 06 '14 at 10:07\expandafter\includegraphics\expandafter[\foo]{fig1.eps}ro expand\foofirst. – David Carlisle Jul 06 '14 at 10:17\expandafter\includegraphics\expandafter[\foo]{fig1.eps}as an answer, I can mark it. But I also think this question might be a duplicate. – osolmaz Jul 06 '14 at 11:24