Math & Sciencebad alignment in align environment

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
thomas
Posts: 7
Joined: Thu Jan 06, 2011 8:23 pm

bad alignment in align environment

Post by thomas »

Hello,

I have in my text many long equations and I write sometimes short comments above equal signs.

Example:

\begin{align*}
a&=b+c\\
&\overset{c=d+e}{=}b+d+e
\end{align*}

In those cases the two equal signs are not on top of each other but the upper equal sign is aligned with the comment above the lower equal sign. This looks really bad.

It is possible to align the upper equal sign with the lower equal sign?? So the two equal lines should be exactly on top of each other.

I tried to manage it with parboxes but unfortunately without success.

Thanks in advance.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

bad alignment in align environment

Post by frabjous »

Here's a rather hackish attempt at it. I can think of some pitfalls in certain scenarios, but maybe it's a start.

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\newlength{\alignoverwidth}
\newlength{\eqwidth}
\settowidth{\eqwidth}{$=$}
\newcommand{\alignovereq}[1]{%
            \settowidth{\alignoverwidth}{\ensuremath{\overset{#1}{=}}}%
            \mathrel{\makebox[\widthof{=}]{\ensuremath{\overset{#1}{=}}}}%
            \hspace*{0.5\alignoverwidth}\hspace*{-0.5\eqwidth}}
\begin{document}

\begin{align*}
a&=b+c\\
&\alignovereq{c=d+e} b+d+e
\end{align*}
\end{document}
align.png
align.png (2.37 KiB) Viewed 6637 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

bad alignment in align environment

Post by Stefan Kottwitz »

Hi Thomas,

here's a solution with \mathclap of the mathtools package:

Code: Select all

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{align*}
a&=b+c\\
&\overset{\mathclap{c=d+e}}{=}b+d+e
\end{align*}
\end{document}
Stefan
LaTeX.org admin
thomas
Posts: 7
Joined: Thu Jan 06, 2011 8:23 pm

Re: bad alignment in align environment

Post by thomas »

Thanks you two. I think Stefans solution is smarter and easier to handle.

But there arises a problem. In the example

\documentclass{article} %I use scrreprt
\usepackage{mathtools}
\begin{document}
\begin{align*}
a&=b+c\\
&\overset{\mathclap{c=d+e}}{=}b+d+e
\end{align*}
\end{document}

the end of the comment c=d+e over the second equal sign and the first letter of the text in this line (that is b) overlap each other i.e. the distance between the equal sign and the beginning of the text is too small. Of course I could insert a distance manually. But I have many of those equations.

Is there a possibility to insert a constant distance in all those cases? I think it would be nice if the distance between the end of the comment and the beginning of the text would be the same like the distance between the equal sign and the text in an equation without any comment over the equal sign.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

bad alignment in align environment

Post by frabjous »

The majority of the code in the definition I have you was designed to solve that problem.

Here's one that mixes the two approaches (although I think the results are identical to what I had before):

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\newlength{\alignoverwidth}
\newlength{\eqwidth}
\settowidth{\eqwidth}{$=$}
\newcommand{\alignovereq}[1]{%
            \settowidth{\alignoverwidth}{\ensuremath{\overset{#1}{=}}}%
            \overset{\mathclap{#1}}{=}%
            \hspace*{0.5\alignoverwidth}\hspace*{-0.5\eqwidth}}
\begin{document}

\begin{align*}
a&=b+c\\
&\alignovereq{c=d+e} b+d+e
\end{align*}
\end{document}
thomas
Posts: 7
Joined: Thu Jan 06, 2011 8:23 pm

Re: bad alignment in align environment

Post by thomas »

Sorry frabjous, I read the fact that you also use the mathclap command over.

Your attempt works very well. Thanks a lot.

But if I have the comment in the first line, e.g.

\begin{align*}
a+b&\overset{b=c-a}{=}a+c-a
&=c
\end{align*}

I have the problem with the distance between the equal sign and the text on the left hand side of the equation,i.e. in the example above the comment overlaps the b on the left hand side of the equation.
How can I manage that I get the same distance between the end oft the text and the comment on both sides?

Of course I tried already to modify your solution but unfortunately without success.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

bad alignment in align environment

Post by frabjous »

Yeah, I did worry about that kind of thing. Here's a modification that may work. Notice that the definition of \alignovereq now includes the '&' separator, so you don't want to reuse that in the actual align* environment; using this command takes care of it:

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\newlength{\alignoverwidth}
\newlength{\eqwidth}
\settowidth{\eqwidth}{$=$}
\newcommand{\alignovereq}[1]{%
            \settowidth{\alignoverwidth}%
                     {\ensuremath{\overset{#1}{=}}}%
            \hspace*{0.5\alignoverwidth}\hspace*{-0.5\eqwidth}&%
            \settowidth{\alignoverwidth}%
                     {\ensuremath{\overset{#1}{=}}}%
            \overset{\mathclap{#1}}{=}%
           \hspace*{0.5\alignoverwidth}\hspace*{-0.5\eqwidth}}
\begin{document}

\begin{align*}
a+b \alignovereq{b=c-a} a+c-a\\
&=c
\end{align*}
\end{document}
thomas
Posts: 7
Joined: Thu Jan 06, 2011 8:23 pm

Re: bad alignment in align environment

Post by thomas »

thank you so much frabjous.

It works perfect and my text looks much better now.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

bad alignment in align environment

Post by localghost »

Now that the problem is solved, please be so kind and mark the topic (not the last post) accordingly as clearly written in the Board Rules (to be read before posting). Please keep that in mind for the future so that further reminders will not be necessary.


Thorsten
Post Reply