0

I have defined a macro which, for the purposes of demonstration, I have stripped down to the following:

\newcommand{\sub}[1]{\mathchoice{_{#1}}{}{}{}}

However, with this macro the subscript no longer plays nicely with superscripts. That is to say, depending on the order in which the two are applied, one of them does not align to the right edge of the base symbol. Consider, for example, x\sub{i}^{2} and x^{2}\sub{i}:

Subscripts & superscripts do not align nicely.

I'd like to get the canonical behavior where both subscript and superscript are basically flush up against the x:

enter image description here

5
  • 1
    Welcome to TeX.SE.
    – Mico
    Sep 28 '20 at 18:19
  • 1
    \newcommand{\sub}[1]{_{\mathchoice{}{}{\scriptstyle#1}{\scriptscriptstyle#1}}} ? Sep 28 '20 at 21:47
  • @DavidCarlisle Thanks! Great suggestion — a simple solution that hadn't crossed my mind. The only thing I dislike about it is that it cannot detect the external context (not a TeX guru so forgive me if this is not the right terminology). What I mean is that, if used (for example) in a nested subscript, mathchoice will not be able to detect this. This is a bit of a fringe use case, however, and I don't think it would be a deal breaker to have to live with it.
    – corvus
    Sep 29 '20 at 1:17
  • @corvus in a top level script the style will be script so the thirds branch taken, in a nested script the style will be scriptscript so the 4th branch taken Sep 29 '20 at 7:39
  • It's hard to understand what this is for. With your macro, the subscript only appears when \sub is used in display math mode, with David's definition, it will appear exactly as if you type _{i} instead of \sub{i}, so what's the idea behind all this?
    – egreg
    Sep 29 '20 at 10:46
1

You should move the _ out of the mathchoice.

\newcommand{\sub}[1]{_{\mathchoice{}{}{\scriptstyle#1}{\scriptscriptstyle#1}}}

I left the first two branches empty as they will not be taken as the style inside the _{...} will be scriptstyle or scriptscriptstyle depending on the nesting level, never display or text style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.