Split Equation within Align and \left \right and Align New Split Equation

I am currently trying to split the last part of the below equation into three parts, but am struggling to do so.

\begin{align*}
\notag \mathbb{E}_{(x,y) \sim P(X,Y)}\left[(y-f(x))^2 + (f(x)-\hat{f}(x))^2\right] &= \mathbb{E}_{(x,y) \sim P(X,Y)}\left[(y-f(x))^2 + (f(x)-\hat{f}(x))^2\right]\\
\notag &= \sigma^{2}(x) + \mathbb{E}_{x \sim P(X)}\left[\left(f(x)-\hat{f}(x)\right)^2\right]\\
\notag &=\sigma^{2}(x)+ \mathbb{E}_{x \sim P(X)}\left[\left(f(x)-\mathbb{E}_{x}\left[\hat{f}(x)\right]\right)^{2}+2\left(\left(f(x)-\mathbb{E}_{x}\left[\hat{f}(x)\right]\right)\left(\mathbb{E}_{x}\left[\hat{f}(x)\right]-\hat{f}(x)\right)\right)+\left(\mathbb{E}_{x}\left[\hat{f}(x)\right] - \hat{f}(x)\right)^2\vphantom{\mathbb{E}_{x \sim P(X)}}\right].
\end{align*}


I've tried

\begin{align*}
\notag \mathbb{E}_{(x,y) \sim P(X,Y)}\left[(y-f(x))^2 + (f(x)-\hat{f}(x))^2\right] &= \mathbb{E}_{(x,y) \sim P(X,Y)}\left[(y-f(x))^2 + (f(x)-\hat{f}(x))^2\right]\\
\notag &= \sigma^{2}(x) + \mathbb{E}_{x \sim P(X)}\left[\left(f(x)-\hat{f}(x)\right)^2\right]\\
\begin{split}
\notag &=\sigma^{2}(x)+ \mathbb{E}_{x \sim P(X)}\left[\left(f(x)-\mathbb{E}_{x}\left[\hat{f}(x)\right]\right)^{2}\right.\\
\left.+2\left(\left(f(x)-\mathbb{E}_{x}\left[\hat{f}(x)\right]\right)\left(\mathbb{E}_{x}\left[\hat{f}(x)\right]-\hat{f}(x)\right)\right)\right.\\
\left.+\left(\mathbb{E}_{x}\left[\hat{f}(x)\right] - \hat{f}(x)\right)^2\vphantom{\mathbb{E}_{x \sim P(X)}}\right].
\end{split}
\end{align*}


which seems to work but doesn't align the bottom part of the equation correctly. The problem also seems to be that I have \left and \right within my equation that I would like to split. Basically, I'd like to split the third line of the equation and align them within the \mathbb{E} sign.

• Why are you using \left...\right here in the first place. I see no reason for any of them in this example. Mar 24, 2021 at 12:45
• \left...\right is there for the expected value notation since I have several equations wrapped within it.
– AW27
Mar 24, 2021 at 12:46
• Don't, in most cases you cannot see any difference in size, and in other cases they become overly large. Scale manually if needed, and only use \left...\right in the cases that really need it. Mar 24, 2021 at 12:55
• Okay, however there is a significant size difference when I don't use it.
– AW27
Mar 24, 2021 at 12:56
• Not in the code you give here. For examply why should a hat on a vaiable need the fences to be scaled? That is just unnecessary Mar 24, 2021 at 12:57

A variation on daleif's theme. I'd prefer to space out the main parts, so they'll be more easily distinguishable between one another.

The alignment points are also different, taking into account the similarities among the expressions.

I also added a version where the first equality is on the same line, so you can see it won't work. The overfull, at 10pt font size is 60pt, about 2.5 centimeters.

\documentclass[a4paper]{article}
\usepackage{mathtools,amssymb}

\newcommand{\EE}{\mathbb{E}}

\begin{document}

