25

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

wrong alignment

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!

I've already tried with

\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.

2
  • 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
    Commented Jul 28, 2015 at 17:40
  • 1
    \buildrel is not a supported command in LaTeX. Use \overset (that needs amsmath, which you use anyway).
    – egreg
    Commented Jul 28, 2015 at 17:52

6 Answers 6

29

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}

Corresponding output

10

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:

enter image description here

\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.

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

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) \\
     &= h(x) &&\qquad\text{(something)}
\end{alignat*}
\end{document}

enter image description here

2
  • Note that t achieve the "better way" you can also utilise the tagcommand. Commented May 2, 2019 at 13:47
  • With tag command, it will be needed the dollar sign to use it with math equations inside the tag command. I'm not sure whether I would like to use tag command. I would like that everything goes to the closest to the right margin possible, like with tag, but with a more practical use, like in the egreg ideas. Commented Sep 8, 2022 at 22:04
1

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}

enter image description here

1

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}

enter image description here

1

The answer by @Marcel is the right way to do this, but since that is a new package, and may not be available everywhere, I'm currently using hspace to move the other relational symbols to be aligned with the o

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .