I want to define a set of macros, with similar behaviour but applied to different symbols. To avoid repetitions, I thought it would be a good idea to generate them programmatically from a template.
I have a bunch of quantities
- with general indices (
\idx
) applied to them, - but I occasionally need variations of this index (e.g., for defining recurrence relations);
- or to scrap the automatic index altogether and replace it with something else.
I thought of using a O{\idx}
argument type, but I do not like this solution, as I would like to especially restate \idx
for 2.
. It seems like a star argument could be useful here.
This is why I came up with:
\documentclass[12pt]{article}
\usepackage{amsmath}
\NewDocumentCommand\idx{}{j}
\NewDocumentCommand\pmatstar{mm}{#1_{#2}}
\NewDocumentCommand\pmatnostar{mm}{#1_{\idx#2}}
\NewDocumentCommand\matnum{smO{}}{
\IfBooleanTF{#1}{
\pmatstar{#2}{#3}
}{
\pmatnostar{#2}{#3}
}
}
%%%%%
% What I do not know how to do
%
% \NewDocumentCommand\specmatnum{mm}{...}
% \spectmatnum{mlow}{L}
% \spectmatnum{mupp}{U}
% ...
%
%%%%%
%%%%%
% This is what I want to avoid
%
% \NewDocumentCommand\mlow{sO{}}{
% \IfBooleanTF{#1}{
% \pmatstar{L}{#2}
% }{
% \pmatnostar{L}{#2}
% }
% }
% \NewDocumentCommand\mupp{sO{}}{
% \IfBooleanTF{#1}{
% \pmatstar{U}{#2}
% }{
% \pmatnostar{U}{#2}
% }
% }
% ...
%
%%%%%
\begin{document}
\begin{align*}
\text{Default: } &\matnum{L}\\
\text{No star, modified default: } &\matnum{L}[+1]\\
\text{Star, no optional argument: } &\matnum*{L}\\
\text{Star, optional '2': } &\matnum*{L}[2]
\end{align*}
%%%%%
% How I would like to be able to replicate the above example without calling \matnum directly
%
% \mlow
% \mlow[+1]
% \mlow*
% \mlow*[2]
%
%%%%%
\end{document}
My question being, how not to duplicate the definition of \matnum
for all my symbols? The main difficulty for me being the passing of the *
, without rewriting the \IfBooleanTF{}{}{}
; which nullifies the point of having \matnum
in the first place. I think the rest is straightforward.