# Errors in arguments when renewing \mod with \mathchoice

Currently I am using the \mod command that comes with amsmath, and while I'm in general satisfied with it, I didn't like the way it handles spacing in text style.

I tried to renew the \mod command as follows:

\usepackage{amsmath}
\makeatletter
\renewcommand{\mod}[1]{\mathchoice
{\allowbreak \if@display \mkern 18mu\else \mkern 5mu\fi
{\operator@font mod} \mkern 6mu #1}
{\allowbreak \if@display \mkern 18mu\else \mkern 5mu\fi
{\operator@font mod} \mkern 6mu #1}
{\mkern4mu {\operator@font mod} \mkern 6mu #1}
{\mkern4mu {\operator@font mod} \mkern 6mu #1}
}
\makeatother


Now I get all kinds of errors if I type \mod \mathfrak p, and in order to avoid these errors I have to type \mod {\mathfrak p}. However, before I renewed the command, it was fine to type \mod \mathfrak p and this produced no errors. Is there a way I could fix this, or should I just stick to typing \mod {\mathfrak p}?

• you are aware that amsmath provide four \mod macros? Jul 16 '15 at 8:14
• The correct syntax should be \mod{\mathfrak{p}} anyway. Notice that the \allowbreak you have in your definition will do nothing, because it sits inside a subformula; put it before \mathchoice. Jul 16 '15 at 8:43

To mimic the amsmath definition you should move the argument outside the \mathchoice. You can move the 6mu spacing and the typing of the word mod too this is the same in all cases. As @egreg points out, the \allowbreak should be moved out of the \mathchoice; inside it has no effect.

\documentclass{article}

\usepackage{amsmath,amssymb}
\makeatletter
\renewcommand{\mod}[1]{\allowbreak
\mathchoice
{\if@display \mkern 18mu\else \mkern 5mu\fi}
{\if@display \mkern 18mu\else \mkern 5mu\fi}
{\mkern4mu}
{\mkern4mu} {\operator@font mod} \mkern 6mu #1
}
\makeatother

\begin{document}

$$5 \mod \mathfrak f$$

\begin{equation*}
5 \mod \mathfrak f
\end{equation*}

\end{document}


In a way it is lucky it works as #1 is just \mathfrak here; add \tracingmacros=1 to see this. The way the command is operating the argument is necessary at all, you could just define

\makeatletter
\renewcommand{\mod}{\allowbreak
\mathchoice
{\if@display \mkern 18mu\else \mkern 5mu\fi}
{\if@display \mkern 18mu\else \mkern 5mu\fi}
{\mkern4mu}
{\mkern4mu} {\operator@font mod} \mkern 6mu
}
\makeatother

• \allowbreak should be before \mathchoice, because it has no effect otherwise. Jul 16 '15 at 8:46

There's no need for \mathchoice here, just of some arithmetic; however, I'd emphasize that the correct syntax for \mod and \mathfrak is

a\equiv b \mod{\mathfrak{p}}


It's true that with the standard definition \mod \mathfrak p seems to work, but it's just that: it seems to work.

TeX provides \nonscript to specify math glue or kern that's not added in subscripts or superscripts:

\documentclass{article}
\usepackage{amsmath,amssymb}

\textwidth=.5\textwidth % just not to waste space

\begin{document}

\section*{Standard}

$a\equiv b\mod{\mathfrak{p}} + X_{a\equiv b\mod{\mathfrak{p}}}$
$a\equiv b\mod{\mathfrak{p}} + X_{a\equiv b\mod{\mathfrak{p}}}$

\section*{Modified}

\makeatletter
\renewcommand{\mod}[1]{%
\allowbreak
\nonscript\if@display \mkern 14mu \else \mkern 1mu \fi
\mkern 4mu
{\operator@font mod}
\mkern 6mu
#1
}
\makeatother

$a\equiv b\mod{\mathfrak{p}} + X_{a\equiv b\mod{\mathfrak{p}}}$
$a\equiv b\mod{\mathfrak{p}} + X_{a\equiv b\mod{\mathfrak{p}}}$

\end{document}


• Thank you! Your answer is very instructive, and I'll correct my bad syntax habits with respect to leaving out pairs of { and }. Since I'm still learning LaTeX, I have a question: you wrote that \mod \mathfrak p only seems to work; could you clarify what goes wrong when one writes this? Jul 16 '15 at 19:43
• @justin When you write that, the argument to \mod is \mathfrak, It's just by chance that the implementation of \mod makes it work the same: you were exactly bitten by the fact that your implementation doesn't make it work. When a macro taking an argument is not followed by {, it takes as argument the first token it finds, otherwise it takes everything up to the matching }. Jul 16 '15 at 19:46