1

I attempted to redefine certain math relations using \mathchoice so that they don't need to be grouped when used as subscripts and superscripts just like how they behaved originally. However, the MWE below results in an error.

\documentclass{article}

\renewcommand{\sim}{\mathchoice{\mathrel{a}}{\mathrel{b}}{\mathrel{c}}{\mathrel{d}}}

\begin{document}
$X_\sim$
\end{document}

I recognized that this error occurs because the \mathchoice{...} command needs to be grouped when used as a subscript. However, simply enclosing the definition with \bgroup and \egroup cancels out the \mathrel in all math styles. Is there a way to define \sim such that groupings around \sim are not required when it is used as a subscript or superscript while keeping it a math relation?

3
  • 1
    \mathchoice isn't helping here, but basically no the token needs to be a mathord group or a mathchardef token so only the latter avoids forcing mathord spacing. Commented Apr 7 at 17:38
  • note that the documented latex syntax for superscript is always to have braces Commented Apr 7 at 17:47
  • See \begingroup vs \bgroup. The former offers a certain transparency in math mode: tex.stackexchange.com/questions/1930/… Commented Apr 7 at 19:53

1 Answer 1

0

Short answer: no.

Where's the obstruction? When Knuth was developing TeX, he wanted to make it possible to type

f'(x)  f''(x)  f'''(x)

instead of the clumsier

f^{\prime}(x)  f^{\prime\prime}(x)  f^{\prime\prime\prime}(x)

whence math active characters were born and also a peculiar feature of ^ (and also of _ for symmetry):

when TeX scans ^ or _ in math mode (catcode 7 and 8 respectively), it performs macro expansion, stopping at the first unexpandable token (and ignoring space and \relax tokens along the way); if this token is { (that is, an explicit or implicit character token with catcode 1), then the whole group is used as superscript or subscript; otherwise the superscript or subscript field of the current math atom will be filled with that next token.

The ^^ feature is also valid in math mode, but it's irrelevant here.

What happens with your attempt? Yes, the first unexpandable token that's found is \mathchoice, but

X_\mathchoice

is not going to do anything useful, besides showing

! Missing { inserted.
<to be read again>
                   \mathchoice
l.6 $X_\sim
           $

If you try to circumvent the issue by doing

\renewcommand{\sim}{{\mathchoice{...}{...}{...}{...}}}

you don't get a Rel atom, because whatever is declared in the arguments to \mathchoice is annulled by the pair of outer braces.

Possible strategy: make _ of catcode 12, but math active, with a definition built along the lines of

  • check what comes next
  • if it is {, then do \sb so the braced group will be used as a subscript
  • otherwise, let <token> be the next token, remove it and call \sb{<token>}

If you're bold, you can try and implement it, but beware: it can break existing constructs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .