Wondering if you can accomplish all of these variations in LaTeX by defining a custom command:

\mycmd{tag}{color}{keyval}
\mycmd{tag}{keyval}
\mycmd{keyval}


So it could be used like this:

\mycmd{foo}{black}{size=1cm}
\mycmd{foo}{color=black,size=1cm}
\mycmd{tag=foo,color=black,size=1cm}


Either one would work. How to define the command so this sort of thing would work?

\newcommand\mycmd[3][...]

• Noticed the packages I mentioned yesterday: xparse can do this, see the g specifier Mar 23, 2018 at 12:27
• while technically possible, you should not do this in latex. LaTeX has clear syntactic guidelines that {} delimited arguments should be mandatory, if you want optional arguments as here, then they should be [] delimited. Mar 23, 2018 at 12:59

You can implement more or less any syntax in tex, but latex has clear guidelines that optional arguments should be [] delimited. \newcommand only defines commands with at most one option, so to have two you would need to define it in two steps but xparse allows it to be declared directly as having two options so for example the following (complete) example test document:

\documentclass{article}

\usepackage{xparse}
\NewDocumentCommand\mycmd{O{deftag}O{red}m}{\typeout{tag=#1,color=#2,#3}}
\begin{document}

\mycmd[foo][black]{size=1cm}
\mycmd[foo]{color=black,size=1cm}
\mycmd{tag=foo,color=black,size=1cm}

\end{document}


which produces

tag=foo,color=black,size=1cm
tag=foo,color=red,color=black,size=1cm
tag=deftag,color=red,tag=foo,color=black,size=1cm

• Wondering, does this output actually mean it is a key-value object that can be iterated over like in a property list, or is size=1cm just a string. Mar 24, 2018 at 4:07
• @LancePollard it means whatever you define \mycmd to make it mean. To TeX there are no strings or objects or anything else lor=red,t is just a list of 9 character tokens. Mar 24, 2018 at 8:14