Math & ScienceGathered Alignment of Terms in Equation

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
niles
Posts: 92
Joined: Mon Dec 01, 2008 9:35 pm

Gathered Alignment of Terms in Equation

Post by niles »

Hi

I have the following MWE doing what it is supposed to do:

Code: Select all

\documentclass[a4paper]{article}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amsthm, amssymb}

\begin{document}
\begin{equation}
  \begin{aligned}
		\alpha = \beta &+ \gamma \\ %gamma and delta are supposed to be above eachother
		               &- \delta
   \end{aligned}
\end{equation}
\end{document}  
Howver now I want to add a new line and thus a new equality sign =, and this I want to be below the one after \alpha. I tried using

Code: Select all

\documentclass[a4paper]{article}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amsthm, amssymb}

\begin{document}
\begin{equation}
  \begin{aligned}
		\alpha &= \beta &+ \gamma \\ %gamma and delta are supposed to be above eachother
		       &        &- \delta \\
                       &= \Pi 
   \end{aligned}
\end{equation}
\end{document} 
but the result does not look correct. I guess I'm using the ampersands wrong, but do I need to insert some "whitespace" in order to deal with two &'s per row?

Thanks in advance.

Best,
Niles.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Gathered Alignment of Terms in Equation

Post by cgnieder »

You need to double the ampersands:

Code: Select all

\documentclass[a4paper]{article}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amsthm, amssymb}

\begin{document}

\begin{equation}
  \begin{aligned}
    \alpha &= \beta &&+ \gamma \\ %gamma and delta are supposed to be above each other
           &        &&- \delta \\
           &= \Pi
  \end{aligned}
\end{equation}

\end{document}
Alignment in math environments typically is alternating right and left

Code: Select all

\begin{aligned}
  right & left    &    right & left \\
  right & left    &    right & left
\end{aligned}
Regards
site moderator & package author
niles
Posts: 92
Joined: Mon Dec 01, 2008 9:35 pm

Re: Gathered Alignment of Terms in Equation

Post by niles »

Hi

Thanks for the reply. I tried it out, but the terms on the right part (for the two first rows) are pushed to the right. Is there a way to correct this?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Gathered Alignment of Terms in Equation

Post by cgnieder »

Maybe something like this?

Code: Select all

\documentclass[a4paper]{article}
% \usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amsthm, amssymb}

\newcommand*\phantomrel[1]{\mathrel{\phantom{#1}}}
\newcommand*\mllap[1]{\llap{$#1$}}

\begin{document}

\begin{equation}
  \begin{aligned}
    \alpha &= \phantom{\Pi}\mllap{\beta} + \gamma \\ %gamma and delta are supposed to be above eachother
           &\phantomrel{=} \phantom{\Pi} - \delta \\
           &= \Pi
  \end{aligned}
\end{equation}

\end{document}
Regards
site moderator & package author
niles
Posts: 92
Joined: Mon Dec 01, 2008 9:35 pm

Re: Gathered Alignment of Terms in Equation

Post by niles »

Thanks, it works. But I am a little surprised that LaTeX doesn't offer a more "automated" approach.

Best wisshes,
Niles.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Gathered Alignment of Terms in Equation

Post by cgnieder »

Even better (and easier):

Code: Select all

\documentclass[a4paper]{article}
% \usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amsthm, amssymb}

\newcommand*\phantomrel[1]{\mathrel{\phantom{#1}}}

\begin{document}

\begin{equation}
  \begin{aligned}
    \alpha &= \beta + \gamma \\ %gamma and delta are supposed to be above eachother
           &\phantomrel{=} \phantom{\beta} - \delta \\
           &= \Pi
  \end{aligned}
\end{equation}

\end{document}
Regards
site moderator & package author
Post Reply