The following example
\documentclass{article}
\NewDocumentCommand{\K}{e{^}}{{\exp(\IfValueT{#1}{#1}n)}}
\begin{document}
These work ( \K, 2^{\K^{2}}, 2_{\K}, 2^{\K}, \log(\K)).
These do not ( 2_\K , 2^\K ).
\end{document}
Shows cases in which my command with embellishments (I am using TeXLive 2020). Is this expected? How do I fix the command definition so that all examples work?
2^\Kand2_\Kwork when\def\K{K}for example. The inconsistency is what is problematic to me. – Tohiko Aug 11 '22 at 08:482^\Kfails also for\def\K{KK}. And having after_or^a macro taking an argument has (almost) never worked. – campa Aug 11 '22 at 08:542^\Kwould work as expected for\def\K{{KK}}. – Tohiko Aug 11 '22 at 08:57:-). It's the kind of dirty coincidence why something likex_\mathrm{...}also works. – campa Aug 11 '22 at 09:05\Kexpands such that the first non-expandable token is{_1. You can't do that in general terms. (With hindsight, it would have been better if\mathrmfor example didn't do that, so people learned to properly brace their input.) – Joseph Wright Aug 11 '22 at 09:13\mathrmexample shouldn't have worked in the first place. I deal with collaborators who use my commands in the "natural way" and it's difficult to explain to them that there's nothing wrong with my command when other commands like\mathrmwork as "expected". – Tohiko Aug 11 '22 at 09:54$X_\notin$. The idea of adding braces to the definition of\notinwouldn't work, because the symbol would lose its status as relation symbol. – egreg Aug 11 '22 at 10:33