12

From the documentation of the graphicx package:

Most of the keyval keys used in the graphicx package may also be set using the command \setkeys provided by the keyval package.

For instance, suppose you wanted all the files to be included in the current document to be scaled to 75% of the width of the lines of text, then one could issue the following command:

\setkeys{Gin}{width=0.75\textwidth} 

Here ‘Gin’ is the name used for the keyval keys associated with ‘Graphics inclusion’. All following \includegraphics commands (within the same group or environment) will act as if [width=0.75\textwidth] had been specified, in addition to any other key settings actually given in the optional argument.

I am trying to use this to create an argument that accepts keys using the pgfkeys package and passes the ones it does not recognize on to an \includegraphics command. Here's a toy working example:

\documentclass[convert]{standalone}
\usepackage{graphicx,pgfkeys}

\newcommand{\mykeyset}[1]{\pgfqkeys{/me}{#1}}

\mykeyset{graphic options/.code={}}
\mykeyset{set graphic option/.style={graphic options/.append code=#1}}

%Set up a handler for unknown keys
\mykeyset{.unknown/.code = %
    {%
        \edef\unknownkey{\pgfkeyscurrentname}%
        \mykeyset{set graphic option/.expand once = {\expandafter\setkeys\expandafter{\expandafter G\expandafter i\expandafter n\expandafter}\expandafter{\unknownkey=#1}}}%
    }%
}

\newcommand{\myincludegraphics}[2][]{\begingroup%
    \mykeyset{#1}%
    \mykeyset{graphic options}%
    \includegraphics{#2}%
    \endgroup%
}

\begin{document}
\myincludegraphics[width = 5cm, height = 8cm]{example-image}
\myincludegraphics[scale=0.5]{example-image}
\myincludegraphics{example-image}
\end{document}

with output

enter image description here

Note that this works for the width and height keys, but that not for the scale key. This is not a problem in itself--I have a method for handling the scale key separately--but I would like to know which other keys require special treatment. Hence, my question:

Which keys to the \includegraphics command cannot be set globally?

3

1 Answer 1

13

Brilliant documentation isn't it:-)

In general setting keys is more or less just making macro definitions, so the ones you can't set in advance are the ones that are initialised by \includegraphics before it processes the keys. Looking at the code (strangely more revealing than the documentation) the only relevant initialisation is

   \@tempswafalse

and the keys that use that boolean flag are just angle and scale.

The reason for the flag is that most graphics inclusion back ends can "natively" scale the image so if scale is used on its own it is passed on to the back end driver code, but if the image has already been rotated and then the rotated image is scaled, \includegraphics essentially inserts a \scalebox{}{\rotatebox{... around the basic image inclusion. Perhaps it could have been handled differently so your scale example works, but it was a long time ago:-)

For slightly different reasons clip should also be listed, see

http://www.latex-project.org/cgi-bin/ltxbugs2html?pr=graphics/2890

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .