Escaping Special Characters in LaTeX: % $ & _ #

Which characters are invisible commands in LaTeX, what each one silently does to your text, and the escape that gets it back.

Ten characters are invisible commands in LaTeX. The two that bite hardest are %, which deletes the rest of the line without a word, and &, which means "column separator" inside a table and breaks the build anywhere else. Escaping each is one backslash; understanding why it needs escaping is what makes the error messages readable. Underscores and carets are really Sub & Sup in disguise.

Which one to use

Situation Write this Because
A percent sign
\%
Otherwise the line becomes a comment.
A dollar sign
\$
Otherwise it toggles maths mode.
An ampersand
\&
Otherwise a column separator.
An underscore
\_
Otherwise a subscript.
A hash sign
\#
Otherwise a macro parameter.
A backslash itself
\textbackslash
It is the escape character.

The commands

What Type this Renders as
Percent Unescaped, everything after % on that line becomes a comment and vanishes from the output.
50\%
50\%
Dollar The character that switches maths mode on and off, so a price without escaping can swallow half a paragraph.
\$5
\$5
Ampersand A column separator in tables and alignment environments; unescaped it breaks the row rather than printing.
A \& B
A \& B
Underscore Subscript marker. In text mode modern LaTeX errors outright instead of printing nothing.
a_{max}
a_{max}
Hash Macro parameter prefix; a bare # throws an error wherever LaTeX is not reading a definition.
\#1
\#1
Braces Grouping delimiters that never appear in the output unless escaped.
\{ x \}
\{ x \}
Tilde Unescaped ~ is a non-breaking space, which is why URLs pasted into LaTeX go wrong quietly.
\tilde{a}
\tilde{a}
Caret Superscript marker in maths; an undefined character in plain text.
\hat{x}
\hat{x}
Backslash Itself an operator symbol in maths mode; use \setminus for a set difference instead.
\backslash
\backslash
Spaces kept Source whitespace collapses, so layout always comes from commands rather than typing gaps.
a\quad b \qquad c
a\quad b \qquad c

Worked examples

A percentage inside a formula

\frac{65\%}{100\%} = 0.65
\frac{65\%}{100\%} = 0.65

Without the escapes the line ends at the first percent sign and the fraction never closes.

Currency in maths

\$1{,}000 - \$250 = \$750
\$1{,}000 - \$250 = \$750

The comma braces keep the thousands separator at normal baseline rather than as a decimal list separator.

Set-builder in a web page

\{ x : x > 0 \}
\{ x : x > 0 \}

Braces escaped, colon rather than HTML entity for the inequality, because entities are not LaTeX.

File-like names

x_{\text{input\_file}}
x_{\text{input\_file}}

Inside \text{} an underscore still needs escaping; there is no quoting mode that lifts the rule.

Common mistakes

  • Pasting & or < into LaTeX source. Entities belong to HTML; LaTeX sees an ampersand followed by letters and treats it as a table cell break.
  • Doubling up escapes while moving text between tools: \\frac in a file that wanted \frac. This is the most common corruption in pasted converter output.
  • Wrapping maths in $...$ and forgetting that a second unescaped $ anywhere later in the paragraph restarts maths mode from there.
  • Assuming braces are harmless because they are invisible. An unmatched } is a hard error, and a missing pair silently changes what a subscript covers.

Questions about this notation

How do I type a percent sign in LaTeX?
Backslash then percent: \%. The unescaped version starts a comment running to the end of the line, which is why text disappears rather than producing an error.
How do I escape an underscore?
In maths mode, avoid the character entirely and write a braced subscript such as a_{max}. Where you genuinely want the printed glyph, \_ is the portable escape; for a filename wrap the whole thing in \text{} and escape inside it.
Why does my ampersand break the build?
Because & is the column separator inside array, tabular and aligned. Outside such an environment it is invalid; inside one it silently moves content to the next cell. Escape it as \& when you mean the character.
How do I type a backslash, a hash sign or a tilde in running text?
A backslash is \textbackslash{}, a hash is \# and a visible tilde is \textasciitilde{} — not \~, which is an accent that needs a letter after it. Literal braces are \{ and \}. The empty group on the \text... forms stops them eating the space that follows, which is why \textasciitilde x and \textasciitilde{} x differ.

Keep reading

Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.