[Tex/LaTex] way to make a symbol behave as either mathord or mathrel depending on context

math-modespacingsymbols

Symbols denoting equivalence relations are typically used in two ways. They are either binary relation symbols denoting that two elements are equivalent or they appear as ordinary symbols in the standard notation for quotients of sets by equivalence relations. In the latter case LaTeX needs to be told explicitly that we want to treat the equivalence symbol as an ordinary symbol.

The code

\documentclass{minimal}

\begin{document}

  Good spacing: $x \sim y$.

  Bad spacing: $X / \sim$.

  Good spacing: $X / \mathord\sim$.

\end{document}

produces

I wonder whether it is possible for \sim to automatically convert to \mathord class in such situations. I think it would be enough to make it so that \sim is \mathrel unless it immediately follows / in which case it should be \mathord.

Best Answer

No, a relation symbol will behave as such in all contexts and there is no “look back to see what came before me”. You could do a look forward for / to see whether what follows is a relation symbol and, in this case, treat it as an ordinary one. However this method has several limitations, because it would always require / (in math mode) to be followed by something still belonging to math mode.

The specific problem, quite common when dealing with equivalence relations, can be solved in a different way:

\newcommand{\quotient}[2]{#1/{#2}}

so that the call

$\quotient{X}{\sim}$

will do what you want and will also provide a good markup for possible improvement of the typesetting (a different slash, some spacing, whatever). The trick is bracing #2, so it will be a subformula and become an ordinary atom.

Here is a possible implementation of the “look forward”, which assumes that / will always be followed by something that should be considered as an ordinary atom:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\qslash}[1]{\normalslash{#1}}
\mathchardef\normalslash=\mathcode`/
\begingroup\lccode`~=`/ \lowercase{\endgroup\let~}\qslash
\AtBeginDocument{\mathcode`/=\string"8000 }

\begin{document}

$X/\sim$

\end{document}

enter image description here

I don't recommend it.

Related Question