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}
\mathrmwould be better (and a lot more efficient) than\textnormal– David Carlisle Oct 31 '20 at 10:52unicode-mathso I will likely use\symup. However, https://tex.stackexchange.com/a/362512/218142 has influenced me. – LaTeXereXeTaL Oct 31 '20 at 13:06\text...commands but\math...or\symb...are usually more suitable. – David Carlisle Oct 31 '20 at 14:41