# How can I align twice a math equation

The code is:

\documentclass[12pt]{article}
\usepackage{fontspec}
\usepackage{graphicx} % Required for inserting images
\usepackage{extarrows}
\usepackage{listings}
\usepackage{wasysym, amsmath, amsfonts, amssymb, mathtools, mathrsfs}
\usepackage{tikz}
\begin{document}
\begin{aligned}
\begin{equation*}
n=p\cdot m&=\\
&=(x_1^2+x_2^2+x_3^2+x_4^2)(y_1^2+y_2^2+y_3^2+y_4^2)&&=(x_1y_1-x_2y_2-x_3y_3-x_4y_4)^2 \\
&+(x_1y_2+x_2y_1+x_3y_4-x_4y_3)^2\\
&+(x_1y_3-x_2y_4+x_3y_1+x_4y_2)^2\\
&+(x_1y_4+x_2y_3-x_3y_2+x_4y_1)^2\\
&=x^2+y^2+z^2+w^2: x,y,z,w\in\mathbb{Z} \square
\end{equation*}
\end{aligned}
\end{document}


The output is

But I want the output to be:
Meaning I want the first two = to be aligned and I want to align the four sums as in the bottom picture.
Please help me, I don't know how to "double align", so I just thought the double && will help me(clearly it didn't). Thank you!

• As an aside, I find the presentation of the mathematics to be inscrutable. I would start a new line with the second = (well, I guess third, no idea why you have two equals signs at the beginning) and then break up the rest of the equation. May 15 at 23:01
• Two further asides: (i) mathtools loads amsmath automatically, and amssymb loads amsfonts automatically; hence, \usepackage{wasysym, amsmath, amsfonts, amssymb, mathtools, mathrsfs} may be simplified to \usepackage{mathtools, amssymb, wasysym, mathrsfs}. (ii) Since you appear to be using either LuaLaTeX or XeLaTeX, I'd load the unicode-math package (which loads fontspec automatically, hence no need to load fontspec explicitly), after loading mathtools and the other math-related packages.
– Mico
May 16 at 2:12

I have used the enviroment alignat. I have deleted some useless packages. You see a short complete compilable code. For the \square to the end is ugly. I suggest to use amsthm package where there is the predefinite \qedsymbol.

\documentclass[12pt]{article}
\usepackage{amsmath,amssymb}
\begin{document}

\begin{alignat*}{2}
n &= p \cdot m && \\
&= (x_1^2 + x_2^2 + x_3^2 + x_4^2)(y_1^2 + y_2^2 + y_3^2 + y_4^2) &&= (x_1y_1 - x_2y_2 - x_3y_3 - x_4y_4)^2 \\
&&&+ (x_1y_2 + x_2y_1 + x_3y_4 - x_4y_3)^2 \\
&&&+ (x_1y_3 - x_2y_4 + x_3y_1 + x_4y_2)^2 \\
&&&+ (x_1y_4 + x_2y_3 - x_3y_2 + x_4y_1)^2 \\
&= x^2 + y^2 + z^2 + w^2: x, y, z, w \in \mathbb{Z}.
\end{alignat*}

\end{document}


• +1. :-) A very minor nit to pick: Assuming that the colon symbol (":") in the final row means "given that" or "conditional on", would the OP's use of : be more appropriate than \colon?
– Mico
May 16 at 1:53
• The colon means "such that" and you are right, it is a very minor nit to pick:) BTW, I switched my code a bit and instead of the last & I put &&& just so it'd look better. But you did save me a lot of time, thank you. May 16 at 4:24
• @MIco I thank you for your precious words. Now edit following your instructions. May 16 at 6:56

For the sake of variety, here's a solution that employs nested aligned environments.

% !TEX TS-program = lualatex   %% or 'xelatex'
\documentclass[12pt]{article}
\usepackage[letterpaper,margin=1in]{geometry} % set page parameters as needed
\usepackage{mathtools} % 'mathtools' loads 'amsmath' automatically
\usepackage{amsthm} % for '\qedhere' macro
\usepackage{unicode-math} % for '\symbb' macro (replacement for '\mathbb')

\begin{document}

\begin{proof}
\dots
\begin{aligned} n &=p\cdot m\\ &=(x_1^2+x_2^2+x_3^2+x_4^2) (y_1^2+y_2^2+y_3^2+y_4^2) \begin{aligned}[t] {}={}&(x_1y_1-x_2y_2-x_3y_3-x_4y_4)^2\\ {}+{}&(x_1y_2+x_2y_1+x_3y_4-x_4y_3)^2\\ {}+{}&(x_1y_3-x_2y_4+x_3y_1+x_4y_2)^2\\ {}+{}&(x_1y_4+x_2y_3-x_3y_2+x_4y_1)^2 \end{aligned}\\ &=x^2+y^2+z^2+w^2: x,y,z,w\in\symbb{Z} \,. \qedhere \end{aligned}
\end{proof}

\end{document}

• \qedhere is misplaced May 16 at 9:25

You can use alignedat, but the result is very unbalanced.

Note that the final clause after the colon should be text after the display.

\documentclass[12pt]{article}
\usepackage{amsmath,amssymb,amsthm}

\begin{document}

\begin{proof}
Given the sums of squares $p=x_1^2+x_2^2+x_3^2+x_4^2$ and $m=y_1^2+y_2^2+y_3^2+y_4^2$,
we can verify that $n=pm$ is a sum of squares as well, because
\begin{equation*}
\begin{aligned}
n&=pm \\[0.5ex]
&=(x_1^2+x_2^2+x_3^2+x_4^2)(y_1^2+y_2^2+y_3^2+y_4^2) \\[0.5ex]
&=(x_1y_1-x_2y_2-x_3y_3-x_4y_4)^2 \\
&+(x_1y_2+x_2y_1+x_3y_4-x_4y_3)^2\\
&+(x_1y_3-x_2y_4+x_3y_1+x_4y_2)^2\\
&+(x_1y_4+x_2y_3-x_3y_2+x_4y_1)^2\\[0.5ex]
&=x^2+y^2+z^2+w^2
\end{aligned}
\end{equation*}
where $x,y,z,w\in\mathbb{Z}$.
\end{proof}

\begin{proof}
Given the sums of squares $p=x_1^2+x_2^2+x_3^2+x_4^2$ and $m=y_1^2+y_2^2+y_3^2+y_4^2$,
we can verify that $n=pm$ is a sum of squares as well, because
\begin{equation*}
\begin{alignedat}{2}
n&=pm \\
&=(x_1^2+x_2^2+x_3^2+x_4^2)(y_1^2+y_2^2+y_3^2+y_4^2)
&&=(x_1y_1-x_2y_2-x_3y_3-x_4y_4)^2 \\
&&&+(x_1y_2+x_2y_1+x_3y_4-x_4y_3)^2\\
&&&+(x_1y_3-x_2y_4+x_3y_1+x_4y_2)^2\\
&&&+(x_1y_4+x_2y_3-x_3y_2+x_4y_1)^2\\
&=x^2+y^2+z^2+w^2
\end{alignedat}
\end{equation*}
where $x,y,z,w\in\mathbb{Z}$.
\end{proof}

\end{document}