13

Look at the following example

\begin{align}
     & x \\
     \overset{\text{some long explanation}}= & foo \\
     & = bar
\end{align}

what is the best way to get the equal signs aligned? Now I have seen different solution such as adding \overset{\hphantom{\ţext{some long explanation}}}= to the beginning of every line, but I don't really think that is the best solution. Any better ideas?


UPDATE

I guess what i am actually after is

  1. aligning the first column of the align environment right
  2. aligning the equal sign under the overset text right
3
  • 1
    Please correct your current code snippet, since I assume you're after some horizontal alignment. You just mention "align" but provide a horizontally set align. At the moment it's not entire clear what you're after.
    – Werner
    Jan 7, 2012 at 15:50
  • yes. I guess what you mean where the missing \\s?
    – niklasfi
    Jan 7, 2012 at 16:18
  • If you modify \mathclap in Herbert's answer to \mathllap, you get something more in line with what you're after. If this is still not the case, could you include an ASCII art (or some other drawing/image) of the exact alignment you're after?
    – Werner
    Jan 7, 2012 at 17:09

4 Answers 4

13
\documentclass{article}
\usepackage{mathtools}
\begin{document}

\begin{align}
  x & \\
    &\overset{\mathclap{\strut\text{some long explanation}}}= foo\\
    & = bar
\end{align}

\end{document}
4
  • 3
    this leads to some long text floating into the formula after the equal sign
    – niklasfi
    Jan 7, 2012 at 16:39
  • 1
    I find \strut to be unrelated with the problem, plus it creates some unwanted vertical space. \mathclap alone seems to be sufficient to solve the issue.
    – Raphael
    Oct 10, 2013 at 14:40
  • You only want to ignore the left overlapping part of the some long text for the align environment while properly aligning the following text foo according to standard math-alignment rules. This has not been answered here.
    – Jost
    Oct 17, 2013 at 14:06
  • Is there any way to get this working without importing mathtools? The latter kind of destroys my over-/underbraces, so that is not really an option. Oct 17, 2019 at 19:42
7

you say you don't want to repeat "some long explanation", but there really isn't any better way to do it that i know of.

is this what you're after?

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
 x &\overset{\text{some long explanation}}= foo \\
   &\overset{\phantom{\text{some long explanation}}}= bar
\end{align}
\end{document}
1
  • Thanks, this works for me, but it is not very neat.
    – quapka
    Apr 22, 2014 at 12:26
1

How about the following \alignedoverset macro, which preserves the spacing and aligns the left edge of the second argument?

\documentclass{article}
\usepackage{amsmath, calc}
\newlength\oversetwidth
\newlength\underwidth
\newcommand\alignedoverset[2]{
  % #1 = over
  % #2 = under
  \settowidth\oversetwidth{$\overset{#1}{#2}$}
  \settowidth\underwidth{$#2$}
  \setlength\oversetwidth{\oversetwidth-\underwidth}
  \hspace{.5\oversetwidth}
  &
  \settowidth\oversetwidth{$\overset{#1}{#2}$}
  \settowidth\underwidth{$#2$}
  \setlength\oversetwidth{\oversetwidth-\underwidth}
  \hspace{-.5\oversetwidth}
  \overset{#1}{#2}
}

\begin{document}
\begin{align}
  x + 2 \alignedoverset{\text{some long explanation}}{\geq} foo \\
  & = bar
\end{align}
\end{document}

enter image description here

This seems to break if the second argument is longer than the first, but in that case it shouldn't be necessary at all.

0

Depending on how much hacky you want to be, you can use the not so dirty trick in the first solution, the very dirty trick in the second solution.

I'd definitely prefer the third solution.

\documentclass{article}
\usepackage{amsmath}

% for the first solution
% see http://tex.stackexchange.com/a/209732/4427
\makeatletter
\newcommand{\Cen}[2]{%
  \ifmeasuring@
    #2%
  \else
    \makebox[\ifcase\expandafter #1\maxcolumn@widths\fi]{$\displaystyle{}#2$}%
  \fi
}
\makeatother

% for the second solution
\newcommand{\CEN}[1]{%
  \multispan{1}\hfill$\displaystyle{}#1$\hfill
}

\begin{document}

\begin{alignat}{2}
x & \Cen{2}{{}\overset{\text{some long explanation}}={}} && foo \\
  & \Cen{2}{{}={}}                                       && bar
\end{alignat}

\begin{alignat}{2}
x & \CEN{\overset{\text{some long explanation}}=} && foo \\
  & \CEN{=}                                       && bar
\end{alignat}

\begin{alignat}{2}
x &= foo &\qquad& \text{(some long explanation)} \\
  &= bar
\end{alignat}

\end{document}

enter image description here

You must log in to answer this question.

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