LaTeX uses backslashes to mark special characters and commands. This means certain characters like backslashes and ampersands need to be escaped with an additional backslash in order for LaTeX to process them correctly.
In LaTeX, the backslash ("\") character is used to denote commands and special symbols. This allows LaTeX to distinguish between normal text and instructions for formatting, math expressions, etc. However, it also means that the backslash itself needs to be handled specially if you want to print an actual backslash character.
Additionally, other special symbols like the ampersand ("&") have predefined meanings in LaTeX involving special commands. To print actual ampersand characters rather than invoking a command, these symbols also need special escaping.
Table of Contents
The way LaTeX allows escaping characters is by adding an additional backslash in front of the character you want to display literally. This "escapes" the character, telling LaTeX to print the character instead of interpreting it as a command or math symbol.
Understanding this escaping mechanism is key to knowing how to handle problematic special characters in LaTeX properly.
The most problematic character in LaTeX is, ironically, the backslash itself. Since backslashes denote special LaTeX commands and environments, a single backslash will often trigger errors or undesired formatting.
To print an actual backslash character, you need to escape it by adding an additional backslash, like \\ . Now, instead of reading the backslash as a command, LaTeX will simply display the character.
For example, to print a Windows-style file path, you would need:
\\hello\\world
The double backslashes are necessary for each backslash you want to display. This is by far the most common escaping required in LaTeX documents.
Ampersands (&) have special meaning in LaTeX, as they are used to denote alignment points in tables and certain other commands. To display a literal ampersand, it needs to be escaped:
Pizza\&Burgers
Anywhere you want to print an actual ampersand, prepend it with a backslash, and LaTeX will interpret it correctly rather than trying to invoke a command.
Hash or pound symbols (#) are used in LaTeX to denote macro parameters or comments when prefacing lines. To display a pound symbol literally, escape it:
My\#1Priority
This tells LaTeX you specifically want the pound character rendered rather than a comment or parameter.
The percent symbol (%) also has special formatting significance in LaTeX when used in math mode for scientific notation display. To show a percent literally, it needs escaping:
100\% Effort
This will render an actual percent sign correctly in text or regular (non-math) mode.
Dollar signs ($) have special LaTeX math mode meaning to display enclosed math equations and expressions. To print dollar signs literally, escape them:
\$5 Dollars
This will show actual dollar sign characters instead of entering math mode.
Underscores (_) also require escaping when used literally in regular body text:
Back\_pain
Without escaping, the underscore LaTeX signifies formatting for subscript or italics in math mode. The backslash escapes it as regular text.
The various braces - curly braces < >, square braces [ ], and regular braces ( ) each have specialized meanings in LaTeX regarding groupings and arguments. To display opening and closing brace characters literally, escape them:
This will show curly braces instead of LaTeX reading grouped syntax commands.
The tilde (~) denotes non-breaking space in LaTeX. To show a literal tilde, use backslash escaping:
Home\~Sweet\~Home
This displays actual tilde symbols without inserting spaces in your text.
The LaTeX "listings" package provides specialized syntax and formatting for displaying source code snippets, pseudocode, and programming languages cleanly. This handles escaping problematic characters automatically:
\usepackage \begin backslash\\hash\#brace\
The lstlisting environment escapes characters, sets proper spacing/fonts, highlights keywords, and more. This makes showing literal symbols easier without manual escaping.
For long, complex URLs with characters like ampersands and backslashes, the LaTeX "url" package helps formatting and line breaking:
\usepackage \url
The \url command handles special characters, hyphenation, and styling. This is better than awkwardly escaping long URLs manually.
The textcomp package expands LaTeX's special character and dingbat options:
\usepackage \textbackslash \textasciitilde \textdagger
Commands like \textbackslash display literal symbols without manual escaping. Convenient for difficult-to-type characters.
If LaTeX gives errors about certain symbols causing command sequence ambiguity, wrap the troublesome spots in curly braces <>:
\textbf
This tells LaTeX exactly what to bold, avoiding confusion from mixing formatting with the underscore and percent symbol.
Enabling the -shell-escape flag in your LaTeX compiler allows even more special characters through system calls. Useful when backslash escaping isn't enough.
Unsure if a symbol needs escaping? Simply prepend it with a backslash to be safe in most cases. At worst LaTeX ignores extra backslashes if they weren't necessary to display the character.
Manual trial-and-error tweaking is often needed when dealing with problematic text characters in LaTeX. But these tips should prevent most common symbol escaping issues users encounter.