# Automatically use long arrows in display mode

I'd like to have the command \to produce the arrow from \longrightarrow, but only when in a display mode, making use of the available space. More precisely, the output of

So we have a map $A \to B$.


should be left unchanged, but

So we have a map
$A \to B.$


should produce the same as

So we have a map
$A \longrightarrow B.$


If possible, I'd like the same to happen to \mapsto getting replaced by \longmapsto when in display mode.

I'm not entirely sure if it is wise to do this (perhaps something breaks?), but you certainly can do this by redefining \to using \mathchoice:

\documentclass{article}
\renewcommand\to{\mathchoice{\longrightarrow}{\rightarrow}{\rightarrow}{\rightarrow}}
\begin{document}

So we have a map $A \to B$ and we have a map
$C \to D.$

\end{document}


Here is the output:

I have made it so that \to expands to \longrightarrow in display mode and otherwise to \rightarrow for text, script and scriptscript styles - in fontmath.ltx the \to command is defined as \let\to\rightarrow.

Have a look at The mysteries of \mathpalette for a description of the \mathchoice and \mathpalette commands.

• I'd use \rightarrow instead of \realto, no need for the \let. – egreg May 1 at 10:40
• Although doing the same for \mapsto would require a \let-construction, would it not? – SvanN May 1 at 11:24