Errors when using \end{align*}

I get 6 errors when I use the code below - all 6 of them occur on the line that contains only \end{align*}. The code between the \begin{align*} and \end{align*} works as a displayed equation.

\begin{align*}
p_{\text{undetect}}
&=\sum_{i=0}^{n} {{A_i}{p^i}{(1-p)^{n-i}}} \\
&={{(1-p)^n}{\sum_{i=0}^{n} {{A_i}{\left({\frac{p}{1-p}}\right)}^i}} \\
&={(1-p)^n}\left[\left({\sum_{i=0}^{n} {{A_i}{{\left({\frac{p}{1-p}}\right)}^i}}}\right)-{A_{0}}\right] \\
&={(1-p)^n}}\left[A\left(\frac{p}{1-p}\right)-1\right]
\end{align*}


Four of the error messages are either Missing { inserted or Missing } inserted. The other two error messages are Missing \endgroup and Misplaced \omit.

• Welcome to TeX.SX! You're using way too many useless braces and get lost counting them. – egreg Jan 21 '17 at 21:38
• What's more the braces around A_i in {A_i} (for example) are just clutter, totally unnecessary, but braces around the i (i.e. A_{i}) are actually to be recommended, although they're not strictly required – Au101 Jan 21 '17 at 22:02
• &= \sum_{i = 0}^{n} A_{i}p^{i}(1 - p)^{n - i} \\ is how I would write your first line, although I don't claim to be an authority on the style of your .tex input, some might, for example, like to separate the various terms of the multiplication with spaces, i.e. A_{i} p^{i} (1 - p)^{n - i} – Au101 Jan 21 '17 at 22:03

Your use of the grouping chars { and } is misleading. First, here is the correct way of writing your formula:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align*}
p_{\mathrm{undetect}}
&=\sum_{i=0}^{n} A_i p^i (1-p)^{n-i} \\
&=(1-p)^n\sum_{i=0}^n A_i \left(\frac{p}{1-p}\right)^i \\
&=(1-p)^n\left[\left(\sum_{i=0}^{n} A_i\left(\frac{p}{1-p}\right)^i\right)-A_0\right] \\
&=(1-p)^n\left[A\left(\frac{p}{1-p}\right)-1\right]
\end{align*}
\end{document}


The rule for using the grouping chars here is quite straight forward (as in most other cases): You need to group arguments of macros as in \frac{...}{...}, which apparently takes two arguments, and you need to group the content that is parsed by the special characters ^ and _ (e.g. A_ijk differs dramatically from A_{ijk} and in contrast to what one possibly could think {A_ijk} is not analogous to latter one but to the first example). Any other group is simply superflous. However, A_i and A_{i} are analogous. Even though in the second case the group isn't mandatory, most would recommand to use it to have a uniform input.

• On the other hand, A_{i} is not superfluous, even recommended for beginners (and text editors can even add the braces automatically, so one hasn't to bother). – egreg Jan 21 '17 at 22:44
• @egreg -- right, thought about writing it. I will add it. thanks. – Ruben Jan 21 '17 at 22:46
• @voliver -- see my answer: I just copied your formula as is. I think you have a typo in the third line. The index should be 1 not 0, right? – Ruben Jan 21 '17 at 23:10
• Thank you for your reply; your explanation was very useful. I am not sure how I got into the habit of inserting useless braces. – voliver Jan 22 '17 at 20:45