# Passing arguments from a parent function to an auxiliary/child function in xparse

Motivation

I'm working on a custom \Colour command/environment. I'd like to have two mandatory arguments, one taking comma-separated parameters and the second taking the body. My approach for the comma-separated part uses xparse's \SplitArgument. Is it possible to get the parent function to pass an argument to its auxiliary/child function?

Current version

\documentclass{article}
\usepackage{
xparse,
amsmath,
xcolor
}

\ExplSyntaxOn
\NewDocumentCommand{\ColourAux}{m m m m}{
\newcommand{\Coloured}{\color{#1}#4}
\tl_if_empty:nTF { #2 } { \newcommand{\Body}{\begin{aligned}\Coloured\end{aligned}} }{
\str_case:nnF { #2 } {
{ t }{ \newcommand{\Body}{\text{\Coloured}} }
{ i }{ \newcommand{\Body}{\textit{\Coloured}} }
{ b }{ \newcommand{\Body}{\textbf{\Coloured}} }
{ ib }{ \newcommand{\Body}{\textit{\textbf{\Coloured}}} }
{ bi }{ \newcommand{\Body}{\textit{\textbf{\Coloured}}} }
} {  }
}
\tl_if_empty:nTF { #3 } { \Body } { \parbox{#3}{\Body} }
}
\ExplSyntaxOff
\NewDocumentCommand{\Colour}{>{\SplitArgument{3}{,}}m}{\ColourAux#1}

\begin{document}
\begin{align*}
& \Colour{red,ib,4cm,the quick brown fox jumps over the lazy dog} \\ % works as desired
% & \Colour{red,ib,4cm}{the quick brown fox jumps over the lazy dog} \\ % desired syntax
& \Colour{red,,,\frac{1}{\sqrt{a^2+b^2}}} \\ % works as desired
% & \Colour{red}{\frac{1}{\sqrt{a^2+b^2}}} % desired syntax
\end{align*}
\end{document}


Edit 1: with thanks to Steven B. Segletes, is it possible to do this with expl3/xparse?

Edit 2: in response to egreg's comment, I've found and implemented a suggestion from an answer by Joseph Wright to use \newcommand to store variables.

• Start with avoiding \def at all costs. Apr 28, 2021 at 21:47
• @egreg I had no idea. Why, and what should I use instead?
– mjc
Apr 28, 2021 at 21:49

Since I don't speak Expl3, I will leave the revision of \ColourAux to others. But here, I show how \Colour can be revised to achieve the desired syntax.

\documentclass{article}
\usepackage{
amsmath,
xcolor
}

\ExplSyntaxOn
\NewDocumentCommand{\ColourAux}{m m m m}{
\def\Coloured{\color{#1}#4}
\tl_if_empty:nTF { #2 } { \def\Body{\begin{aligned}\Coloured\end{aligned}} }{
\str_case:nnF{#2}{
{ t }{ \def\Body{\text{\Coloured}} }
{ i }{ \def\Body{\textit{\Coloured}} }
{ b }{ \def\Body{\textbf{\Coloured}} }
{ ib }{ \def\Body{\textit{\textbf{\Coloured}}} }
{ bi }{ \def\Body{\textit{\textbf{\Coloured}}} }
}{}
}
\tl_if_empty:nTF { #3 } { \Body } { \parbox{#3}{\Body} }
}
\ExplSyntaxOff
\usepackage{listofitems}
\newcommand\Colour[2]{%