TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I notice that includegraphics takes what seems to be named arguments:

\includegraphics[width=50,height=25,trim=1 2 3 4,clip]{an_img.png}

I'd like to wrap a number of my includegraphics calls in a macro, so that the following commands


expand to

\includegraphics[width=50,height=25,trim=1 2 3 4,clip]{an_img.png}
\includegraphics[width=50,height=25,trim=1 2 3 4,clip,rotate=90]{an_img.png}


I've tried using

\DeclareDocumentCommand\song{ m g }{
  \includegraphics[width=50,height=25,trim=1 2 3 4,clip,rotate=90\IfNoValueF{#2}{,#2}]{#1}

but I get the dreaded

! Package keyval Error: keepaspectratio,trim=0 0 0.5in 0,clip undefined.


How should I go about doing this?

share|improve this question
up vote 8 down vote accepted

A slightly modified interface via the definition

  \includegraphics[width=50,height=25,trim=1 2 3 4,clip,#1]{#2}}%

would allow to add options as needed to \happygraphics[<options>]{<file>}.

A similar interface is provided through xparse:

\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\happygraphics}{O{,} m}{%
  \includegraphics[width=50,height=25,trim=1 2 3 4,clip,#1]{#2}}%

The above takes advantage of supplying empty (default) key-values as ,.

Since the key-value interface is comfortable with supplying empty options (or consecutive commas ,,,...), there's no need for extra testing of the existence of an optional argument. Unless, of course, you want to do more based on whether or not the use supplied an optional argument.

share|improve this answer
How can I know whether the user has elected to use the optional argument? This seems very clear in DeclareDocumentCommand. – Richard Mar 12 '13 at 19:45
@Richard: If the user did not supply the optional argument, then it defaults to , (as specified by the second optional argument of \newcommand) and it's stored in #1. If the user did supply an optional argument, it is still stored in #1. So, regardless of whether they supply something or not, #1 contains that, and is passed to the list of options for \includegraphics. xparse allows the same interface using the O{,} m argument type. – Werner Mar 12 '13 at 19:49
Got it working, thanks, @Werner. – Richard Mar 12 '13 at 19:51

You don't need to define a new top level command, if you execute

\setkeys{Gin}{width=50,height=25,trim=1 2 3 4,clip}

then those keys will be automatically set for all \includegraphics in the scope of the \setkeys declaration.

share|improve this answer
Thanks, David. In reality, the code I've shown is part of a much more complex macro of which \includegraphics is a part, so I don't think there is a way to get around passing the parameters in the manner I indicated. – Richard Mar 12 '13 at 20:15
In such cases, I really like ConTeXt's inheritance mechanism. You can define a set of options as \defineexternalfigure[myoptions][width=50pt, height=25pt]; for images where you just want to use these options use: \externalfigure[filename][myoptions]; for images where you want to use these options with other options as well use: \externalfigure[filename][myoptions][orientation=90]. LaTeX assumes that there is one set of default options, which leads to the need to define macros to handle this common use case. – Aditya Mar 13 '13 at 1:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.