# Double alignment of equation align equal sign and plus and minus signs

Trying to align the following equation along the equal signs then the two summation terms of the first line aligned align the + and - signs. I have tried

\begin{alignat*}{2} {\rho}_{{p}^{2}: a < {p}^{2}}^{{p}^{2} \times 1} \left({N}\right) & {}={} 2\, p \left({p - 1}\right) {}+{} 2 \sum\limits_{a = 1}^{{p}^{2} - 1} \left({\left\lfloor{\frac{N + a}{{p}^{2}}}\right\rfloor + \left\lfloor{\frac{N - a}{{p}^{2}}}\right\rfloor}\right) \\ &\quad {}-{} 2 \sum\limits_{a = 1}^{p - 1} \left({\left\lfloor{\frac{N + p\, a}{{p}^{2}}}\right\rfloor + \left\lfloor{\frac{N - p\, a}{{p}^{2}}}\right\rfloor}\right) \\ & {}={} 4 \sum\limits_{a = 1}^{{p}^{2} - 1} \left\lfloor{\frac{N + a}{{p}^{2}}}\right\rfloor - 4 \sum\limits_{a = 1}^{p - 1} \left\lfloor{\frac{N + p\, a}{{p}^{2}}}\right\rfloor \end{alignat*}


Here's a solution that nests an aligned environment inside an align* environment.

I can't help make a suggestion regarding your LaTeX coding style: Please don't clutter up the code with lots of (presumably well-intentioned) pairs of curly braces. The readability (and debuggability) of the code is much improved if you use curly braces only where they are absolutely needed.

\documentclass{article}
\usepackage{mathtools}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}
\begin{document}
\begin{align*}
\rho_{p^2: a < p^2}^{p^2 \times 1} (N)
&= 2 p (p-1)
\begin{aligned}[t]
&+ 2 \sum_{a=1}^{p^2 - 1}
\biggl(\floor[\bigg]{\frac{N + a}{p^2}}
+ \floor[\bigg]{\frac{N - a}{p^2}}\biggr) \\
&- 2 \sum\limits_{a=1}^{p-1}
\biggl(\floor[\bigg]{\frac{N + p a}{p^2}}
+ \floor[\bigg]{\frac{N - p a}{p^2}}\biggr)
\end{aligned}\\
&= 4 \sum_{a=1}^{p^2-1} \floor[\bigg]{\frac{N + a}{p^2}}
- 4 \sum_{a=1}^{p-1}   \floor[\bigg]{\frac{N + p a}{p^2}}
\end{align*}
\end{document}

• The simplest one. I don't agree with p\,a – egreg Mar 12 '17 at 22:09
• @egreg - I've removed the four instances of \, and posted a new screenshot. – Mico Mar 13 '17 at 21:31

First of all, don't use $$..$$ but use $..$. Second, using aligned and balancing the second line by adding \phantom{{}= 2p(p-1)} will be easier in such a case. Finally, using too many {}s and \left .. \right makes the markup hard to follow, it seems that you used a software to translate the equation into LaTeX markup. Usually, hand-coded equations are much nicer and more readable.

\begin{aligned} \rho_{p^2: a<p^2}^{p^2\times 1}(N) &= 2p(p-1) + 2\sum_{a=1}^{p^2-1}\left({\left\lfloor{\frac{N+a}{p^2}}\right\rfloor+ \left\lfloor{\frac{N-a}{p^2}}\right\rfloor}\right)\\ &\phantom{{}= 2p(p-1)} -2\sum_{a=1}^{p-1}\left({\left\lfloor{\frac{N+p\, a}{p^2}}\right\rfloor+ \left\lfloor{\frac{N-p\,a}{p^2}}\right\rfloor}\right)\\ & = 4\sum_{a=1}^{p^2-1}\left\lfloor{\frac{N+a}{p^2}}\right\rfloor - 4\sum_{a=1}^{p-1}\left\lfloor{\frac{N+p\,a}{p^2}}\right\rfloor \end{aligned}


Another option (with the same result) is to use an outer align* for aligning the = signs and an inner aligned for aligning the + and the - operators:

\begin{align*}
\rho_{p^2: a<p^2}^{p^2\times 1}(N) &= 2p(p-1)
\begin{aligned}[t]
&+2\sum_{a=1}^{p^2-1}\left({\left\lfloor{\frac{N+a}{p^2}}\right\rfloor+ \left\lfloor{\frac{N-a}{p^2}}\right\rfloor}\right)\\
&-2\sum_{a=1}^{p-1}\left({\left\lfloor{\frac{N+p\, a}{p^2}}\right\rfloor+ \left\lfloor{\frac{N-p\,a}{p^2}}\right\rfloor}\right)\\
\end{aligned}\\
&= 4\sum_{a=1}^{p^2-1}\left\lfloor{\frac{N+a}{p^2}}\right\rfloor - 4\sum_{a=1}^{p-1}\left\lfloor{\frac{N+p\,a}{p^2}}\right\rfloor
\end{align*}


You forgot that in an align or alignat environment, n alignment points require 2*n* – 1 &. I propose two ways to obtain the two alignments: either with alignat{2}and a\mathrlap for the second line, or with a simple align* and a nested aligned environment for the Σs.

Since \mathrlap is defined in mathtools, I took the opportunity to simplify your code, defining a \floor command, with \DeclarePairedDelimiter: the starred version adds implicitly a pair of \left … \right before the delimiters. Alternatively, you can adjust the size of the delimiters using as an optional argument one of \big, \Big, &c. Also, you don't have to add \limits in a display equation.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{mathtools}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}
\begin{document}

\begin{alignat*}{2}
{\rho}_{p^{2}: a < p^{2}}^{p^{2} \times 1}(N) & = &
2\, p (p - 1)
& + 2 \sum_{a = 1}^{p^{2} - 1}
\left(\floor*{\frac{N + a}{p^{2}}}
+ \floor*{\frac{N - a}{p^{2}}}\right) \\
& & & {}- 2 \sum_{a = 1}^{p - 1}
\left(\floor*{\frac{N + p\, a}{p^{2}}}
+ \floor*{\frac{N - p\, a}{p^{2}}}\right) \\
&= \mathrlap{4 \sum_{a = 1}^{p^{2} - 1} \floor*{\frac{N + a}{{p}^{2}}}
- 4 \sum_{a = 1}^{p - 1} \floor*{\frac{N + p\, a}{{p}^{2}}}}
\end{alignat*}

\begin{align*}
{\rho}_{p^{2}: a < p^{2}}^{p^{2} \times 1}(N) & =
2\, p (p - 1)
\begin{aligned}[t] & + 2 \sum_{a = 1}^{p^{2} - 1}
\left(\floor*{\frac{N + a}{p^{2}}}
+ \floor*{\frac{N - a}{p^{2}}}\right) \\
& - 2 \sum_{a = 1}^{p - 1}
\left(\floor*{\frac{N + p\, a}{p^{2}}}
+ \floor*{\frac{N - p\, a}{p^{2}}}\right)
\end{aligned}\\
&= 4 \sum_{a = 1}^{p^{2} - 1} \floor*{\frac{N + a}{{p}^{2}}}
- 4 \sum_{a = 1}^{p - 1} \floor*{\frac{N + p\, a}{{p}^{2}}}
\end{align*}

\end{document}