I defined a command using \NewDocumentCommand
with specifiers {O{strict} m}
. I expected #1
to default to strict when calling the command with only a parameter enclosed in {}
, and to pick up all of the text in {}
for #2
. What is happening instead is that the \strictn
command is receiving only the first character of the second parameter unless I specify it with double braces, e.g., \strictn[*]{{star}}
.
A secondary problem is that the test of a trailing hyphen in #1
(the optional parameter) is failing.
\documentclass{article}
\usepackage{mathtools}
\usepackage{xparse}
\ExplSyntaxOn
% Render underscored with some variation of strict
\NewDocumentCommand{\strictn}{O{strict} m}
{
\str_if_eq:nnTF {\str_item:nn {#1} {-1}} {-}
{
\show_strict:nn {{#1}strict} {#2}
}
{
\show_strict:nn {#1} #2
}
}
\cs_new:Npn \show_strict:nn #1 #2
{
\msg_term:n {show_strict \ P1 \ = \ #1}
\msg_term:n {show_strict \ P2 \ = \ #2}
\str_if_eq:nnTF {#1} {*}
{\underset{\mathrm {(semi-strict,strict)}{#2}}}
{\underset{\mathrm {{#1}}{#2}}}
}
\ExplSyntaxOff
\begin{document}
\begin{enumerate}
\item Test $\strictn{default}$
\item Test $\strictn[semi-]{hyphen}$
\item Test $\strictn[*]{star}$
\end{enumerate}
\end{document}
I didn't initially give the intended appearance because I was asking about the tokenization rather than about the rendering. For the curious, it depends on the optional parameter. In all cases the macro is used in math mode and only the text set under the mandatory is upright.
- Optional parameter missing: underset with (semi-strict,strict)
- Optional parameter ends in hyphen (-): underset with #1 concatenated with strict
- Optional parameter present and doesn't end with hyphen: underset with #1
\show_strict:nn {#1} #2