I have a number of commands with the same structure as this one and I'm wondering whether or not the code can be compacted to avoid having to type the E_{K
and K_{\symup{trans}
twice each. I don't see a way to do it without creating problems with breaking the grouping.
My MWE:
% !TEX TS-program = lualatexmk
% !TEX encoding = UTF-8 Unicode
\documentclass{article}
% xparse is now automatically loaded
\NewDocumentCommand{\translationalkineticenergy}{ s d[] }{%
% d[] must be used because of the way consecutive optional
% arguments are handled. See xparse docs for details.
\IfBooleanTF{#1}%
{% We have a *.
\IfValueTF{#2}%
{%
E_{K{\symup{,}#2}}%
}%
{%
E_{K}%
}%
}%
{% We don't have a *.
\IfValueTF{#2}%
{%
K_{\symup{trans}{\textnormal{,}#2}}%
}%
{%
K_{\symup{trans}}%
}%
}%
}%
\begin{document}
\( \translationalkineticenergy \quad \translationalkineticenergy[\symup{final}]
\quad \translationalkineticenergy* \quad \translationalkineticenergy*[\symup{final}] \)
\end{document}
UPDATE: While I was waiting I got it down to this new MWE.
% !TEX TS-program = lualatexmk
% !TEX encoding = UTF-8 Unicode
\documentclass{article}
% xparse is now automatically loaded
\NewDocumentCommand{\translationalkineticenergy}{ s d[] }{%
% d[] must be used because of the way consecutive optional
% arguments are handled. See xparse docs for details.
\IfBooleanTF{#1}%
{% We have a *.
E_{K{\IfValueT{#2}{\symup{,}#2}}}
}%
{% We don't have a *.
K_{\symup{trans}\IfValueT{#2}{\symup{,}#2}}
}%
}%
\begin{document}
\( \translationalkineticenergy \quad \translationalkineticenergy[\symup{final}]
\quad \translationalkineticenergy* \quad \translationalkineticenergy*[\symup{final}] \)
\end{document}
\mathrm
would be better (and a lot more efficient) than\textnormal
unicode-math
so I will likely use\symup
. However, tex.stackexchange.com/a/362512/218142 has influenced me.\text...
commands but\math...
or\symb...
are usually more suitable.