# Equation not aligning properly

I have to write the following equation

I have prepared the MWE, but it is not aligning properly,

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{mathtools, nccmath}
\usepackage{array}
\begin{document}
$$\begin{array}{rl} L_{Y_{1} X_{1}} L_{Y_{0} X_{0}}\left(\left|I_{1}\right\rangle\right) & =L_{Y_{1} X_{1}} L_{Y_{0} X_{0}}\left(\frac{1}{2^{n}} \sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}\left|P_{y x}\right\rangle\left|y^{\prime}\right\rangle\left|x^{\prime}\right\rangle\right)\\ & =\frac{1}{2^{n}}(\sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}\left|P_{yx}\right\rangle\left|y^{\prime}\right\rangle\left|x^{\prime}\right\rangle+U_{Y X}\left(\left|P_{Y_{0} X_{0}}\right|\right)\left|Y_{0} X_{0}\right\rangle+U_{Y X}\left(\left|P_{Y_{1} X_{1}}\right\rangle\right)\left|Y_{1} X_{1}\right\rangle \\ & =\frac{1}{2^{n}}(\begin{array}{c} \sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}\left|P_{y x}\right\rangle\left|y^{\prime}\right\rangle\left|x^{\prime}\right\rangle+\left|p_{Y_{0} X_{0}}^{2} p_{Y_{0} X_{0}}^{3^{\prime}} \cdots p_{Y_{0} X_{0}}^{0^{\prime}} p_{Y_{0} X_{0}}^{1^{\prime}}\right\rangle\left|Y_{0} X_{0}\right\rangle \\ y x \neq Y_{0} X_{0}, Y_{1} X_{1} \end{array}\\ \qquad+\left|p_{Y_{1} X_{1}}^{2^{\prime}} p_{Y_{1} X_{1}}^{3^{\prime}} \cdots p_{Y_{1} X_{1}}^{0^{\prime}} p_{Y_{1} X_{1}}^{1^{\prime}}\right\rangle\left|Y_{1} X_{1}\right\rangle \end{array})$$
\end{document}


Kindly help me.

• there is no need to write 2^{\prime} it is simpler to write 2' and it produces identical output. Aug 14, 2021 at 14:50

I came up with a similar solution as Enevevet, but the spacing is better.

1. I use @{} in the array to suppress intercolumn spacing.
2. I add {} before the + in the last equation to get a proper binary operator spacing on both sides.
3. I put \displaystyle in the array columns to get a consistent layout.
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{mathtools, nccmath}
\usepackage{array}
\begin{document}
\begin{align*}
L_{Y_{1} X_{1}} L_{Y_{0} X_{0}}\left(\left|I_{1}\right\rangle\right) &
=L_{Y_{1} X_{1}} L_{Y_{0} X_{0}}\left(\frac{1}{2^{n}} \sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}
\left|P_{yx}\right\rangle\left|y'\right\rangle\left|x'\right\rangle\right)\\
& =\frac{1}{2^{n}}\left(
\underset{y x \neq Y_{0} X_{0}, Y_{1} X_{1}}
{\sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}\left|P_{yx}\right\rangle\left|y'\right\rangle\left|x'\right\rangle}
+U_{Y X}\left(\left|P_{Y_{0} X_{0}}\right|\right)\left|Y_{0} X_{0}\right\rangle+U_{Y X}\left(\left|P_{Y_{1}X_{1}}
\right\rangle\right)\left|Y_{1} X_{1}\right\rangle\right) \\
& =\frac{1}{2^{n}}\left(\begin{array}{@{}>{\displaystyle}r@{}>{\displaystyle}l@{}}
\underset{y x \neq Y_{0} X_{0}, Y_{1} X_{1}}
{\sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}\left|P_{y x}\right\rangle\left|y'\right\rangle\left|x'\right\rangle} &
{}+\left|p_{Y_{0} X_{0}}^{2'} p_{Y_{0} X_{0}}^{3'} \cdots p_{Y_{0} X_{0}}^{0'} p_{Y_{0} X_{0}}^{1'}\right\rangle\left|Y_{0} X_{0}\right\rangle \\
& {}+\left|p_{Y_{1} X_{1}}^{2'} p_{Y_{1} X_{1}}^{3'} \cdots p_{Y_{1} X_{1}}^{0'} p_{Y_{1} X_{1}}^{1'}\right\rangle\left|Y_{1} X_{1}\right\rangle
\end{array}\right)\\
\end{align*}
\end{document}


