226

I try to write the following in latex:

\begin{itemize}
    \item \textbf{insert(element|text)} inserts the element or text passed at the start of the selection.
    \item \textbf{insert_after(element|text)} inserts the element or text passed at the end of the selection.
    \item \textbf{replace(element|text)} replaces the selection with the passed text/element.
    \item \textbf{delete()} deletes the selected text.
    \item \textbf{annotate(name,value)} annotates the selected text with the passed name and value-pair. This can either be a hidden meta-data about the selection, or can alter the visible appearance.
    \item \textbf{clear_annotation()} removes any annotation for this specific selection.
    \item \textbf{update_element(value)} performs an update of the element at the selection with the passed value.
\end{itemize}

For some reason, I get a bunch of errors. I think there is something with the use of the word "insert". I get errors like "Missing $ inserted", so it seems like the parses tries to fix some "errors" on my parts. Do I need to escape words like "insert", how do I do that?

11 Answers 11

395

The "Missing $ inserted" is probably caused by the underscores and bars. These characters in LaTeX have special meaning in math mode (which is delimited by $ characters). Try escaping them; e.g. update\_element instead of update_element.

However, if you're trying to display code, a better solution would be to use the \verb command, which will typeset the text in a monospaced font and will automatically handle underscores and bars correctly (no need to escape them with \).

1
  • 2
    I had this error because there was $ symbol in one of the titles in my bibliography. Using \$ helped.
    – azet52
    Commented Feb 1, 2019 at 21:54
54

It's actually the underscores. Use \_ instead, or include the underscore package.

2
  • texlive-latex-recommended has the underscore package. I had to also add the usepackage underscore to my sphinx.sty file
    – zerocog
    Commented Jul 22, 2016 at 22:59
  • Using the 'underscore" package also solved an issue I encountered when trying to generate pdf out from a knitr document in RStudio by using the function "semTable" (before it resulted in the 'Missing $' error.
    – Will
    Commented Aug 4, 2018 at 8:08
21

I had this problem too. I solved it by removing the unnecessary blank line between equation tags. This gives the error:

\begin{equation}
P(\underline{\hat{X}} | \underline{Y}) = ...

\end{equation}

while this code compiles succesfully:

\begin{equation}
P(\underline{\hat{X}} | \underline{Y}) = ...
\end{equation}
2
  • This solved my problem. I was using a \label inside the equation and I had left a blank line after it.
    – jpmorin
    Commented Jan 10, 2013 at 6:35
  • 1
    I keep tripping over this one. I'd happily upvote this answer each time.
    – user443854
    Commented Jan 4 at 21:06
8

also, I had this problem but the bib file wouldn't recompile. I removed the problem, which was an underscore in the note field, and compiled the tex file again, but kept getting the same errors. In the end I del'd the compiled bib file (.bbl I think) and it worked fine. I had to escape the _ using a backslash.

1
  • I to had an underscore in a note field. Thanks so much for suggesting this!
    – Richard
    Commented Jun 20, 2012 at 18:22
8

I had the same problem - and I have read all these answers, but unfortunately none of them worked for me. Eventually I tried removing this line

%\usepackage[latin1]{inputenc}

and all errors disappeared.

1
  • and replacing it with \usepackage[utf8]{inputenc} is not a bad idea
    – Dorian
    Commented Jul 7, 2021 at 21:37
2

My first guess is that LaTeX chokes on | outside a math environment. Missing $ inserted is usually a symptom of something like that.

1
  • 1
    As @Will points out, LaTeX doesn't like _ . Looks like a better explanation than mine too. Commented Mar 19, 2010 at 11:43
2

You can also get this error if you use special character greek letters like \alpha \beta and so on outside of math mode. After i wrapped them in \(...\) the error was gone.

3
  • This worked for me. Any idea why this works and how to make it work without wrapping it?
    – Octane
    Commented Jul 6, 2018 at 14:06
  • it's an escape character.
    – Brndn
    Commented Feb 9, 2020 at 2:52
  • Thank you so much!!! I had this error with using \rightarrow outside math mode and after replacing it with \(\rightarrow\) the errors are gone.
    – Ax M
    Commented May 11, 2022 at 13:21
1

In my code, when I got the error, I checked the possible source, In a line, I had typed a beginning \[ and an ending \] due to which the error of missing $ appeared though I tried using $ for both the brackets. Removing the brackets or using $[$ instead of $\[$ solved my problem. If you've something like that, try altering.

0

I think it gives the error because of the underscore symbol.

Note : underscore symbol should not be written directly, you have to write like as \_.

So fix these kind special symbol errors.

-1

I had this symbol _ in the head of one table and the code didn't run, so I had to delete.

-1

My case was similar to Adrian's. It was an incorrectly written online bib source.

I had

 @online{some_pretty_website, 
    title={Pretty Website}, 
    url={https://www.prettywebsite.com/foo_bar.pdf}
 } 

I had to escape the "_" symbol in the url

 @online{some_pretty_website, 
    title={Pretty Website}, 
    url={https://www.prettywebsite.com/foo\_bar.pdf}
 } 

Not the answer you're looking for? Browse other questions tagged or ask your own question.