Math & ScienceAligning items in a broken equation

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Aligning items in a broken equation

Post by Cham »

I need to align some terms in an equation which is splited on two lines, like the compilable example below.

Currently, I'm using the \phantom command to align the last terms (on the second line) to the minus sign of the first line. This is a "hack". What is the proper way to do this ?

Code: Select all

\documentclass[12pt,letterpaper]{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\begin{document}
\noindent
Bla bla bla bla :
	\begin{align*}
		A &= B + C - D \\ \\
		%phantom
		&\phantom{= B + C \;}
		%phantom
		 + D - E \\ \\
		&= F + G - H.
	\end{align*}
\end{document}
Here's a preview :
align.jpg
align.jpg (9.4 KiB) Viewed 4664 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
User avatar
Stefan Kottwitz
Site Admin
Posts: 10308
Joined: Mon Mar 10, 2008 9:44 pm

Aligning items in a broken equation

Post by Stefan Kottwitz »

Hi Cham,

good MWE! The proper way is to use alignat, such as:

Code: Select all

\begin{alignat*}{2}
  A &= B + C &&- D \\
    &        &&+ D - E \\
    &= F + G &&- H.
\end{alignat*}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Aligning items in a broken equation

Post by Cham »

Ahaa !

Thanks for the fast answer, Stefan !

This should solve my problem. :D
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Aligning items in a broken equation

Post by Cham »

Hmm, no, there's a small problem with this.

The third line shouldn't have its items aligned with the lines above it. Only its "=" sign should be aligned with the equal sign of the first line.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10308
Joined: Mon Mar 10, 2008 9:44 pm

Aligning items in a broken equation

Post by Stefan Kottwitz »

Make another hack. ;-) Just joking.

Code: Select all

\documentclass[12pt,letterpaper]{article}
\usepackage[T1]{fontenc}
\usepackage{mathtools}
\begin{document}
\begin{alignat*}{2}
  A &= B + C &&- D \\
    &        &&+ D - E \\
    &= \mathrlap{F + G - H}.
\end{alignat*}
\end{document}
Sometimes, tweaking is necessary. However, problems with the commonly used environments may also be a hint that the desired formatting might be a bit unusual.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Aligning items in a broken equation

Post by Cham »

The \mathrlap command appears to be the solution. This one was really unexpected !
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Aligning items in a broken equation

Post by Cham »

Stefan_K wrote: Sometimes, tweaking is necessary. However, problems with the commonly used environments may also be a hint that the desired formatting might be a bit unusual.
Well, the actual equation is too long to fit on a single line. This is why I splited the first equation on two lines. However, there's a second medium-long equation on the third line and I need it to be aligned with the equal sign of the first equation, like the example above.

So if this formatting is a bit unusual, I wonder how should I format both equations.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10308
Joined: Mon Mar 10, 2008 9:44 pm

Aligning items in a broken equation

Post by Stefan Kottwitz »

A proper way would be using blocks, also provided by amsmath.

Code: Select all

\begin{align*}
  A &= B + C
    \begin{aligned}[t]
      &- D \\
      &+ D - E
    \end{aligned} \\
    &= F + G - H.
\end{align*}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Aligning items in a broken equation

Post by Cham »

I prefer the previous solution. With the last solution, I'm getting an extra space around an aligned "+", because of the invisible \right. delimiter. I don't understand why it's doing this.
Post Reply