Question
Is it possible to get and store a count of the optional arguments actually passed to an xparse
declare argument/environment?
Purpose
Regardless of the number of arguments provided (arbitrary depending on situation), I want to do something very specific with the last one given. Because this is a variable number, I need to retrieve the count of arguments given and use that combined with #
to do something with that variable.
Code
\documentclass{article}
\usepackage{fontspec}
\usepackage{xparse}
\DeclareDocumentEnvironment{myenv}{O{}O{}O{}O{}O{}O{}O{}O{}O{}O{}} % 10 optional args
{startcode}
{endcode}
\DeclareDocumentCommand{\mycom}{O{}O{}O{}O{}O{}O{}O{}O{}O{}O{}}{% 10 optional args
}%
\begin{document}
\mycom[a][b][c] % Count 3
% Do something special with #3
\begin{myenv}[a][b][c][d] % Count 4
% Do something special with #4
\end{myenv}
\end{document}
\IfValueTF{#9}{9 arguments}{\IfValueTF{#8}{8 arguments}{..}}
would work. By the way, why not declare the last one asm
mandatory, and the eight before aso
?\foo[a][b][c][d]{e}
.\foo[a,b,c,d,e,f]
expl3 then makes it easy to count the list, iterate over it, select the last etc.[][][]
is the wrong input syntax for that (irrespective of coding and efficiency issues)