LaTeX Special Characters: Escape Characters Correctly

In ordinary LaTeX text, escape # $ % & _ { } with a backslash: \# \$ \% \& \_ \{ \}. Print the remaining reserved characters with \textbackslash{}, \textasciitilde{}, and \textasciicircum{}. A bare backslash begins a command, a bare tilde creates a nonbreaking space, and a bare caret has a special TeX role. For literal code or URLs, use a purpose-built command instead of escaping each character manually.

Updated November 22, 2026

The ten reserved characters and their printable forms

LaTeX assigns syntax to ten familiar keyboard characters: backslash, braces, dollar, ampersand, hash, percent, underscore, caret, and tilde. Their source meaning is different from the glyph you may want to print. The usual escape is a backslash followed by the character, but that rule does not work uniformly for all ten.

A backslash followed by another backslash is a line-break command, not a printable backslash. Similarly, backslash-tilde and backslash-caret are accent commands rather than reliable literal ASCII symbols. Use the named text commands for these three characters. The empty braces after a letter-based command terminate its name without consuming the following word space.

The complete example prints the characters in a text context with T1 font encoding. Current LaTeX supports UTF-8 input by default, but input encoding and font encoding are different concerns. T1 selects an output font encoding with suitable text glyphs; it does not make reserved syntax stop being special.

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
Hash: \#; dollar: \$; percent: \%; ampersand: \&.

Underscore: \_; braces: \{ and \}.

Backslash: \textbackslash{};
tilde: \textasciitilde{};
caret: \textasciicircum{}.
\end{document}

Percent, dollar, ampersand, and hash

An unescaped percent begins a comment that continues to the end of the source line. If a sentence disappears after a percentage, write \% rather than changing the font or adding math mode. A comment can also swallow a closing brace, making the eventual error appear much later than the actual mistake.

An unescaped dollar changes math mode. Use \$ for a price and ordinary text for the surrounding currency description. Putting an entire price sentence inside math mode changes spacing and letter styling unnecessarily. For mathematical quantities, enter math mode intentionally and keep currency or explanatory words in an appropriate text context.

An ampersand separates alignment cells in tabular and mathematical alignments. A company name containing an ampersand must use \& even inside a table cell. A hash is a parameter marker in macro definitions; use \# when you mean a visible issue number or hashtag. These escapes preserve the intended content instead of altering the surrounding structure.

\documentclass{article}
\begin{document}
The discount is 15\%, and the price is \$20.
Issue \#42 belongs to Research \& Development.

\begin{tabular}{ll}
Department & Budget \\
Research \& Development & \$20
\end{tabular}
\end{document}

Underscores, braces, and mathematical meaning

An underscore introduces a subscript in math mode. In ordinary text it commonly triggers Missing dollar inserted, because TeX assumes you intended a mathematical subscript. Use \_ for a literal underscore in an identifier. Do not fix a filename by putting it in math mode: that would treat its letters as variables and the suffix as a subscript.

Braces group source and delimit command arguments, so plain braces are not printed. Use \{ and \} for visible braces in text or math. In a set expression, escaped braces are delimiters, while the unescaped braces around a command argument remain structural. Both roles can occur next to each other in valid source.

The same visual character can have different mathematical jobs. A literal underscore is not a subscript, and an ASCII caret is not an exponent. For actual mathematical structure, use the syntax x_i or x^2 inside math mode. Escaping everything indiscriminately can turn a correct formula into literal text instead of protecting it.

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
The identifier is \texttt{sample\_id}.
The visible group is \{alpha, beta\}.

The mathematical expression is $x_i^2$.
The set is $A=\{1,2,3\}$.
\end{document}

Literal code and file paths

For a short literal fragment, \verb uses a delimiter of your choice and prints the intervening characters without normal command interpretation. Choose a delimiter absent from the fragment. A vertical bar is common, but a fragment containing a bar needs another character. The opening and closing delimiter must match on the same source line.

\verb is useful for showing a Windows path, a command name, or an identifier with several special characters. It is not suitable inside ordinary command arguments such as section headings or captions. In those locations, use escaped text with \texttt for simple content, or a package interface explicitly designed for the context.

For a multi-line literal block, the verbatim environment preserves source-like characters and spacing. Long lines do not automatically become well-formatted wrapped code. For larger listings with line breaking or syntax highlighting, choose a dedicated code package rather than forcing a paragraph of \verb fragments into a narrow column.

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
Path: \verb|C:\data\run_01|.