The following display would fit on any textwidth, possibly even in two-column format.
Some more words to see what the text width is.
\begin{align*}
\lefteqn{\EE_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]} % no space taken
\\[1ex]
&= \EE_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
\\[1ex]
&= \sigma^{2}(x) + \EE_{x \sim P(X)}[(f(x)-\hat{f}(x))^2]
\\[1ex]
&= \begin{aligned}[t]
\sigma^{2}(x) &+ \EE_{x \sim  P(X)} \bigl[ (f(x)-\EE_{x}[\hat{f}(x)])^{2}
\\
&+ 2\bigl((f(x)-\EE_{x}[\hat{f}(x)])(\EE_{x}[\hat{f}(x)]-\hat{f}(x))\bigr)
\\
&+ (\EE_{x}[\hat{f}(x)] - \hat{f}(x))^2%\vphantom{\EE_{x \sim P(X)}}
\bigr].
\end{aligned}
\end{align*}

The following display instead would not fit on any reasonable text width.
Some more words to see what the text width is.
\begin{align*}
\EE_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
&= \EE_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
\\
&= \sigma^{2}(x) + \EE_{x \sim P(X)}[(f(x)-\hat{f}(x))^2]
\\
&= \begin{aligned}[t]
\sigma^{2}(x) &+ \EE_{x \sim  P(X)} \bigl[ (f(x)-\EE_{x}[\hat{f}(x)])^{2}
\\
&+ 2\bigl((f(x)-\EE_{x}[\hat{f}(x)])(\EE_{x}[\hat{f}(x)]-\hat{f}(x))\bigr)
\\
&+ (\EE_{x}[\hat{f}(x)] - \hat{f}(x))^2\vphantom{\EE_{x \sim P(X)}}
\bigr].
\end{aligned}
\end{align*}

\end{document}


I'd probably do something like this. Note how much less cluttered the code is without all those \left...\right. I'd probably also make a macro for the E operator that automatically adds [] to the argument. But that is for another day.

\documentclass[a4paper]{article}
\usepackage{mathtools,amssymb}

\begin{document}

\begin{align*}
\MoveEqLeft[3] \mathbb{E}_{(x,y) \sim P(X,Y)}
[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
\\
= {} & \mathbb{E}_{(x,y) \sim P(X,Y)}[(y-f(x))^2 + (f(x)-\hat{f}(x))^2]
\\
= {} & \sigma^{2}(x) + \mathbb{E}_{x \sim P(X)}[(f(x)-\hat{f}(x))^2]
\\
= {} &\sigma^{2}(x)
\begin{aligned}[t]
{}+{} & \mathbb{E}_{x \sim  P(X)}
\bigl[
(f(x)-\mathbb{E}_{x}[\hat{f}(x)])^{2}
\\
&+2\bigl((f(x)-\mathbb{E}_{x}[\hat{f}(x)])
(\mathbb{E}_{x}[\hat{f}(x)]-\hat{f}(x))
\bigr)
\\
&+(\mathbb{E}_{x}[\hat{f}(x)]
- \hat{f}(x))^2\vphantom{\mathbb{E}_{x \sim P(X)}}
\bigr].
\end{aligned}
\end{align*}

\end{document}


• Thanks, is it possible to align the top two equations first?
– AW27
Mar 24, 2021 at 13:24
• @AW27 just remove the \MoveEqLeft[3] and the first \\, note that this then wastes a lot of space below the first half of the first line, which is why I write it like this. Mar 24, 2021 at 13:49

Here's another solution. It gets rid of all \left and \right autosizing directives, gets rid of the \notag and \vphantom directives, uses curly braces instead of square brackets to enclose the term that spans three rows, gets rid of four pairs of square brackets, gets rid of an unnecessary pair of round parentheses in the penultimate row, and uses \bigl and \bigr only where necessary.

\documentclass{article}
\usepackage{amsmath,amssymb}
\DeclareMathOperator{\E}{\mathbb{E}} % expectations operator
\newcommand\hf{\hat{f}} % handy shortcut macro
\begin{document}
\begin{align*}
&\E_{(x,y) \sim P(X,Y)}\bigl[(y-f(x))^2 + (f(x)-\hf(x))^2\,\bigr] \\

• Just a note for the OP, wen I use the \MoveEqLeft in my solution, we don't have to add these \quad's manually. Use whatever fits you best. Mar 24, 2021 at 13:51
• Good one on the use of braces. Too many have a tendency to use ))))), this is typography, not lisp ;-) Mar 24, 2021 at 13:52