Special Characters in LaTeX: Underscore, Tilde, Pipe, Percent, Dollar, Hash

Ten characters have a special meaning in LaTeX source: #, $, %, &, _, {, }, ~, ^ and \. Type one of them as ordinary text and you get a comment, a subscript, a table column or an error instead of the character. This guide shows how to print each one, how to handle the pipe, angle brackets, quotes and colons, how to put underscores in URLs and file names, and how to typeset the degree, copyright, section, currency and inverted punctuation signs that are not on the keyboard.

The Ten Reserved Characters

Seven of the ten are printed by putting a backslash in front of them. The other three (~, ^ and \) are different, because \~ and \^ are accent commands and \\ is a line break, so they have named commands instead:

CharacterMeaning in LaTeX sourceTo print it
#Macro parameter (#1)\#
$Starts and ends inline math\$
%Comment to the end of the line\%
&Column separator in tables and align\&
_Subscript (math only)\_
{ }Grouping and command arguments\{ \}
~Non-breaking space\textasciitilde
^Superscript (math only)\textasciicircum
\Starts every command\textbackslash
\# \$ \% \& \_ \{ \} \textasciitilde{} \textasciicircum{} \textbackslash{}

50\% of \$100 is \$50. Item \#3. Tom \& Jerry. my\_file. \{braces\}.

The empty braces after \textasciitilde{} and the other word-like commands matter when text follows: a command name made of letters swallows the space after it, so \textbackslash n prints \n with no gap, while \textbackslash{} n keeps the space. The single-character commands such as \% do not have this problem.

All of these work in math mode too. $x\_y$ prints an underscore between the letters where $x_y$ would set y as a subscript, and $\{1, 2, 3\}$ gives the braces of a set.

Tilde, Caret and Backslash in Detail

These three cause the most confusion because there are several similar commands and only one is right for a given job:

Tilde:     \textasciitilde{}   % proper ~ character (with T1, see below)
           \~{}                % a small accent-sized tilde, raised
           $\sim$              % math "similar to" relation
           Fig.~3              % bare ~ is a non-breaking space, nothing printed

Caret:     \textasciicircum{}  % proper ^ character (with T1, see below)
           \^{}                % a small circumflex accent
           $x^2$               % superscript in math

Backslash: \textbackslash{}    % in text
           $\backslash$        % in math
           $A \setminus B$     % set difference, with relation spacing

Many older answers suggest \~{} and \^{} for the tilde and caret. They compile, but they produce the accent glyphs, which are smaller and sit above the baseline. \textasciitilde and \textasciicircum give the characters themselves, with one catch: in the default OT1 font encoding they are defined as \~{} and \^{}, so they print the same raised accents. Load \usepackage[T1]{fontenc}, whose fonts have real ASCII tilde and circumflex glyphs, and the output is also correct when copied from the PDF. In typewriter text (\texttt{\textasciitilde} or \verb|~|) the tilde is a full character in either encoding. For a tilde in a URL or path, see the section on \url below.

The Vertical Bar (Pipe)

| is not reserved, but it is still a trap. In math mode it works as typed. In text mode with the default OT1 font encoding, the | slot of the Computer Modern font holds an em dash, so a|b prints as a—b. The same encoding quirk turns < and > into inverted exclamation and question marks. There are two fixes: use the named commands, or switch to T1 encoding, which puts the ASCII characters in their expected places.

\usepackage[T1]{fontenc}   % then | < > work directly in text

\textbar{} in text, \textless{} and \textgreater{} work in any encoding.

$|x|$                     % plain bars, ordinary symbols
$\lvert x \rvert$          % amsmath: correct open/close spacing
$\{\, x \mid x > 0 \,\}$    % \mid is the relation "such that" or "divides"
$a \vert b$                % \vert is the same as |
$\| v \|$                   % double bar, same as \Vert
$\lVert v \rVert$          % amsmath norm with correct spacing

The difference between | and \lvert ... \rvert is spacing. TeX treats | as an ordinary symbol, so it cannot tell an opening bar from a closing one, and expressions such as $|-x|$ get uneven space around the minus sign. \lvert and \rvert from amsmath are declared as opening and closing delimiters and also work with \left and \right. Use \mid when the bar is a relation, as in set-builder notation and conditional probability $P(A \mid B)$. The full set of delimiters, with sizing, is in our brackets and parentheses guide and in the brackets symbol reference.

Quotation Marks

The straight double quote " on the keyboard is not reserved, but it prints a closing quote in both positions. LaTeX builds opening quotes from backticks: ``double'' and `single'. The csquotes package's \enquote handles nesting and language conventions automatically. This has its own guide: quotation marks in LaTeX.

Colon Symbols: : ∶ ∷ and \colon

