Math & Science ⇒ bad alignment in align environment
bad alignment in align environment
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.
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
bad alignment in align environment
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}
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
bad alignment in align environment
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}
Re: bad alignment in align environment
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.
bad alignment in align environment
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}
Re: bad alignment in align environment
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.
bad alignment in align environment
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}
Re: bad alignment in align environment
It works perfect and my text looks much better now.
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
bad alignment in align environment
Thorsten
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10