[Tex/LaTex] xparse processor with multiple arguments

macrosxparse

I would like to define a function with a fixed (three) number of optional arguments followed by a variable number of mandatory comma separated arguments. Each one of the mandatory argument has to produce a text in a colored box (the three optional arguments defines the color of the boxes).

Following the advices of this thread, I have written this function:

\NewDocumentCommand{\boxed}{O{white}O{gray}O{white}>{\SplitList{,}}m}{%
  \ProcessList{#4}{\func}}
\NewDocumentCommand{\func}{m}{%
  \fcolorbox{white}{gray}{\color{white}#1}\space}

The above function produces the desired boxes, but ignore the optional arguments, because I haven't found a way to pass more than one argument to func.

After some researches, I have found this workaround:

\NewDocumentCommand{\boxed}{O{white}O{gray}O{white}>{\SplitList{,}}m}{%
  \def\@opta{#1}%
  \def\@optb{#2}%
  \def\@optc{#3}%
  \ProcessList{#4}{\func}}
\NewDocumentCommand{\func}{m}{%
  \fcolorbox{\@optc}{\@optb}{\color{\@opta}#1}\space}

The above function does what I want to, but I can't believe there is no easier way to accomplish the task without those ugly def. Am I wrong?

Best Answer

The \ProcessList functionality is experimental and suggests that the macro takes a single argument. One way around this is to define \func within \boxed so it grabs the arguments:

enter image description here

\documentclass{article}

\usepackage{xparse,xcolor}

\NewDocumentCommand{\boxed}{ O{white} O{gray} O{white} >{\SplitList{,}}m }{{%
  \NewDocumentCommand{\func}{ m }{%
    \fcolorbox{#3}{#2}{\color{#1}\strut##1}\space}
  \ProcessList{#4}{\func}}}

\begin{document}

\boxed{a,b,c}

\boxed[yellow]{d,e,f}

\boxed[yellow][black]{g,h,i}

\boxed[yellow][black][red]{j,k,l}

\end{document}
Related Question