# \overset and align environment: how to get correct alignment?

I'm using the align environment together with the \overset command to put some text over a relation symbol. Here is my MWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{amsmath}

\begin{document}
\begin{align*}
f(x)&=g(x)\\
&\overset{something}{=} h(x)
\end{align*}
\end{document}


And the output is

As you can see the alignment is wrong, or at least is not what I want! I would like the two equal signs to be aligned one to the other, while with my code I get the alignment of the first equal sign with the start of the text over the symbol!

\overset{something}{&=} h(x)


and

\overset{something}&{=} h(x)


but both solutions led to an error.

How can I get the correct alignment?

Edit: the original question was about \buildrel, but, as @egreg said in a comment, this command is not supported in LaTeX, so I've substituted it with \overset.

• You should really reconsider writing longer text above a = like that. IMO it does not belong there in print. It can be fine on a black board. But does not belong in print. – daleif Jul 28 '15 at 17:40
• \buildrel is not a supported command in LaTeX. Use \overset (that needs amsmath, which you use anyway). – egreg Jul 28 '15 at 17:52

I would stack something on top of a relation using \stackrel{<stack>}{<relation>} (of something similar like \overset) and use mathtools's \mathclap. Space corrections can be accommodated using an appropriate \hspace:

\documentclass{article}
\usepackage{mathtools}

\begin{document}
\begin{align*}
f(x) &= g(x) \\
&\buildrel{something}\over{=} h(x)
\end{align*}

\begin{align*}
f(x) &= g(x) \\
&\stackrel{\mathclap{\text{something}}}{=} \hspace*{1.5em} h(x) \\
&\overset{\mathclap{\text{something}}}{=} \hspace*{1.5em} h(x)
\end{align*}

\end{document}


However, avoid lengthy phrases on symbols. Instead you could define a new symbol to represent the relation.

• \overset, not \stackrel – egreg Jul 28 '15 at 17:53
• @egreg: nice trick! May I ask you why you used \hspace* instead of simply \hspace? – Rackbox Jul 29 '15 at 13:23
• May be &\overset{\text{\llap{somethin}g}}{=} h(x)? – Manuel Jul 29 '15 at 15:27

You shouldn't be using \buildrel, which is not a supported command of LaTeX, but \overset.

You can get alignment using a phantom, but in my opinion it's better to add a side condition:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{amsmath}

\begin{document}
Possible way
\begin{align*}
f(x)&\overset{\hphantom{\text{something}}}{=}g(x)\\
&\overset{\text{something}}{=} h(x)
\end{align*}

Better way
\begin{alignat*}{2}
f(x) &= g(x) \\
\end{alignat*}
\end{document}


• Note that t achieve the "better way" you can also utilise the tagcommand. – Viktor Glombik May 2 at 13:47

The aligned-overset package now provides an easy solution for this: Just write

\documentclass{article}
...
\usepackage{aligned-overset}
\begin{document}
\begin{align*}
f(x)&=g(x)\\
\overset{something}&{=} h(x)
\end{align*}
\end{document}


Here is a version using \stackon. The overset gap is set to 2pt using the optional argument. The text has been set in \scriptsize.

\documentclass{article}
%\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}
%\usepackage[italian]{babel}
\usepackage{amsmath,stackengine}
\stackMath
\begin{document}
\begin{align*}
f(x)&\stackon{=}{\phantom{\text{\scriptsize something}}}g(x)\\
&\stackon[2pt]{=}{\text{\scriptsize something}} h(x)
\end{align*}
\end{document}


Another solution which use the length of the box containing \buildrel{something}\over{=} for negative shift

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{amsmath}

\newlength{\mylength}
\settowidth{\mylength}{$\buildrel{something}\over{=}$}

\begin{document}
\begin{align*}
f(x)&=g(x)\\
&=\kern-0.7\mylength{\buildrel{something}\over{\phantom{=}}} h(x)
\end{align*}
\end{document}