8

From my experience, most control sequences function as an implicit group, for example g^\textrm{th}. However, if we look at the example below, the first line throws an error while the second line does not.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$g^\mod$
$g^{\mod}$

\end{document}

I first noticed this because I used DeclareMathOperator to declare my own operators, based on the answer to Define additional math operators to be typeset in roman.

I then tried with \mod which is defined in amsmath

What is the reason that math operators do not play well with ^, unlike other control sequences, and is there an option to make them do so without explicit {}?

0

1 Answer 1

13

Actually, most control sequences do not function as an implicit group; you have simply been lucky with your choice of superscripts. \textrm is a font macro, and has been carefully designed as such to work without braces in a superscript for just this purpose. If you were to define your own macro

\newcommand*\timestwo[1]{2*#1}

you would find that $g^\timestwo{x}$ would be accepted, but produce the equivalent of ${g^2} * 2$. This is because TeX will expand "arguments" to an exponent ^ but then take the first group (more or less) for the actual text of the exponent. (Hence, \textrm works by expanding in such a way as to make a group around its argument the first thing that ^ eventually sees.)

Something similar happens with \mod, which in fact expects an argument: writing \show\mod at the command line would say

\mod=\long macro:
#1->\allowbreak \if@display \mkern 18mu \else \mkern 12mu\fi {\operator@font mod}\,\,#1.

That is, you're supposed to use it as \mod n. In fact, this one will give you an additional error because the first typesettable material seen by ^ is the \mkern, and since it's not expandable it both a) does not absorb its "argument" (either 18mu or 12mu depending on context) and b) is taken on its own as an exponent. And the expression \mkern without an argument is invalid.

If you just want the upright-typeset operator called "mod", you can \DeclareMathOperator{\Mod}{mod} and that will be usable in a superscript, because it is a single token taking no arguments.

You must log in to answer this question.

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