# Writing mod in congruence problems without leading space

I have been writing

$a \equiv r (\mod n)$


but this puts a space between the ( and mod. For example, r ( mod n ) instead of r (mod n). So, how do you write r (\mod n) without that space? What would be the correct way to write $a \equiv r (\mod n)$?

The spacing issue is an obvious indication that it's not meant to be used that way. However, you could wing your own, depending on the sophistication you're after:

\documentclass{article}

\usepackage{amsmath}
\newcommand{\Mod}[1]{\ (\mathrm{mod}\ #1)}

\begin{document}

\begin{align*}
a &\equiv r \mod n \\
a &\equiv r \pmod{n} \\
a &\equiv r \Mod{n}
\end{align*}

\end{document}

• This is great! I like how \Mod{n} looks. The only problem I'm encountering with \Mod{n} is that the "mod" becomes italicized in theorem/lemma/example/proof/etc. environments. – Mathemanic Aug 31 '14 at 19:12
• @Mathemanic: You can try using \textup (or perhaps \mbox) instead of \text. – Werner Aug 31 '14 at 19:38
• This should be \mathrm{mod}. – egreg Mar 9 '17 at 13:23
• @egreg why is it \mathrm over \operatorname? – It'sNotALie. Nov 3 '19 at 22:59
• @It'sNotALie. Because this is not an operator. – egreg Nov 3 '19 at 23:27

I think $a \equiv r \;(\bmod\; n)$ does what you want (as mentioned by jfbu in the comment).

Both the latex kernel and amsmath provide the command \pmod. In the latex kernel \pmod provides a fixed amount of space before (mod ...). The amsmath version is slightly more sophisticated, using a smaller amount of space when not in a display. So one way is to define a new command that temporarily switches off the display mode:

\usepackage{amsmath}

\makeatletter
\newcommand{\tpmod}[1]{{\@displayfalse\pmod{#1}}}
\makeatother


\documentclass{article}

\usepackage{amsmath}

\makeatletter
\newcommand{\tpmod}[1]{{\@displayfalse\pmod{#1}}}
\makeatother

\begin{document}

Text (no change): $$a\tpmod{b}$$ vs.\ $$c\pmod{d}$$.

Display:
\begin{gather*}
a\tpmod{b} \\
\intertext{vs.}
c\pmod{d}.
\end{gather*}

\end{document}