3

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?

user202729
  • 7,143
Tohiko
  • 1,789
  • 2
    Sub- and superscripts should always be given in braces – Joseph Wright Aug 11 '22 at 08:45
  • I understand that's the preferred method of doing things and I tend to follow that style. But clearly examples like 2^\K and 2_\K work when \def\K{K} for example. The inconsistency is what is problematic to me. – Tohiko Aug 11 '22 at 08:48
  • 1
    I don't really see the inconsistency: even the TeX book states clearly that omitting the braces is fine only for a single character. Note that 2^\K fails also for \def\K{KK}. And having after _ or ^ a macro taking an argument has (almost) never worked. – campa Aug 11 '22 at 08:54
  • @campa. 2^\K would work as expected for \def\K{{KK}}. – Tohiko Aug 11 '22 at 08:57
  • 1
    Yes, clearly. That's why I've added an "almost" :-). It's the kind of dirty coincidence why something like x_\mathrm{...} also works. – campa Aug 11 '22 at 09:05
  • 1
    @Tohiko That's the 'relying on expansion behaviour' business: you need to know that \K expands 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 \mathrm for example didn't do that, so people learned to properly brace their input.) – Joseph Wright Aug 11 '22 at 09:13
  • 1
    The underlying reason why some works and others don't are explained in macros - Is it possible to define a command taking an optional star and working in a subscript? - TeX - LaTeX Stack Exchange and linked questions ; nevertheless you need deep understanding of how TeX works at low level to understand the answers. – user202729 Aug 11 '22 at 09:17
  • @JosephWright, point taken. Thanks for explaining. I agree that the \mathrm example 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 \mathrm work as "expected". – Tohiko Aug 11 '22 at 09:54
  • 1
    @Tohiko Try with the simple example $X_\notin$. The idea of adding braces to the definition of \notin wouldn't work, because the symbol would lose its status as relation symbol. – egreg Aug 11 '22 at 10:33

1 Answers1

7

Sub- and superscripts should always be given in braces. TeX's rules for handling _ and ^ if followed by non-braced material depend on the expansion behaviour of the tokens. This is problematic as

  • The expansion behaviour is not always obvious
  • The rule is different from argument grabbing
  • The outcome can be different if _ or ^ are active tokens rather than using TeX's catcode approach

In contrast, _{...}/^{...} always work the same way: this is the only supported syntax in LaTeX.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036