# Aligning an equation in Latex

I've the following equation :

    \begin{equation}\label{eq:5}
\frac{\partial s}{\partial b} = \frac{\partial \sum_{i=1}^{n} (y_i -(W\cdot x_i+b))^2}{\partial b}\\

=\sum_{i=1}^{n} 2(y_i -W\cdot x_i-b)\cdot(-1))

=2\cdot \sum_{i=1}^{n} (-y_i + a \cdot x_i + b )  \stackrel{!}{=} 0} \\

\Longleftrightarrow a\cdot (\sum_{i=1}^{n} x_i) + \sum_{i=1}^{n}b = \sum_{i=1}^{n} y_i

\Longleftrightarrow a\cdot (\sum_{i=1}^{n} x_i) + n \cdot b = \sum_{i=1}^{n}y_i
\end{equation}


and here how the result looks like : I tough that using align would solve the issue here is what I've tried:

    \begin{equation}\label{eq:5}
\begin{align}

\frac{\partial s}{\partial b} = \frac{\partial \sum_{i=1}^{n} (y_i -(W\cdot x_i+b))^2}{\partial b}\\

&=\sum_{i=1}^{n} 2(y_i -W\cdot x_i-b)\cdot(-1))

=2\cdot \sum_{i=1}^{n} (-y_i + a \cdot x_i + b )  \stackrel{!}{=} 0} \\

\Longleftrightarrow a\cdot (\sum_{i=1}^{n} x_i) + \sum_{i=1}^{n}b = \sum_{i=1}^{n} y_i

\Longleftrightarrow a\cdot (\sum_{i=1}^{n} x_i) + n \cdot b = \sum_{i=1}^{n}y_i

\end{align}
\end{equation}


There is now change in the result ! My question is how to align the equation despite starting a new line ?

• Proper use of alignment characters. You have to add the alignment character & in each line. Equations will be aligned relative to the alignment character(s). Jun 7, 2017 at 12:43
• @Huang_d can you write an example because I've aligned '&' without any change ! Jun 7, 2017 at 12:49
• You can't have empty lines inside a display math environment, and you can't have an align environment inside an equation environment. Both will throw errors. Jun 7, 2017 at 12:54
• @TorbjørnT. is there no way to align them ? Jun 7, 2017 at 12:56
• I didn't say there is no way to align them, I merely pointed out a couple of obvious mistakes in your code. Jun 7, 2017 at 14:18

It is not very clear how you like format your equations ... Like this? \documentclass{article}
\usepackage{mathtools}

\begin{document}
\begin{equation}\label{eq:5}
\begin{split}
\frac{\partial s}
{\partial b}
& = \frac{\partial \sum_{i=1}^{n} (y_i -(W\cdot x_i+b))^2}
{\partial b}\\
& = \sum_{i=1}^{n} 2(y_i -W\cdot x_i-b)\cdot(-1))
= 2\cdot \sum_{i=1}^{n} (-y_i + a \cdot x_i + b )  \stackrel{!}{=} 0    \\
&   \Longleftrightarrow a\cdot \sum_{i=1}^{n} x_i + \sum_{i=1}^{n}b = \sum_{i=1}^{n} y_i  \\
&   \Longleftrightarrow a\cdot \sum_{i=1}^{n} x_i + n \cdot b = \sum_{i=1}^{n}y_i
\end{split}
\end{equation}
\end{document}


As related links on the side indicate (such as this one https://tex.stackexchange.com/a/74822/132800 ), alignment characters have to be put on each line. In your case,

\begin{align}
\frac{\partial s}{\partial b} =& \frac{\partial \sum_{i=1}^{n} (y_i -(W\cdot x_i+b))^2}{\partial b}\\
=&\sum_{i=1}^{n} 2(y_i -W\cdot x_i-b)\cdot(-1))\\
=&2\cdot \sum_{i=1}^{n} (-y_i + a \cdot x_i + b )  \stackrel{!}{=} 0} \\
\Longleftrightarrow& a\cdot (\sum_{i=1}^{n} x_i) + \sum_{i=1}^{n}b = \sum_{i=1}^{n} y_i\\
\Longleftrightarrow& a\cdot (\sum_{i=1}^{n} x_i) + n \cdot b = \sum_{i=1}^{n}y_i
\end{align}

• thanks for helping but ur answer did't change the result Jun 7, 2017 at 13:02