• Very very good. :-) Aug 14, 2021 at 15:56

Still another possibility: I nested the equation in a fleqn environment (from nccmath) and used the \DeclarePairedDelimiter command from mathtools to define an \abs and a \ket commands, which make the code more readable. Last, I loaded geometry to have more decent margins, that the equation could fit in:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{nccmath,amssymb}
\usepackage{mathtools}
\DeclarePairedDelimiter\abs{\lvert}{\rvert}
\DeclarePairedDelimiter\ket\lvert\rangle

\begin{document}

\begin{fleqn}[\parindent]
\begin{aligned} & L_{Y_1 X_1} L_{Y_0 X_0}\bigl(\ket{I_1}\bigr)= L_{Y_1 X_1} L_{Y_0 X_0}\left(\frac{1}{2ⁿ} ∑_{y=0}^{2ⁿ-1} ∑_{x=0}^{2ⁿ-1}\ket{P_{y x}} \ket{y'}\ket{x'}\right)\\ & =\frac{1}{2ⁿ}\Biggl(∑_{y=0}^{2ⁿ-1}\smashoperator{∑_{\substack{x=0\\[0.5ex]y x ≠ Y_0 X_0, Y_1 X_1}}^{2ⁿ-1}} % \ket{P_{yx}}\ket{y'}\ket{x'}+U_{Y X}\bigl(\abs{P_{Y_0 X_0}}\bigr)\ket{Y_0 X_0}+ U_{Y X}\bigl(\ket{P_{Y_1 X_1}}\bigr)\ket{Y_1 X_1}\Biggr) \\ & =\begin{aligned}[t]\frac{1}{2ⁿ}\Biggl( ∑_{y=0}^{2ⁿ-1} \smashoperator{∑_{\substack{x=0\\y x ≠ Y_0 X_0, Y_1 X_1}}^{2ⁿ-1}}\ket{P_{y x}}\ket{y'}\ket{x'}& +\ket[\big]{p_{Y_0 X_0}^2 p_{Y_0 X_0}^{3' }⋯ p_{Y_0 X_0}^{0'} p_{Y_0 X_0}^{1'}}\ket{Y_0 X_0} \\[-4ex] & +\ket[\big]{p_{Y_1 X_1}^{2'} p_{Y_1 X_1}^{3'} ⋯ p_{Y_1 X_1}^{0'} p_{Y_1 X_1}^{1'}}\ket{Y_1 X_1} \Biggr) \end{aligned} \end{aligned}
\end{fleqn}

\end{document}


For the last equation I would use multlined. To my opinion the structure of equations with it is more logical. Since equations is quit wide, the \MoveEqLeft defined in the mathtools package is employed. It also load amsmath, so it is not need to load it separately.

\documentclass{article}
\usepackage{geometry}
\usepackage{amssymb}
\usepackage{mathtools}

%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\begin{align*}
\MoveEqLeft
L_{Y_{1} X_{1}} L_{Y_{0} X_{0}}\left(\left|I_{1}\right\rangle\right)    \\
& = L_{Y_{1} X_{1}} L_{Y_{0} X_{0}}\left(\frac{1}{2^{n}}
\sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}
\left|P_{yx}\right\rangle\left|y'\right\rangle\left|x'\right\rangle\right)\\
& = \frac{1}{2^{n}}\left(
\underset{y x \neq Y_{0} X_{0}, Y_{1} X_{1}}
{\sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}\left|P_{yx}\right\rangle\left|y'\right\rangle\left|x'\right\rangle}
+ U_{Y X}\left(\left|P_{Y_{0} X_{0}}\right|\right)\left|Y_{0} X_{0}\right\rangle
+ U_{Y X}\left(\left|P_{Y_{1}X_{1}}
\right\rangle\right)\left|Y_{1} X_{1}\right\rangle\right) \\
& = \frac{1}{2^{n}}\left(%
\begin{multlined}
\underset{y x \neq Y_{0} X_{0}, Y_{1} X_{1}}
{\sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}\left|P_{y x}\right\rangle\left|y'\right\rangle\left|x'\right\rangle} \\
+ \left|p_{Y_{0} X_{0}}^{2'} p_{Y_{0} X_{0}}^{3'} \cdots p_{Y_{0} X_{0}}^{0'} p_{Y_{0} X_{0}}^{1'}\right\rangle\left|Y_{0} X_{0}\right\rangle
+ \left|p_{Y_{1} X_{1}}^{2'} p_{Y_{1} X_{1}}^{3'} \cdots p_{Y_{1} X_{1}}^{0'} p_{Y_{1} X_{1}}^{1'}\right\rangle\left|Y_{1} X_{1}\right\rangle
\end{multlined}\right)
\end{align*}
\end{document}