In text a colon is just a colon. Type : and nothing special happens, so a 16:9 screen or a 3:2 ratio prints as typed. The subtlety is in math mode, where TeX classes : as a relation (its mathcode is "303A, class 3) and puts a medium space on both sides, exactly as it does for =. That is right for a ratio or a “such that” colon and wrong for a function signature, where the colon is punctuation that belongs to the f. \colon is the punctuation version: the LaTeX kernel defines it as a class 6 symbol, and amsmath redefines it with a negative kern in front and a thin space after, so f\colon A \to B sets the way the AMS style guide asks for.

\usepackage{amsmath, mathtools}

A 16:9 screen, a 3:2 ratio          % plain colon in text, nothing to escape

$f : A \to B$          % : is a relation, spaced on both sides like =
$f\colon A \to B$      % \colon is punctuation: tight to f, thin space after
$a : b = c : d$        % ratio and proportion, relation spacing is right

$x \coloneqq y$        % mathtools: := with the colon centred on the = sign
$y \eqqcolon x$        % =:
$x \Coloneqq y$        % ::=
$a \vcentcolon b$      % centred ratio colon, like Unicode U+2236
$a \dblcolon b$        % centred double colon, like U+2237
$a \mathrel{::} b$     % without mathtools: two plain colons as one relation

$\{\, x \mid x > 0 \,\}$     % "such that" with a bar
$\{ x : x > 0 \}$            % "such that" with a colon
$\{ x : \lvert x \rvert < 1 \}$   % prefer : when the condition has bars itself

The plain colon glyph sits on the baseline, so in := it hangs below the equals sign. mathtools fixes this with \vcentcolon, a colon lifted onto the math axis, and builds the definition symbols from it: \coloneqq (≔), \eqqcolon (≕), \Coloneqq (⩴) and \dblcolon for the proportion sign ∷. There is no \Colon in mathtools; that name exists only in unicode-math. Without mathtools, \mathrel{:} and \mathrel{::} give the same relation spacing with the ordinary glyph.

For “such that” in set-builder notation, \mid and : are both relations with identical spacing; which one you use is a matter of house style, and the colon is the safer choice when the condition contains absolute-value bars (\lvert x \rvert) that would otherwise run into the bar. Do not use \colon there: as punctuation it pulls toward the x.

SymbolUnicodeLaTeX
:U+003A colon: in text; : (relation) or \colon (punctuation) in math
∶U+2236 ratio\vcentcolon (mathtools) or :
∷U+2237 proportion\dblcolon (mathtools) or \mathrel{::}
≔U+2254 colon equals\coloneqq (mathtools)
≕U+2255 equals colon\eqqcolon (mathtools)
⩴U+2A74 double colon equal\Coloneqq (mathtools)
:U+FF1A fullwidth colonCJK text only; use : in LaTeX

To type them outside LaTeX: the plain colon is Alt+58 on a Windows numeric keypad, and the others have no Alt code. In Word, type the hex code and press Alt+X (2254 becomes ≔). On a Mac, open the Character Viewer with Ctrl+Cmd+Space and search for “ratio” or “colon”. Under pdfLaTeX the ratio, proportion and colon-equals characters have no UTF-8 mapping, so paste the commands above rather than the symbols. Which to use: \colon for f\colon A \to B and nothing else; : or \mid for ratios and “such that”; \coloneqq from mathtools for definitions.

Verbatim Text, URLs and File Names

When a whole string is full of special characters, escaping each one is tedious and error-prone. Three tools print text exactly as typed:

\usepackage{hyperref}      % provides \url (or \usepackage{url} for \url only)

\verb|# $ % & _ { } ~ ^ \|    % inline, any delimiter that is not in the text
\verb+a|b+                   % use + when the text contains |

\begin{verbatim}
# $ % & _ { } ~ ^ \
\end{verbatim}

\url{https://example.com/my_page?a=1&b=2#top}
\url{~user/file_name.txt}

\texttt{my\_file.tex}        % escaped by hand, in typewriter font
  • \verb takes any character as its delimiter and repeats it to close. It cannot be used inside the argument of another command (a footnote, caption or section title); use \texttt with escaped characters there, or \cprotect from the cprotect package.
  • \url prints the address in typewriter font, allows line breaks after slashes and dots, and makes it clickable when hyperref is loaded. Underscores, tildes, percent signs, hashes and ampersands need no escaping inside it. The exception is \url inside the argument of another command (a footnote or caption), where % and # lose their protection; move the address out with \urldef or escape those two characters there. \href{url}{text} is the same for linked text, covered in the hyperlinks guide.
  • For code listings, the listings or minted packages give verbatim blocks with syntax highlighting.

If a document has many underscores in plain prose, the underscore package makes a bare _ print as an underscore in text while keeping its subscript meaning in math. It can interfere with packages that parse underscores in arguments (file names passed to \includegraphics, for example), so prefer \_ unless the count is overwhelming. \textunderscore is the long name of the same character as \_.

Inverted Question and Exclamation Marks

Spanish-style ¿ and ¡ are ligatures built from a question or exclamation mark followed by a backtick, and also have named commands:

?`Qu\'e tal? !`Hola!
\textquestiondown Qu\'e tal? \textexclamdown Hola!

The ligature form (?` and !`) works in the standard Computer Modern fonts and in T1 encoding. With babel or polyglossia and Spanish selected, the characters ¿ and ¡ can also be typed directly in a UTF-8 file.

Degree, Copyright, Section, Currency and Other Signs

These symbols are available in any document; the ones that used to need \usepackage{textcomp} have been part of the LaTeX kernel since the 2020 release, and loading textcomp is harmless on older installations:

\usepackage[T1]{fontenc}
\usepackage{eurosym}        % only for \euro; \texteuro needs no package

90\textdegree{} and 20\textcelsius{}
\textregistered{} \copyright{} \texttrademark{}
\S\,3 and \P\,2            % section and paragraph (pilcrow) signs
\dag{} \ddag{}               % dagger and double dagger
\pounds{}5 \texteuro{}5 \euro{5} \textyen{} \textcent{}
\textbullet{} \textperiodcentered{} \textendash{} \textemdash{} \textellipsis{} \dots
SymbolCommandNotes
°\textdegreeMath: $90^{\circ}$; the siunitx package gives \ang{90} and \qty{20}{\celsius}
®\textregistered
©\copyright
™\texttrademark
§\SSection sign
\PPilcrow
† ‡\dag, \ddagAlso \dagger, \ddagger in math
£\poundsSame as \textsterling
€\texteuroGlyph depends on the font; \euro from eurosym draws its own
¥ ¢\textyen, \textcent
• ·\textbullet, \textperiodcenteredMath: \bullet, \cdot
– —--, ---Also \textendash, \textemdash
…\dotsMath: \ldots, \cdots

Under XeLaTeX or LuaLaTeX with fontspec, every one of these can also be typed directly as a Unicode character, provided the chosen font contains the glyph. Under pdfLaTeX, a UTF-8 file can contain the common ones (°, ©, £, €, accented letters) and they are translated to the commands above; a character that has no mapping stops with Unicode character ... not set up for use with LaTeX. Greek letters and math symbols have their own reference in the Greek letters and math symbols guide and the symbols reference.

Ampersand and Hash in Tables and Macros

Two of the reserved characters are reserved for a reason you will use constantly: & separates the columns of a tabular or an align, and #1 to #9 stand for the arguments of a macro. Inside those structures the characters do their job; everywhere else they need the backslash:

\begin{tabular}{ll}
  Tom \& Jerry & cartoon \\      % \& prints, & separates
  R\&D         & department \\
\end{tabular}

\newcommand{\twice}[1]{#1 #1}    % #1 is the argument
\twice{hi}                        % hi hi
\newcommand{\hashtag}[1]{\##1}   % prints #tag

Common Errors and Fixes

Message or symptomCauseFix
Missing $ insertedA bare _ or ^ in text, which LaTeX reads as a subscript or superscript and tries to enter math mode forWrite \_ or \textasciicircum; for file names use \verb or \url
Misplaced alignment tab character &A bare & outside a table or alignWrite \&; in a bibliography entry, escape it in the .bib file too
You can't use `macro parameter character #' in horizontal modeA bare # in textWrite \#
Illegal parameter number in definition of \foo# inside a \newcommand body that is not a valid argument numberUse \# for a literal hash, and only #1…#n with n declared in [n]
Text vanishes after a certain word, or Runaway argument? / File ended while scanning use of ...A bare % commented out the rest of the line, often including a closing braceWrite \%
Undefined control sequence pointing at a backslash followed by textA bare \ in a path or expression read as a command, e.g. C:\UsersUse \textbackslash or put the path in \verb
Too many }'s or Missing } insertedUnbalanced braces, often from { or } meant as literal charactersWrite \{ and \}
A dash where a pipe should be; ¡ or ¿ instead of < or >; a raised accent from \textasciitildeOT1 font encoding in text mode\textbar, \textless, \textgreater, or \usepackage[T1]{fontenc} (the only fix for the tilde)
Words glued together after a command: \textbackslash n prints \nThe command name swallowed the following spaceAdd {} or \ after the command
\verb ended by end of line or \verb illegal in argument (older kernels: \verb illegal in command argument)\verb inside a footnote, caption, section title or macro argumentEscape by hand inside \texttt, or use the cprotect package
A mysterious space is missing or no line break happens between two wordsA bare ~ where a tilde was meantWrite \textasciitilde

Complete Example

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amssymb}
\usepackage{hyperref}

\begin{document}

\section{Reserved characters}
Escaped: \# \$ \% \& \_ \{ \} \textasciitilde{} \textasciicircum{} \textbackslash{}

A 50\% discount on a \$20 item saves \$10 (see item \#4, R\&D budget).

\section{Pipes and angle brackets}
In text: \textbar{} \textless{} \textgreater{} (or | < > with T1 encoding).
In math: $\lvert x \rvert$, $\{\, n \in \mathbb{N} \mid n > 0 \,\}$, $\lVert v \rVert$.

\section{URLs, paths and code}
Source: \url{https://example.com/data_set?id=7&v=2#results} \\
Path: \verb|C:\Users\me\my_file.tex| \\
Macro: \texttt{\textbackslash newcommand\{\textbackslash foo\}[1]\{\#1\}}

\section{Symbols}
90\textdegree{} \quad \copyright{} 2026 \quad \textregistered{} \quad \texttrademark{} \quad
\S\,2 \quad \P\,3 \quad \dag{} \quad \pounds{}5 \quad \texteuro{}5 \quad
?`Por qu\'e? \quad !`Claro!

\end{document}

Quick Reference

CharacterText modeMath mode
#\#\#
$\$\$
%\%\%
&\&\&
_\_, \textunderscore\_ (bare _ is a subscript)
{ }\{ \}\{ \}, \lbrace \rbrace
~\textasciitilde\sim (relation), \tilde{x} (accent)
^\textasciicircum\hat{x} (accent), \wedge (logic)
\\textbackslash\backslash, \setminus
|\textbar|, \lvert \rvert, \mid, \|
::: (relation), \colon (punctuation), \coloneqq (mathtools)
< >\textless, \textgreater<, >
" "``...'' or \enquote\text{``...''}
¿ ¡?`, !`, \textquestiondown, \textexclamdown
°\textdegree^\circ
© ® ™\copyright, \textregistered, \texttrademark
§ ¶ † ‡\S, \P, \dag, \ddag\dagger, \ddagger
£ €\pounds, \texteuro / \euro (eurosym)
Literal string\verb|...|, \url{...}

Frequently Asked Questions

How do I type an underscore in LaTeX?

Write \_ in text or math. A bare _ is the subscript operator and gives Missing $ inserted in text. In URLs use \url{...} from hyperref or url, and in file names or code use \verb|file_name| or \texttt{file\_name}.

How do I write a tilde (~) in LaTeX?

Use \textasciitilde together with \usepackage[T1]{fontenc}, or \texttt{\textasciitilde} for a typewriter tilde. A bare ~ is a non-breaking space, and \~{} is a small raised tilde accent. With the default OT1 font encoding \textasciitilde is defined as \~{} and prints that same accent, which is why T1 matters. In math mode, \sim gives the “similar to” relation.

How do I get a vertical bar or pipe in LaTeX?

In math mode type | directly, or use \lvert ... \rvert from amsmath for absolute values and \mid for the “such that” or “divides” relation. In text use \textbar; with the default OT1 font encoding, a bare | in text prints as an em dash.

Why does a percent sign make my text disappear?

Because % starts a comment: LaTeX ignores everything from the % to the end of the line. Write \% to print a percent sign. If the comment swallows a closing brace you get Runaway argument? or File ended while scanning use of ....

How do I write less-than and greater-than signs in text?

Use \textless and \textgreater, or switch to the T1 font encoding with \usepackage[T1]{fontenc} and type < and > directly. With the default OT1 encoding they print as inverted exclamation and question marks. In math mode, $<$ and $>$ always work.

How do I print a backslash in LaTeX?

Use \textbackslash in text and \backslash in math. The sequence \\ is a line break, not a backslash. For a backslash in code or a file path, \verb|C:\path| or the listings package is simpler.

What is the difference between : and \colon in LaTeX?

In math mode a plain : is a relation symbol with a medium space on both sides, like =, which suits ratios (a : b) and set-builder notation. \colon is a punctuation symbol: with amsmath it sits tight against the letter before it and takes a thin space after, which is the correct form for function signatures, f\colon A \to B. For := and ::=, load mathtools and use \coloneqq and \Coloneqq, which centre the colon on the equals sign.

Next Steps

Reserved characters sorted? These guides cover the related territory:

Free LaTeX Tools

Fix special-character errors faster with AI

Octree's AI editor spots the unescaped underscore or ampersand behind a “Missing $ inserted” error, fixes it in place, and compiles your document in seconds.

Try Octree Free →