Search code examples
latexoverleaf

"Missing $ inserted", even though I am using \begin{equation}


Here is my code in Overleaf:

\begin{equation}
\left(\begin{tabular}{l}\sum\limits_{i=0}^n\bigg |\sum\limits_{j=0}^ip_{j}-q_{j}\bigg | \geq \bigg | \sum\limits_{i=0}^n\sum\limits_{j=0}^ip_{j}-q_{j}\bigg | = \bigg | \sum\limits_{j=0}^n\sum\limits_{i=0}^jp_{j}-q_{j}\bigg |\\ = \bigg |\sum\limits_{j=0}^n(p_{j}-q_{j})(j+1)\bigg | = \bigg |\sum\limits_{j=0}^n j(p_{j}-q_{j}) + (p_{j}-q_{j})\bigg |\\
=   \bigg |\sum\limits_{j=0}^n jp_{j} - \sum\limits_{j=0}^n jq_{j}\bigg | = \bigg |\mu_{p} - \mu_{q}\bigg |\end{tabular}\right)
\end{equation}

It runs and produces the output I want, but it gives me a "Missing $ inserted" error, even though I was under the impression that $'s weren't necessary for math mode. Here is a more detailed error message when I make a mistake that for some reason leaves it unable to compile.

Error

I get the feeling that that mistake relates to the "Missing $" error, so now I want to see if I can rectify it.


Solution

  • A tabular is for text, thus the cells of a tabular will be in text mode and you will correctly get errors if you use math content without $ or similar within them.

    You could e.g. use aligned instead:

    \documentclass{article}
    
    \usepackage{mathtools}
    
    \begin{document}
    
    \begin{equation}
    \left(\begin{aligned}
    &\sum\limits_{i=0}^n\bigg |\sum\limits_{j=0}^ip_{j}-q_{j}\bigg | \geq \bigg | \sum\limits_{i=0}^n\sum\limits_{j=0}^ip_{j}-q_{j}\bigg | = \bigg | \sum\limits_{j=0}^n\sum\limits_{i=0}^jp_{j}-q_{j}\bigg |\\
    & = \bigg |\sum\limits_{j=0}^n(p_{j}-q_{j})(j+1)\bigg | = \bigg |\sum\limits_{j=0}^n j(p_{j}-q_{j}) + (p_{j}-q_{j})\bigg |\\
    &=   \bigg |\sum\limits_{j=0}^n jp_{j} - \sum\limits_{j=0}^n jq_{j}\bigg | = \bigg |\mu_{p} - \mu_{q}\bigg |
    \end{aligned}\right)
    \end{equation}
    
    
    \end{document}