# How to declare a macro with a variable argument list?

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

\documentclass{tufte-handout}
%
\usepackage{xparse}
\usepackage{lipsum}
\usepackage[demo]{graphics}
\usepackage{easyfig}
%
%% Usage \mfig[position][trim]{name-figure}{caption}{label}
%
\NewDocumentCommand{\mfig}{o o m m m}{%
\begin{marginfigure}[\IfValueTF{#1}{#1}{0}cm]
\Figure[trim={.0\width} {\IfValueTF{#2}{#2}{.05}\height}%
{.0\width} {.0\height},clip,%
width=\linewidth,keepaspectratio=true,%
caption={#4},label={#5},center,here]{#3}
\end{marginfigure}%
}
%
\begin{document}

\section{A simple test}

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


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:

\mfig[-1.5]{name}{caption}{label}


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

\mfig[.55]{name}{caption}{label}


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?

-
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 '14 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 '14 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 '14 at 23:08
But how to use [a,b] variant? – jotagah Jul 13 '14 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 '14 at 1:43

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

\usepackage{pgfkeys}
\def\mycaption{default caption}
\def\mylabel{default label}
\def\myfigwidth{0pt}
\def\myfigheight{0.05}
\pgfkeys{/jotagah/myfig/.cd,
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

\NewDocumentCommand{\mfig}{om}{%
\pgfkeys{/jotagah/myfig/.cd,
fig width=0cm,
fig height=0.05,
#1}%%
\begin{marginfigure}[\myfigwidth]
\Figure[trim={.0\width} {\myfgheight\height}%
{.0\width} {.0\height},clip,%
width=\linewidth,keepaspectratio=true,%
caption=\mycaption,label=\mylabel,center,here]{#2}
\end{marginfigure}%

-
@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 '14 at 23:12