Math & Science ⇒ bad alignment in align environment
bad alignment in align environment
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.
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.

bad alignment in align environment
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}
- Stefan Kottwitz
- Site Admin
- Posts: 10348
- Joined: Mon Mar 10, 2008 9:44 pm
bad alignment in align environment
Hi Thomas,
here's a solution with \mathclap of the mathtools package:
Stefan
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}
LaTeX.org admin
Re: bad alignment in align environment
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.
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
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):
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
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.
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
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}
Re: bad alignment in align environment
thank you so much frabjous.
It works perfect and my text looks much better now.
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
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
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10