What are the differences between \stackrel and \stackbin? In what situations is it better to use one or the other?
3 Answers
Don't use \stackrel or \stackbin, they are obsolete. Use instead \underset and \overset from amsmath as they automatically space things correctly (whether it's a binary relation or a binary operator):

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$f(x) \overset{\text{def}}{=} x \ln(1+x)$
$f(x) \underset{x \to 0}{=} x^2 + o(x^2)$
$A \underset{\text{below}}{\overset{\text{above}}{+}} C$
\end{document}
- 28,978
-
Can you please add a reference which clearly shows that and why \stack* is obsolete? – ManuelSchneid3r Feb 05 '21 at 11:51
-
2@ManuelSchneid3r: you're not going to find a better reason than the one I gave in my post (manual spacing vs automatic spacing). I should also have mentioned
\overundersetthat puts something both above and below the symbol (and would have simplified my third example). – Philippe Goutet Feb 08 '21 at 09:09
Without the stackrel package, \stackrel is defined in ltxmath.dtx as
\def\stackrel#1#2{\mathrel{\mathop{#2}\limits^{#1}}}
which typesets a relational operator with a top limit (effectively placing it on top). Heiko Oberdiek's stackrel package is said to provide an
Enhancement to the
\stackrelcommand.
This "enhancement" provides an optional argument to \stackrel for placing something below the relational operator (using a similar process as the original \stackrel). Additionally, it provides a counterpart for binary relations called \stackbin. The difference between the two (or when to use which one) is contained within the post What is the difference between \mathbin vs. \mathrel? Here is a similar take on the use of stackrel.

\documentclass{article}
\usepackage{stackrel}
\begin{document}
\begin{tabular}{clc}
\multicolumn{3}{c}{Relations} \\[5pt]
\LaTeX & Typeset & width \\
\hline
\verb|$x=x$| & $x=x$ & \setbox0=\hbox{$x=x$} \the\wd0 \\
\verb|$x\stackbin[c]{a}{=}x$| & $x\stackbin[c]{a}{=}x$ &
\setbox0=\hbox{$x\stackbin[c]{a}{=}x$} \the\wd0 \\
\verb|$x\stackrel[c]{a}{=}x$| & $x\stackrel[c]{a}{=}x$ &
\setbox0=\hbox{$x\stackrel[c]{a}{=}x$} \the\wd0 \\[10pt]
\multicolumn{3}{c}{Binary operators} \\[5pt]
\LaTeX & Typeset & width \\
\hline
\verb|$x+x$| & $x+x$ & \setbox0=\hbox{$x+x$} \the\wd0 \\
\verb|$x\stackbin[c]{a}{+}x$| & $x\stackbin[c]{a}{+}x$ &
\setbox0=\hbox{$x\stackbin[c]{a}{+}x$} \the\wd0 \\
\verb|$x\stackrel[c]{a}{+}x$| & $x\stackrel[c]{a}{+}x$ &
\setbox0=\hbox{$x\stackrel[c]{a}{+}x$} \the\wd0
\end{tabular}
\end{document}
Note the equivalent spacing using \stackrel with =, while similar spacing is returned using \stackbin and +. In essence, use \stackrel for relational operators, and \stackbin for binary operators.
-
why do you use
$x=x$ & \setbox0=\hbox{$x=x$} \the\wd0instead of\setbox0=\hbox{$x=x$} \usebox0 & \the\wd0? – user2987828 Mar 02 '17 at 12:54 -
@user2987828: No particular reason. The storing to a box and measuring occurs in the same cell. – Werner Mar 02 '17 at 16:18
I personally prefer using \mathop, because it's simpler. Instead of using nested under/oversets, you could simply write:
$A \mathop{+}_{\text{below}}^{\text{above}} C$
- 929
-
4That works if you wrap it in \displaystyle{} ; otherwise (inline mode) it behaves like \Sum etc. and puts the text on the right as superscript / subscript. – Frentos Mar 14 '18 at 19:09
-
1You can use $A \mathop{+}\limits_{\text{below}}^{\text{above}} C$ in inline mode. – VicaYang May 26 '21 at 08:14
\mathbinand\mathrelwhich can be found in what is the difference betweenmathbinvs.mathrel– percusse Dec 24 '11 at 03:36\stackrel(or\stackbin) choice, then you can use\usepackage{amsmath,stackrel}$a\stackrel{\text{\makebox[0pt]{abc}}}{=}b$. This typesetsabcin text mode in the appropriate font size without any width, and would therefore not influence math spacing.\stackreland\stackbintypically only have narrow entries on top of/below them. Is this what you might be after? – Werner Dec 24 '11 at 04:05arrayortabularthat is vertically centered, or use\stackrel. Either way, this seems to be a departure from your current question which relates specifically to\stackreland\stackbin. – Werner Dec 24 '11 at 04:45