# Align environment end {align*} error

I have an issue to run the following code in an align environment:

\begin{align*}
&\mathrm{
E(Y_i|D_i=1)-E(Y_i|D_i=0)&=E(Y_{1i}|D_i=1)-E(Y_{0i}|D_i=0)
}\\
\mathrm{
&=\underbrace{E(Y_{1i}|D_i=1)-E(Y_{0i}|D_i=1)}_{\text{ATT}}+\underbrace{E(Y_{0i}|D_i=1)-E(Y_{0i}|D_i=0)}_{\text{selection bias}}
}.
\end{align*}


The error message shows me the following: !Missing } inserted.\end {align}

I have tried to debug the code myself, however, all debug attempts were unsuccessful.

Can anyone figure out where the mistake is?

Since you put & inside the \mathrm{}, it will cause that error. I modified your code a little bit (move the & out of \mathrm{} and add some spaces with \:).

\begin{align*}
&\mathrm{
E(Y_i|D_i=1)-E(Y_i|D_i=0)}\\
=\:&\mathrm{E(Y_{1i}|D_i=1)-E(Y_{0i}|D_i=0)
}\\
=\:&\mathrm{\underbrace{E(Y_{1i}|D_i=1)-E(Y_{0i}|D_i=1)}_{\text{ATT}}+\underbrace{E(Y_{0i}|D_i=1)-E(Y_{0i}|D_i=0)}_{\text{selection bias}}
}.
\end{align*}


Here is the result:

For the last line to get normal text too, you can try this

\begin{align*}
&\mathrm{
E(Y_i|D_i=1)-E(Y_i|D_i=0)}\\
=\:&\mathrm{E(Y_{1i}|D_i=1)-E(Y_{0i}|D_i=0)
}\\
=\:&\underbrace{\mathrm{E(Y_{1i}|D_i=1)-E(Y_{0i}|D_i=1)}}_{\text{ATT}}+\underbrace{\mathrm{E(Y_{0i}|D_i=1)-E(Y_{0i}|D_i=0)}}_{\text{selection bias}}.
\end{align*}


• This is what I was just looking for. But how can I solve the issue that the last line is still italic? Jun 5 '21 at 8:34
• You can add \mathrm to each underbrace. You can see the edit in my post. Jun 5 '21 at 8:40

If you really want to print math upright, use a dedicated package rather than adding unwanted markup such as \mathrm.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathastext}

\begin{document}

\begin{align*}
& E(Y_i|D_i=1)-E(Y_i|D_i=0)
\\
&=E(Y_{1i}|D_i=1)-E(Y_{0i}|D_i=0)
\\
&=\underbrace{E(Y_{1i}|D_i=1)-E(Y_{0i}|D_i=1)}_{\text{ATT}}
+\underbrace{E(Y_{0i}|D_i=1)-E(Y_{0i}|D_i=0)}_{\text{selection bias}}.
\end{align*}

\end{document}


• Hi, in this case is it important to write | or \mid? Jun 5 '21 at 9:04
• @Sebastiano I'd use \mid, or, at least, \,|\, But the OP seems not to be worried about correct appearance of math… Jun 5 '21 at 9:09

Welcome! This is due to the mathrm{…} commands containing an ampersand. Anyway, with align*, the equation does not fit the text width, so I suggest using multline*. I also replaced the various | with \mid, which has a better spacing, and added a small vertical spacing between the two lines.

\documentclass{article}
\usepackage{amsmath, amssymb}

\begin{document}

\begin{multline*}
\mathrm{E(Y_i\mid D_i=1)-E(Y_i\mid D_i=0) =E(Y_{1i}\mid D_i=1)-E(Y_{0i}\mid D_i=0)}
\\[0.8ex]
=\underbrace{\mathrm{E(Y_{1i}\mid D_i = 1) - E(Y_{0i}\mid D_i=1)}}_{\text{ATT}} + \underbrace{\mathrm{E(Y_{0i}\mid D_i=1)-E(Y_{0i}\mid D_i=}0)}_{\text{selection bias}}.
\end{multline*}

\end{document}