You have to make an additional comparison on those arguments specified as []
, which technically differ from \NoValue
. And, you can't just leave them out, since subsequent optional arguments would then be used out-of-sequence. You can use the ifmtarg
package:
\documentclass{minimal}
\usepackage{xparse}% http://ctan.org/pkg/xparse
\usepackage{ifmtarg}% http://ctan.org/pkg/ifmtarg
\makeatletter
\DeclareDocumentCommand{\mycommand}{ O{mydefault} m o o o }{%
p:#2%
\IfNoValueTF{#3}%
{}%
{\@ifmtarg{#3}{}{ p:#3}}%
\IfNoValueTF{#4}%
{}%
{\@ifmtarg{#4}{}{ p:#4}}%
\IfNoValueTF{#5}%
{}%
{\@ifmtarg{#5}{}{ p:#5}}
p:#1
}
\makeatother
\begin{document}
\mycommand[one]{two} \par % p:two p:one
\mycommand[one]{two}[three] \par % p:two p:three p:one
\mycommand[one]{two}[three][four] \par % p:two p:three p:four p:one
\mycommand[one]{two}[three][four][five] \par % p:two p:three p:four p:five p:one
\mycommand[one]{two}[three][][five] \par % p:two p:three p:five p:one
\mycommand[one]{two}[][][five] % p:two p:five p:one
\end{document}
\newcommand{\iitthesis@thesisdatafield}[2]{%
\@namedef{iitthesis@#1}{#2}}
With
\iitthesis@thesisdatafield{authorEnglish}{Name of Author}
you'd define \iitthesis@authorEnglish
to expand to "Name of Author", that is, you'd have issued the equivalent of
\def\iitthesis@authorEnglish{Name of Author}
This wouldn't check for the defined command to be previously undefined. If you want also this check, do
\newcommand{\iitthesis@thesisdatafield}[2]{%
\expandafter\@ifdefinable\csname iitthesis@#1\endcsname
{\@namedef{iitthesis@#1}{#2}}}
but for internal commands this isn't usually done.
In your motivation I don't see any need of defining the new command with an argument. If you need also to define a user level command, you can do with the same technique:
\newcommand{\iitthesis@thesisdatafield}[1]{%
\long\@namedef{#1}##1{\@namedef{iitthesis@#1}{##1}}}
In this case saying
\iitthesis@thesisdatafield{authorEnglish}
would define the command \authorEnglish
so that if the user types
\authorEnglish{A. U. Thor}
the effect would be as if doing
\def\iitthesis@authorEnglish{A. U. Thor}
The \long
prefix to \@namedef
causes \long\def
to be executed, so the argument can span one or more paragraphs.
This technique is employed by the LaTeX kernel, where \author{A. U. Thor}
actually defines \@author
expanding eventually to "A. U. Thor".
Best Answer
You can define recursive macros that call themselves until some termination is found; a solution to your problem can be as follows:
The macro
\twoimages
really has no argument; it simply starts the business by opening thefigure
environment and calls the real command,\@twoimagesi
. This command checks whether it's followed by\stopimages
; if it is, it calls\@twoimagesend
that ends the environment; otherwise it executes\@twoimagesii
that has the job of printing a row, after having absorbed its six arguments. It does so in an indirect way, so that we can write only once the sameminipage
, using the first three arguments and the last three.Then
\@twoimagesii
restarts the recursion by calling\@twoimagesi
again.Let's see a complete example (I use the
demo
option tographicx
that draws a black blob instead of requiring external files).A quite different approach is via the
expl3
macros. Also the syntax will be different; I'll present the example, which is almost self-explanatory: the main macro splits its argument at the commas and each chunk is processed in a very similar way as before.The result is exactly the same as before. There can be any number of sixtuples of arguments, separated by a comma.
Just for completeness, a version that accepts any number of images; now each triple should be separated by a comma; if the number of images is odd, the last one will be centered.