Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am looking for a macro with two optional arguments and tried the following code:

%% Usage \mfig[position][trim]{name-figure}{caption}{label}
\NewDocumentCommand{\mfig}{o o m m m}{%
        \Figure[trim={.0\width} {\IfValueTF{#2}{#2}{.05}\height}%
            {.0\width} {.0\height},clip,%

\section{A simple test}

\mfig{myfigure}{This is just a caption to the demo figure.}{fig:test}
Figure~\ref{fig:test} shows a test. \lipsum[3].

The first optional argument gives the margin figure vertical position and the second gives the trim coordinates (an inferior cut-off actually).

It is ok to to use:


to move vertically the margin figure about 1.5cm up, for instance, and with the default trim coordinates of .05, but I am unable to use


to cut-off 55% of my graphic, while using the default position of 0cm, because my "macro" is unable to recognize the optional argument #2 without to explicitly use the #1.

So, my question is: is there any way to specify only the #2 optional argument while the macro accepts the default #1 argument like the above?

share|improve this question
optional arguments are positional so you can place one in a different position such as o m o m m but it's usually better to consider a variant syntax such as [a,b] or [a=x, b=y] that is the key value syntax of \includegraphics is designed to allow optional arguments without this problem, but your wrapper hides that. –  David Carlisle Jul 13 at 20:13
@David Yes, I can see it now ... much more clever, and simple, I believe. But I did not understood your variant syntax note. How does it changes my macro/function declaration and usage? –  jotagah Jul 13 at 23:03
if you have two optional arguments and put one before and one after the mandatory argument they can be distinguished. with o o m if you have just one [] argument it has to be #1 but if you have o m o you can have \foo[a]{b}[c] and distinguish \foo[a]{b} from \foo{b}[c] –  David Carlisle Jul 13 at 23:08
But how to use [a,b] variant? –  jotagah Jul 13 at 23:15
As a note, I would very much recommend using the default-value syntax for optional arguments in the argspec. Instead of oommm, use O{0cm} O{0.05} mmm. –  Sean Allred Jul 14 at 1:43

1 Answer 1

Why not use keys? That way you can have practically as many parameters as you would like.

Of the many ways you could do this, here's one:

In the preface you could have

\def\mycaption{default caption}
\def\mylabel{default label}
  caption/.store in=\mycaption,
  label/.store in=\mylabel,
  fig width/.store in=\myfigwidth,
  fig height/.store in=\myfigheight,

Then you could define your function as

      fig width=0cm,
      fig height=0.05,
        \Figure[trim={.0\width} {\myfgheight\height}%
            {.0\width} {.0\height},clip,%
share|improve this answer
@Ellett I do not know the pgfkeys package and so I have to read a bit more to understand your solution. May I ask you how to call my macro/function using only #1, only #2,both, or none of the optional arguments? –  jotagah Jul 13 at 23:12

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.