How to Fix the LaTeX Error “Missing $ Inserted”
At a glance
- 1. Find the first error
Read the first reported error and inspect the source line it names, not just the final line of the log.
- 2. Check for underscores
Look for an underscore in ordinary text, a filename, or a path; TeX treats it as a math subscript.
- 3. Use safe syntax
Write a text underscore as \_ or use a path-aware command; for files, consider renaming underscores to hyphens.
- 4. Compile again
Fix the earliest cause and rerun LaTeX, since later errors may be consequences of the first one.
The LaTeX message ! Missing $ inserted means TeX encountered something it normally expects inside math mode while processing ordinary text. The usual trigger is an underscore (_), which LaTeX interprets as a subscript marker. A path or filename can be involved, but the actual problem may instead be a line of text, a command argument, or a math expression with a missing delimiter.
The message is an error-recovery guess, not a reliable diagnosis. TeX tries to continue by inserting a math shift, so later errors can be consequences of the first one. Start with the earliest error in the log.
Why an underscore causes this error
In ordinary LaTeX text, _ is reserved for subscripts in math mode. For example, x_1 is valid in math mode but not as plain text:
The value is x_1.
Write a literal underscore using \_:
The value is x\_1.
For a mathematical subscript, put the expression in math mode instead:
The value is $x_1$.
The distinction matters when you mention a path. If the path is being typeset as ordinary text, its underscore needs a text-safe command or a path-aware command. A filename supplied to a command such as \includegraphics, however, is not simply ordinary prose. How special characters are handled there can depend on the command and setup, so do not assume every underscore in a filename is necessarily the cause.
Find the actual source of the error
Look in the .log file for the first occurrence of ! Missing $ inserted. LaTeX usually prints a line number nearby, but the named line may be where TeX noticed the problem rather than where it began. Inspect that line and the preceding few lines for:
- An unescaped underscore in text, such as
file_nameorx_1outside math mode. - A path being printed with a text-formatting command.
- A missing
$or\)earlier in the document that left TeX in the wrong mode. - A command or environment whose contents require math mode.
If the error points to a line containing a path, check how the path is used. A path printed in a sentence is text; a path passed as an argument to a file command is a filename. Those cases do not always need the same fix.
Print paths safely
For a path that appears as text, the \path command from the url package is a good choice. It handles common path characters, including underscores, and can break long paths across lines.
Here is a complete example that compiles without needing an external image file:
\documentclass{article}
\usepackage{url}
\begin{document}
The image is stored at \path{figures/run_1/photo.png}.
In ordinary text, write an underscore as \texttt{run\_1}.
\end{document}
The url package provides \path; the \_ in the second sentence is the text-safe way to print an underscore. The hyperref package also provides path and URL commands and is commonly used in documents with links.
If you are referring to a file in a command such as \includegraphics, first check whether the error actually comes from that command. A straightforward way to avoid filename-special-character problems is to rename the file and use a simple name such as run-1.png. If renaming is not practical, \detokenize can be useful in supported filename arguments; for example, try \includegraphics{\detokenize{figures/run_1.png}} with the graphicx package. Since command and package details can matter, confirm that the file is found and that the first error disappears. Renaming is often the clearest fix.
Worked example: a path in a sentence
Suppose the document contains:
The output file is results/run_2.csv.
The underscore is being typeset as ordinary text, so TeX reads it as a subscript marker and may report Missing $ inserted. Either escape it:
The output file is results/run\_2.csv.
or use a path command:
\path{results/run_2.csv}
The first option is convenient for a short mention. The second is usually better for a full path because it also handles other path characters and long lines more appropriately.
Common mistakes
- Adding
$around the whole line without checking it. This can hide the symptom while changing the document’s spacing and meaning. Use math mode only for mathematical notation. - Fixing a later error first. TeX’s recovery attempt can produce a chain of confusing messages. Correct the earliest error, then compile again.
- Assuming every error mentioning a path is caused by the filename. The underscore may be in nearby prose, or an earlier missing delimiter may have changed TeX’s mode.
- Using
\_indiscriminately in a filename argument. It is the standard way to print an underscore in text, but filename arguments are handled by the command that reads the file. If in doubt, use a simple filename or test the command’s supported syntax. - Using a Windows backslash path directly. In LaTeX, backslashes begin commands. Use forward slashes in paths, such as
figures/run-1.png.
After correcting the likely source, compile again and inspect the new first error, if any. If no underscore or other math-only syntax appears on the reported line, check earlier math delimiters and command arguments before changing the path.
References
- LaTeX2e: An unofficial reference manual
- The LaTeX2e Sources
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.