[Tex/LaTex] mathcal inside gather environment not working

alignmath-mode

I'm trying to display a sequence of equations, plainly centred, so without any alignment you get from the align environment. I thought gather would do the job, but when I try to use mathcal, I get the error:

! LaTeX Error: \mathcal allowed only in math mode.

It works fine using align. What am I doing wrong?

Minimal code to reproduce:

\documentclass[12pt]{article}
\usepackage{a4}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\begin{align*}
p(f) &= \mathcal{N}\left(f; 0, K\right)
\end{align*}

\begin{gather}
p(f) &= \mathcal{N}\left(f; 0, K\right)
\end{gather}

\end{document}

Best Answer

For several reasons some error catching in AMS environments is awkward; this is a case. The error is not really in \mathcal; indeed, if you remove it, the first error message is

! Missing $ inserted.

If you also remove \left and \right (that shouldn't be there to begin with), the error becomes

! Extra alignment tab has been changed to \cr.

Hmm, there's something wrong with &. And this is the real problem: the gather environment has no alignment point, so & must not be used in it.

Here's an example:

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\begin{align*}
p(f) &= \mathcal{N}(f; 0, K) \\
q(f) &= \mathcal{N}(f; K, 0) + 3
\end{align*}

\begin{gather}
p(f) = \mathcal{N}(f; 0, K) \\
q(f) = \mathcal{N}(f; K, 0) + 3
\end{gather}

\end{document}

enter image description here

Note that I removed \left and \right that here only do damages. I also removed the call to the a4 package that's obsolete and deprecated.

Don't use align or gather as substitutes for equation (or equation*) when you have just one formula: they're for multiline displays.