Command: \verb|\textbf{example}|.

A pipe: \verb+left|right+.

\begin{verbatim}
# literal example
cost = "$20"  # 15% discount
file_name = "run_01"
\end{verbatim}
\end{document}

URLs need URL handling, not ordinary text escaping

Load hyperref and use \url for a visible clickable URL. It understands common URL characters such as underscores and gives LaTeX suitable break opportunities. Writing the same address as ordinary text can trigger syntax errors or create a line too long for the available width. The url package is an alternative when you do not need hyperlinks.

Use \href when the visible link text should differ from the address. The label is ordinary LaTeX content and must follow ordinary escaping rules, even though the destination has special URL handling. Do not apply one blanket escape operation to both arguments without considering their separate contexts.

Test real addresses in the generated PDF, especially those containing percent-encoded bytes, fragments, or query strings. Wrapping a URL command in another macro can change when special characters are interpreted. A valid-looking printed address is not enough: check that the clickable destination is the intended one and that punctuation after the link is not accidentally part of it.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
\begin{document}
\url{https://example.org/data/sample_id}

\href{https://example.org/data/sample_id}{View sample\_id}
\end{document}

Tildes, pipes, angle brackets, and Unicode

A bare tilde creates a nonbreaking space, useful between a label and number when they should stay together. A visible text tilde uses \textasciitilde; a mathematical similarity relation uses \sim. These are different operations, not interchangeable ways to solve the same display problem. Use an accent command only when you actually want a tilde accent.

For a text pipe, use \textbar. In mathematics, use \mid for a relation such as divides, and \lvert and \rvert from amsmath for absolute-value delimiters. The glyph may look similar, but the mathematical role controls surrounding spacing. Likewise, use text angle-bracket commands for literal text and mathematical comparison symbols in math mode.

Some direct keyboard glyphs depend on the text font encoding. Under traditional OT1 encoding, a raw pipe or angle bracket can print an unexpected character, so named text commands and a suitable encoding are safer. Unicode support also depends on the engine and selected font. Switching engines can expand font access, but does not remove LaTeX's reserved-character syntax.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\begin{document}
Figure~1 stays together.
Literal: \textasciitilde{}, \textbar{},
\textless{}tag\textgreater{}.

Math: $a\sim b$, $a\mid b$, $\lvert x\rvert$, and $x<y$.
\end{document}

Escape once, at the right boundary

When generating LaTeX from a program, distinguish the programming language's string syntax from the final LaTeX source. A doubled backslash in a JSON or JavaScript string may produce one backslash in the emitted file. Inspect the actual .tex output before adding more escapes, because fixing the representation you see in a debugger can corrupt the document.

Treat plain-text fields and trusted LaTeX fragments differently. Escape plain text before inserting it into ordinary text contexts, but do not blindly escape commands that are meant to execute. Conversely, accepting arbitrary LaTeX is not made safe by replacing a handful of characters. Security and compilation sandboxing require separate controls.

For debugging, read the first relevant error and inspect the preceding source line too. A missing brace, dollar, or commented-out delimiter can shift the apparent location of a failure. Reduce the input to the troublesome character in a minimal document, then restore its original heading, URL, table, or math context to identify which rules apply.

Frequently asked questions

Which LaTeX characters need escaping?

In ordinary text, write \# \$ \% \& \_ \{ \} for those seven reserved characters. Use \textbackslash{}, \textasciitilde{}, and \textasciicircum{} for a literal backslash, tilde, and caret.

Why does my percent sign hide the rest of a line?

A bare % begins a comment. Use \% to print a percentage. Check whether the comment also hid a closing brace, because that can cause a later runaway-argument error.

How do I print a filename containing underscores?

Use \texttt{file\_name} for simple text or \verb|file_name| in ordinary body content. For a URL, prefer \url from hyperref or url rather than manually escaping its characters.

Is a double backslash a printable backslash?

No. It requests a line break in supported contexts. Use \textbackslash{} in text, \backslash in math, or \verb for a short literal code fragment.

Documentation and related guides

The LaTeX documentation, url documentation, and hyperref manual cover text commands and URL-specific parsing.

Compile a small example first

Paste a complete example into a project in Octree, compile it, and inspect the PDF before applying the change to your main document. Keep the packages and document class from your real template when checking compatibility.

Try Octree Free →