Written and maintained by CASRAI Editorial Board
Last updated
Short answer: in LaTeX, type \_ (backslash, underscore) to print an underscore in ordinary text. A bare _ is a reserved character, and in math mode it creates a subscript. This guide is for researchers and analysts who get an error or a vanished underscore in a manuscript, thesis, table caption or bibliography and need the correct fix for the place where it occurs.
Underscore in LaTeX: type \_, not _
The Not So Short Introduction to LaTeX 2e (lshort) lists # $ % ^ & _ { } ~ \ as reserved characters that “have a special meaning under LaTeX”, and says that typing them directly “will normally not print, but rather coerce LaTeX to do things you did not intend.” The fix it gives is a prefix backslash. For the underscore that is \_.
The file is named my\_data\_final.csv
This renders as “The file is named my_data_final.csv”. The same command covers the variants people search for: latex underscore, underscore in latex and underscores in latex are all the same task.
What it is used for in research
Underscores turn up constantly in research writing: variable and column names from analysis code, file and dataset names, identifiers and accession-style labels, and URLs or DOIs pasted into a reference list. In each case the character has to survive LaTeX’s processing as a visible underscore. The rest of this guide covers the plain-text fix, the math-mode behaviour, and the arguments (file names, citation keys, labels) where an escape is not enough.
How to input _ in LaTeX? Is _ an underscore?
Yes, the character _ is the underscore. LaTeX gives it a job of its own: in math mode the lshort chapter on mathematical notation describes ^ and _ as typesetting superscripts and subscripts (for example \( A_x = G_{a\times c} \)). That is why a bare underscore in running text fails, and why an underscore you want to see in a formula needs the same escape.
- Literal underscore in text:
\_ - Subscript in a formula:
\( x_i \), or\( x_{ij} \)when the subscript has more than one character (lshort’s examples group multi-character subscripts in braces, as inG_{a\times c}). - Literal underscore inside a formula:
\_, the same escape as in text.
Install or access (official source only)
The \_ escape is part of LaTeX itself, so nothing extra needs installing: it works in any LaTeX installation or hosted LaTeX editor. Obtain LaTeX from a TeX distribution or a hosted editor you trust. The two documents this guide relies on are published on CTAN, the Comprehensive TeX Archive Network: lshort at tobi.oetiker.ch/lshort/lshort.pdf and the optional underscore package at ctan.org/pkg/underscore. Download packages from CTAN or through your distribution’s package manager, not from third-party mirrors.
Step-by-step: print an underscore
- Find the text containing the underscore, for example
my_data_final.csv. - Decide where it sits: ordinary text, a formula, or a file name, citation key or label.
- In ordinary text or a formula where you want the character itself, put a backslash in front of each underscore:
my\_data\_final.csv. - If you want a subscript inside a formula, leave the underscore bare and group multi-character subscripts in braces:
\( x_{ij} \). - If the underscore is in a file name, citation key or label, rename it with a hyphen or camelCase, or use the
underscorepackage routes below. - Recompile and check the output.
Output and how to read it
With my\_data\_final.csv the PDF shows the visible underscores: my_data_final.csv. If the underscore disappears, or a “missing $ inserted” style error appears, a bare _ remains somewhere in text mode. If a formula shows a lowered character instead of an underscore, the underscore was read as a subscript; escape it as \_ if you wanted the character. If an underscore looks like a drawn rule rather than a font character, the package notes below explain that the font encoding has no underscore glyph.
Reserved characters and their escapes
The underscore is one of ten reserved characters. The table is taken from section 1.3.3 of lshort (CTAN nightly build, read 2026-11-07) and covers the related searches for percent sign, dollar sign, ampersand, backslash and tilde in one place.
| Character | Name | What to type |
|---|---|---|
# |
hash | \# |
$ |
dollar sign | \$ |
% |
percent sign | \% |
^ |
caret | \^{} |
& |
ampersand | \& |
_ |
underscore | \_ |
{ |
left brace | \{ |
} |
right brace | \} |
~ |
tilde | \~{} |
\ |
backslash | \textbackslash{} |
LaTeX backslash
The backslash cannot be typed by doubling it. lshort states that the backslash “can not be entered by adding another backslash in front of it (\\); this sequence is used for line breaking. Use the \textbackslash command instead.”
LaTeX percent sign, dollar sign and ampersand
Use \%, \$ and \&. For the percent sign in particular, see the CASRAI guide on writing the percent sign in LaTeX.
LaTeX tilde symbol
lshort gives \~{}. The community-maintained Wikibooks page on LaTeX special characters (read 2026-11-07) gives the alternative \textasciitilde{} and notes that a bare tilde is a non-breaking space.
Special characters inside formulas
What does \( do in LaTeX?
\( opens an inline formula and \) closes it. lshort: “To typeset an inline formula, put it between \( and \) commands, while \[ and \] typesets it in display mode.” Inside that math mode, _ and ^ work as subscript and superscript, which is the behaviour that clashes with a literal underscore.
How to get a negation symbol in LaTeX?
In math mode use \lnot, the logical-negation command shown in lshort’s quantifier example, \( \lnot\forall_{q} P(q) \iff \exists_{q}\lnot P(q) \). Note that this example uses _ for subscripts, so it also shows the underscore doing its math-mode job.
Where \_ still breaks: file names, citation keys and URLs
The documentation for the CTAN underscore package (Donald Arseneau, version 1.0, 13 September 2006, read 2026-11-07) lists the places an underscore cannot simply be typed or escaped. It says to avoid _ in file names and in \cite or \ref tags, and names the arguments that are “moving” in this sense: \cite/\ref tags, \input, \include, \includegraphics, environment names and counter names.
- Safest approach: use hyphens or camelCase in file names, citation keys and label names so that no escape is needed. See the CASRAI guide to the BibTeX file format for how keys are used.
- When you cannot rename a file: the package documentation gives
\include{file\string_name}as an occasional-use workaround. - With the package loaded: it offers a
[strings]option for underscores in these arguments.
Using the underscore package
Load it with \usepackage{underscore}. According to its documentation, a simple _ then “acts just like \_ in text mode, but makes a subscript in maths mode”, so pasted text such as variable names no longer needs escaping outside formulas. It also changes how _ hyphenates: hyphenation of the constituent words is unaffected and a break is allowed after the underscore, and the [nohyphen] option breaks without a hyphen. Both forms print an underscore character if the font encoding has one (for example with \usepackage[T1]{fontenc} or typewriter fonts) and otherwise draw a rule.
Free alternatives and other routes
LaTeX and the CTAN package are free to use. The table compares the routes described in the sources above.
| Route | Where it works | Source |
|---|---|---|
\_ escape |
Ordinary text and formulas, one underscore at a time | lshort, section 1.3.3 |
\usepackage{underscore} |
Text mode, so bare _ prints an underscore; subscripts still work in math mode |
CTAN underscore package documentation |
[strings] package option |
Underscores in file names, \cite/\ref tags and similar arguments |
CTAN underscore package documentation |
\include{file\string_name} |
One-off underscore in an include argument | CTAN underscore package documentation |
| Rename to hyphens or camelCase | File names, citation keys, labels (editorial recommendation: no escape needed) | CASRAI editorial advice |
Troubleshooting
- Error or missing text where a name contains underscores: escape each one as
\_, or load theunderscorepackage for text-mode use. - Underscore in a citation key, label or file name: rename, or use the
\string_and[strings]routes above. - Underscore shows as a subscript in a formula: escape it with
\_if you want the character itself. - Underscore looks like a rule rather than a character: check the font encoding, as in the package notes above.
Working in an editor or online service? The CASRAI guides to Overleaf and TeXstudio cover the tools themselves; the escapes above are the same in any LaTeX engine. For other symbol tasks, see Greek symbols in LaTeX and vertical lines in LaTeX.
FAQ
How do I type an underscore in LaTeX?
Type \_ in text or in math mode. A bare _ is reserved (lshort, section 1.3.3).
Why does a bare underscore cause an error?
Because _ is a reserved character; lshort says reserved characters typed directly will normally not print and can make LaTeX do things you did not intend, and in math mode _ means “subscript”.
Can I stop escaping underscores in text altogether?
Load the underscore package, with the file-name and citation-key limits described above.
How do I print a backslash?
Use \textbackslash{}, not \\.
Sources
- Oetiker, T., et al. The Not So Short Introduction to LaTeX 2e (lshort), section 1.3.3 “Special Characters” and the mathematical-notation sections. CTAN nightly build. tobi.oetiker.ch/lshort/lshort.pdf. Read 2026-11-07.
- Arseneau, D. The underscore package, v1.0, 13 September 2006. CTAN: ctan.org/pkg/underscore. Read 2026-11-07.
- Wikibooks. LaTeX/Special Characters (community-edited). en.wikibooks.org/wiki/LaTeX/Special_Characters. Read 2026-11-07.







