7
\documentclass[a4paper,10pt,twoside]{book}
\usepackage[demo]{graphicx}

\begin{document}

\newcommand{\scalefactor}{scale = 0.25}

\begin{figure}[!ht]
  \centering
  %Next line does not work
  \includegraphics[\scalefactor]{image}
  % The following line works
  %\includegraphics[scale = 0.25]{image}
\end{figure}

\end{document}

Output:

! Package keyval Error: scale = 0.25 undefined.

I need to scale a number of images by the same factor, hence the command.

  1. How to solve this and more importantly
  2. Why is this only specific to \includegraphics
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
aiao
  • 757
  • 1
    You can see why it's a problem if you use \expandafter\includegraphics\expandafter[\scalefactor]{image} which is longer than the intented shortcut. – percusse Feb 08 '13 at 12:31
  • 2
    It's not specific only of \includegraphics but of most (if not all) arguments that consist of key=value options. – egreg Feb 08 '13 at 12:35
  • @percuße I don't use this directly, it is nested in another macro, will using \expandafter hurt the general case? If not clear please let me know I will edit the question – aiao Feb 08 '13 at 12:47
  • The problem here is that the key=value algorithm has to see the = to split both parts. If it is included in a macro it can see it and therefore the whole macro is incorrectly taken as one value-less key, which is of course not defined. – Martin Scharrer Jun 25 '13 at 10:30

3 Answers3

8

If you use

\setkeys{Gin}{scale=0.25}

that key value will be in force by default until you change it, so there is no need to use a LaTeX command macro syntax here.

If your issue is that you want to set the value somewhere else you can go

\newcommand{\scalefactor}{0.25}
...
\includegraphics[scale = \scalefactor]{image}
David Carlisle
  • 757,742
  • As far as I understand this is global scaling factor. If this is the case, then this wont work for me because I scale only specific images – aiao Feb 08 '13 at 12:40
  • well it's local to the current environment or group, or you can set it back to 1 explicitly, or if it really is only sporadic images what's wrong with [scale=.25] – David Carlisle Feb 08 '13 at 12:47
  • aetting back to 1 is a good idea. They are sporadic, all images must be scaled by the same factor. I am currently adding images as I go. If I get Overfull then I step down the scaling a notch. – aiao Feb 08 '13 at 12:50
  • @aiao scale is not the right key for solving the overfull line problems. Use width=\columnwidth or width=.9\textwidth (adjust the factor to suit). – egreg Feb 08 '13 at 13:46
  • @egreg but that would mess up the scaling ratio across images, which is imporatant in my case – aiao Feb 08 '13 at 13:50
8

Try this way:

\newcommand{\scalefactor}{.25}
\includegraphics[scale=\scalefactor]{image}
deimi
  • 1,173
1

Defining a private \includegraphics allows 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}
Ahmed Musa
  • 11,742
  • but if you want to do this you should probably at least use \@protected@edef rather than \edef but even then it makes the argument a "moving argument" in which fragile commands will break. The keyval parser goes to a lot of effort to avoid that, and this disables all of that mechanism. – David Carlisle Feb 08 '13 at 14:04
  • @DavidCarlisle: Thanks. Do you have examples of the possibly fragile arguments? I haven't come across them in \includegraphics. The values of keys or a file to be inserted? – Ahmed Musa Feb 08 '13 at 17:08
  • well you can go width=\ifdim\Gin@nat@width>\textwidth \textwidth \else \Gin@nat@width\fi to set the size to be natural size or textwidth, but that relies on \ifdim not being expanded during the keyval parsing and as I say the parser is designed so that it works. – David Carlisle Feb 08 '13 at 17:12
  • @DavidCarlisle: OK, thanks. I will leave the answer with \@protected@edef. Most likely the OP didn't intend to call fragile commands in the optional argument. – Ahmed Musa Feb 08 '13 at 20:26
  • I am getting this error: ERROR: You can't use `\spacefactor' in vertical mode. Anybody has an idea? – osolmaz Jul 05 '14 at 18:37
  • Use \makeatletter \makeatother. – Ahmed Musa Jul 07 '14 at 09:32