(red lines shows text area borders)

I would use an align environment then an array environment. For the subscript below the two sums, I used \underset since the subscript is common to the two sums. Finally, I got:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{mathtools, nccmath}
\usepackage{array}
\begin{document}
\begin{align*}
L_{Y_{1} X_{1}} L_{Y_{0} X_{0}}\left(\left|I_{1}\right\rangle\right) & =L_{Y_{1} X_{1}} L_{Y_{0} X_{0}}\left(\frac{1}{2^{n}} \sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}\left|P_{y x}\right\rangle\left|y^{\prime}\right\rangle\left|x^{\prime}\right\rangle\right)\\
& =\frac{1}{2^{n}}\left(\underset{y x \neq Y_{0} X_{0}, Y_{1} X_{1}}{\displaystyle\sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}}\left|P_{yx}\right\rangle\left|y^{\prime}\right\rangle\left|x^{\prime}\right\rangle+U_{Y X}\left(\left|P_{Y_{0} X_{0}}\right|\right)\left|Y_{0} X_{0}\right\rangle+U_{Y X}\left(\left|P_{Y_{1} X_{1}}\right\rangle\right)\left|Y_{1} X_{1}\right\rangle\right) \\
& =\frac{1}{2^{n}}\left(\begin{array}{rl}
\underset{y x \neq Y_{0} X_{0}, Y_{1} X_{1}}{\displaystyle\sum_{y=0}^{2^{n}-1} \sum_{x=0}^{2^{n}-1}}\left|P_{y x}\right\rangle\left|y^{\prime}\right\rangle\left|x^{\prime}\right\rangle &+\left|p_{Y_{0} X_{0}}^{2} p_{Y_{0} X_{0}}^{3^{\prime}} \cdots p_{Y_{0} X_{0}}^{0^{\prime}} p_{Y_{0} X_{0}}^{1^{\prime}}\right\rangle\left|Y_{0} X_{0}\right\rangle \\
&+\left|p_{Y_{1} X_{1}}^{2^{\prime}} p_{Y_{1} X_{1}}^{3^{\prime}} \cdots p_{Y_{1} X_{1}}^{0^{\prime}} p_{Y_{1} X_{1}}^{1^{\prime}}\right\rangle\left|Y_{1} X_{1}\right\rangle
\end{array}\right)
\end{align*}
\end{document}


And here's how it looks:

NB : You forgot a \prime after a subscript 2 on the third line of your code compared to your picture.

• You should look for \substack Aug 14, 2021 at 17:15