Using expl3
syntax, I want in my \coolgloss
command to use as an optional argument one or several formatting commands such as \small
and \itshape
.
With a command like \newcommand{\coolgloss}[3][\small\itshape]{text}{gloss}
I have no problems, but I specifically want to use expl3
in order to have a split optional which includes the negative raise
value for \rule
.
It works if I only want to pass one command name (e.g. small
) through \csname...\endcsname
, but I want support for multiple commands (e.g. \small\itshape
), and I want the arguments to be given with the backslash included.
Thus, the command must take a split optional argument, whose the second subargument (argument #2
) is dedicated to the formatting:
\coolgloss[5,\small\itshape]{text}{gloss}
But when I input it, the \small
command (not the \itshape
one) triggers a TeX capacity exceeded
error.
This has to be an issue with expansion and variable types, i.e. somewhere I should replace a n
with some other type.
MWE
\documentclass{article}
\usepackage{gb4e}
\NewDocumentCommand{\coolglossaux}{mmmm}{%
\renewcommand{\eachwordtwo}{\rule[-#1pt]{0pt}{0pt}#2}% !! #2 is the one !!
\getwords(\lineone,\eachwordone)#3 \\%
\getwords(\linetwo,\eachwordtwo)#4 \\%
\loop\lastword{\eachwordone}{\lineone}{\wordone}%
\lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
\global\setbox\gline=\hbox{\unhbox\gline
\hskip\glossglue
\vtop{\box\wordone
\nointerlineskip
\box\wordtwo
}%
}%
\testdone
\ifnotdone
\repeat
{\hskip -\glossglue}\unhbox\gline
}
\ExplSyntaxOn
\NewDocumentCommand{\coolgloss}{>{\SplitArgument{1}{,}}omm}
{
\my_xcoolgloss:nnnn #1 {#2} {#3}
}
\cs_new_protected:Nn \my_xcoolgloss:nnnn
{
\__my_xcoolgloss:eenn
{ \tl_if_novalue:nTF { #1 } { 10 } { #1 } }
{ \tl_if_novalue:nTF { #2 } {} { #2 } }
{ #3 }
{ #4 }
}
\cs_set_eq:NN \__my_xcoolgloss:nnnn \coolglossaux
\cs_generate_variant:Nn \__my_xcoolgloss:nnnn {ee}
\ExplSyntaxOff
\begin{document}
\coolgloss[5,\itshape]{I like \LaTeX}{I like \LaTeX}
%\coolgloss[5,\small\itshape]{I like \LaTeX}{I like \LaTeX} => TeX capacity exceeded
\end{document}
\small
(you get the same error with just\expanded{\small}
). You need to either protect\small
or not try and expand it.\small
is not expandable because in its definition,\selectfont
has already expanded\fontsize{}{}
.\itshape
and\small
both ultimately call\selectfont
, which is unexpandable since it calls\def
. The difference is that\itshape
is a\protected
macro while\small
is\protect
'ed.\protected
macros never expand (except with\expandafter
) while\protect
'ed macros only don't expand if they are used in certain LaTeX constructs (like\protected@edef
). It's a fairly confusing topic, not at all helped by the confusing names.