Ten characters that mean something to TeX before they mean something to you
TeX does not read your file as text. Its input processor sorts every character into a category — escape, begin-group, end-group, math shift, alignment tab, end of line, parameter, superscript, subscript, active, comment, letter, other, invalid — and only the other and letter categories reach the page as glyphs. Ten characters are not other out of the box, and that is the entire reason they need treatment: \ escapes, { and } group, $ shifts into maths, & is the alignment tab, # marks a macro parameter, ^ and _ are the superscript and subscript characters, ~ is an active character (LaTeX gives it a non-breaking space), and % starts a comment.
The escape rule is one line: a backslash followed by a non-letter is a control sequence of length two, and it means "that character, literally". So \%, \$, \&, \#, \_, \{, \} are \ plus the character itself, and they work because those names are defined in the kernel. The three characters with no such shortcut are the ones whose escape would be self-referential or ambiguous: the backslash itself is \textbackslash, the tilde is \textasciitilde, the caret is \textasciicircum, and for symmetry the grave accent is \textasciigrave.
The characters people expect to be special and are not: <, >, |, ", ?, / and * all have the other category in plain LaTeX, so they typeset as themselves. Two caveats. The < and > glyphs do not exist in the older OT1 font encoding, so they can silently print nothing at all — T1 has them, and \textless/\textgreater from textcomp are the encoding-independent answers. And " is made active by some babel language options (German shorthand, Polish, Catalan), which is why a file that compiles in English fails in another language with an undefined-shorthand error.
You can inspect the categorisation yourself, which is the fastest way to stop guessing: \showthe\catcode`\_ prints the number TeX has for a character, and \catcode`\_=12 would demote it to an ordinary character — which is exactly what the verbatim machinery does in a local group, along with turning \ into an ordinary character so that nothing in the block is an escape any more.
\begin{array}{c|l|l}\text{source} & \text{role in TeX} & \text{to print it} \\\hline \texttt{\textbackslash\%} & \text{comment} & \text{percent} \\ \texttt{\textbackslash\$} & \text{math shift} & \text{dollar} \\ \texttt{\textbackslash\&} & \text{alignment tab} & \text{ampersand} \\ \texttt{\textbackslash\#} & \text{parameter} & \text{hash} \\ \texttt{\textbackslash\_} & \text{subscript} & \text{underscore} \\ \texttt{\textasciicircum} & \text{superscript} & \text{caret} \\ \texttt{\textasciitilde} & \text{active} & \text{tilde} \\ \texttt{\textbackslash\{\ \textbackslash\}} & \text{grouping} & \text{braces}\end{array}
Why the same character behaves differently in maths and in text
The category codes do not change between modes, but which of them TeX acts on does. ^ and _ are only interpreted in maths; in text mode they are dropped, which is why a file name typed as sum\_i.cfg without escapes loses its underscore silently instead of raising an error. The alignment character & is the reverse: it is honoured in both modes — in maths inside aligned, array and matrix, in text inside tabular and align — and outside any alignment it produces TeX's most literal message, Misplaced alignment tab character &.
% is unconditional: the comment character ends the line in maths, inside a table cell, inside an argument, everywhere. That makes the unescaped percent the most destructive of the ten, because its damage is invisible on the line where it happens: 50% of cases deletes the rest of that line, so a \{ opened earlier is still unclosed when TeX reaches the next line, and the error message points at the wrong place entirely.
$ is the mode switch itself, so a stray dollar does not "print wrong" — it changes the rules for everything after it, and the classic symptom is a complaint about Math formula or Misplaced } inserted many lines later. { and } group in both modes and print in neither; to print them inside maths you need \{ and \}, which carry math-open and math-close delimiter codes so they scale with \left/\right, or you need \lbrace/\rbrace for the symbols alone.
# is a parameter marker and is only expected inside the body of a definition. Two consequences worth knowing: a literal hash inside \newcommand must be typed ##, because one level of stripping happens when the body is scanned; and the expl3 interface of LaTeX uses \csname-style names full of underscores and colons, which is why those documents wrap everything in \ExplSyntaxOn — a catcode change, not a font choice.
\{\,x \in \mathbb{R} : x_{i}^{2} \le 1\,\} \quad R \sim S \quad a \mapsto b
\{ and \} are delimiters; the grouping braces in the same source are invisible.\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage[utf8]{inputenc} % harmless today, required on older installs
\begin{document}
\section*{Money, measures and percentages}
The invoice reads \$1{,}250.00 for the item, \texteuro 480.00 for the
delivery and \pounds 12.50 in tax; \SI{45}{\percent} of the budget is gone.
The \texttt{siunitx} route keeps quantity and unit together, so a line break
cannot put the number at the end of one line and the sign on the next.
\section*{Special characters inside maths}
The braces below are \emph{printed}, because \verb|\{| and \verb|\}| carry
math-open and math-close delimiter codes rather than grouping:
\[
\mu(A \cap B) \le \min\{\mu(A), \mu(B)\}, \qquad
\{x : \lvert x\rvert < 1\}
\]
Grouping and printing are different jobs: \verb|{a,b}| on its own would
vanish, and \verb|\left\{ ... \right\}| sizes the braces to the content.
Subscripts and superscripts are the whole reason \verb|_| and \verb|^| cannot
be typed in maths, and \verb|\&| is legal only at an alignment tab:
\[
\begin{aligned}
a_{ij}^{2} &= \sum_{k} a_{ik} a_{kj}^{2} \\
b_{1} &= 2a_{1} - 3
\end{aligned}
\]
\section*{A heading is a moving argument}
% \verb inside \section fails: the argument is written to the .toc file.
\section{Files named \texttt{final\_v2.tex}}
To keep verbatim material in a heading you need the \texttt{cprotect}
package, or you accept \texttt{\textbackslash texttt\{...\}} with an escaped
underscore, as above.
\end{document}
Accents: the commands, the letters, and the encoding underneath
The accent commands take one argument and cover the Latin alphabet: \'{e} acute, \`a grave, \^o circumflex, \"u diaeresis or umlaut, \~n tilde, \v{c} caron (hacek), \c{c} cedilla, \.{z} dot above, \u{g} breve, \={a} macron, \b{o} line below, \d{n} dot below, \H{o} double acute, \r{a} ring above, \k{a} ogonek and \t{oo} tie bar. The letters that are not accents have their own commands: \o/\O, \l/\L, \ae/\AE, \oe/\OE, \aa/\AA and \ss, plus \i and \j for the dotless letters you need when you compose something over an i by hand. A capital sharp s exists in Unicode but has no slot in the traditional 8-bit text fonts, which is one of many reasons that particular character waits for XeLaTeX or LuaLaTeX.
Whether those commands produce a single glyph or a composite is decided by the font encoding, and it is the practical difference between OT1 and T1. Under T1 with \usepackage[T1]{fontenc} and a font that has the slots, \'e is the precomposed é: the hyphenator can break inside the word, a reader can copy it out of the PDF, and small caps get an accented shape. Under OT1 the accent is printed over the letter at ship-out, so the word is unhyphenatable at that point, text extraction yields two characters, and some shapes simply do not exist. Since the 2018 release, UTF-8 is LaTeX's default input encoding, so typing é directly works under pdfLaTeX with T1 loaded — the accent commands stay useful because a source file with \'e survives an editor that mangles encodings.
A missing glyph is reported, not invented: the log line Missing character: There is no ż in font cmr10 is a font complaint, and the fixes in order are T1 or the encoding that contains it, \usepackage{lmodern} for a wider Latin-Modern range, or newtxtext/XCharter/fontspec when the character is outside the 8-bit world altogether. Turkish and Polish are the classic traps because dotted and dotless I are meaning-bearing (\i, \I, \k{a}), and a font without the right slots will substitute quietly under pdfLaTeX but not under XeLaTeX.
Maths has its own accent set, entirely separate: \hat, \widehat, \tilde, \widetilde, \bar/\overline, \vec, \dot, \ddot, \check, \breve, \acute, \grave and \mathring. These are accent placements on a math atom, they do not change the letter's symbol class, and using \hat{\gamma} is not the same object as \gamma when you set it in bold or size it — which is why the reference page on accents in maths is linked below.
\acute{e} \quad \grave{a} \quad \hat{o} \quad \check{c} \quad \tilde{n} \quad \bar{a} \quad \dot{x} \quad \ddot{u} \quad \mathring{a} \quad \breve{g}
Punctuation that is not special but still has a right form
Quotation marks are the most visible beginner tell: the ASCII " is a straight double mark, and correct typography opens with two backticks and closes with two apostrophes, so the source ``like this'' yields a matched pair of curved marks and `like this' yields the single form. Nested quotation needs four characters on each side. If that is too much to remember, the csquotes package provides \enquote{...} which also handles the French and German conventions through babel.
Dashes are three different characters: - is the hyphen inside a word, -- is the en dash for ranges (2019--2024, pp.~12--18) and math minus, --- is the em dash for a parenthetical break. Three typed periods are three periods with a sentence-ending spacefactor after them; \ldots in text and \dots in maths is the ellipsis, and in maths the position matters — \dotsc, \dotsb, \dotsm, \dotsi from amsmath place the dots on the axis or at the baseline depending on what follows.
Currency is where the escape is technically right and typographically weak: \$100 prints the ASCII dollar glyph out of the text font. The proper glyphs are \pounds, \texteuro and \textdollar from textcomp, and the structural answer is to keep quantity, space and symbol in one object — \SI{45}{\percent} for a percentage, siunitx's currency support for a money amount — so that a line break cannot separate the digits from the sign.
Sentence spacing is the last of the family, and it is a real mechanism: after a period TeX raises the \spacefactor so the following space is wider — which is correct at the end of a sentence and wrong in Mr. Smith, 1. Introduction, i.e. hence. The fixes are an explicit control space (Mr.\ Smith), a brace to hide the period from the scanner (Mr.{}), or the xspace package for macros that end a word.
Printing material that refuses to be typeset: verb, verbatim and detokenize
Escaping twelve characters by hand inside a code sample is worse than the problem. \verb|...| is the inline answer: everything between the two identical delimiters is printed as-is, spaces preserved, with no escaping and no command interpretation. The constraints are all consequences of how it is implemented (an active delimiter character plus catcode changes): | cannot be the delimiter of a string containing |; \verb cannot appear in the argument of another command, so no \verb in a section heading, a caption, a footnote or a moving argument; and it cannot span a paragraph.
verbatim lifts those limits inside its own environment, so \begin{verbatim}...\end{verbatim} can hold anything — but the environment itself is still a command, so it cannot be nested inside another command's argument and its content may not contain a line that is exactly \end{verbatim}. The starred form verbatim* keeps the material out of the .aux when it is inside a moving argument, and \begin{verbatim} within a file you plan to \\input is safe because the block is read by the same catcode machinery.
For anything you want styled, framed or line-numbered, listings is the tool: it has its own (weaker) escape rules, preserves spaces, and can highlight. Inside ordinary prose the fallbacks are \texttt{} with each character escaped by hand, and the plain-TeX \string\frac, which prints the characters of a control-sequence name instead of running it — the trick behind any macro that has to show its own output.
One trap repeats across all three: a \ in a verbatim block is an ordinary character, so the block cannot contain the delimiter you chose, and — more importantly — \end{verbatim} inside it is read as the end of the environment. If your sample has to contain a literal \end{, choose listings, which reads its argument differently.
% Listings of un-typesettable material: the four tools, weakest first.
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{verbatim}
\usepackage{listings}
\begin{document}
One character or one short run inside a sentence: \verb|a_b#c|, and with a
different delimiter so that a bar can be printed, \verb+#&_|+.
A whole block, unsanitised:
\begin{verbatim}
for i in range(10): # % $ & _ { } ^ ~ all arrive untouched
print(f"{i}\t{i^2}")
\end{verbatim}
The starred form \verb*|trailing space | is the one that shows blanks as
visible boxes, which is what you want when the point of the sample is a
space. The \verb|verbatim| environment cannot appear inside another
command's argument, and \verb|\verb| cannot appear in a section heading, a
caption or a footnote: those arguments are moved, and \verb|\protect| does
not rescue either of them. For code that needs a frame, a language or line
numbers, use the \texttt{listings} package:
\begin{lstlisting}[basicstyle=\ttfamily\small,frame=single]
SELECT a, b FROM t WHERE a LIKE '%x_' -- % and _ stay literal
\end{lstlisting}
One asymmetry to remember: inside \texttt{lstlisting} the percent sign is
still just a character, but the environment reads its own closing line, so a
sample that has to contain \verb|\end{lstlisting}| needs a different tool.
\end{document}
Characters beyond Latin: engines, encodings and the font that must exist
Three layers have to agree for a non-Latin character to appear: the bytes in your file (UTF-8 by default), the encoding the font is selected in, and a font that actually owns the glyph. pdfLaTeX can do real work here, but each script needs its own encoding table and its own fonts — T2A for Cyrillic with the lh families, LGR for Greek with the cbgreek fonts — plus a babel language to move hyphenation and spelling with it: \usepackage[T2A,T1]{fontenc} and \usepackage[russian,english]{babel}, after which \foreignlanguage{russian}{\dots} switches for one passage.
For anything else — CJK, Arabic and Hebrew, Devanagari, IPA, emoji — the 8-bit route is a dead end and the answer is the Unicode engines: XeLaTeX or LuaLaTeX with fontspec, plus polyglossia or the modern babel for language-specific behaviour. There the character is chosen by name from the OpenType file you point at, hyphenation and direction come from the language module, and \newfontfamily\cjkfont{Noto Serif CJK SC} is one line instead of an encoding project. Bidirectional text needs the same route: bidi is implemented in those engines, not in pdfLaTeX.
The failure modes are always the same two, and both are log messages. Missing character ... in font means the encoding was fine and the font is not: pick a broader font, or a font with the right OpenType coverage. Package inputenc Error: Unicode character … not set up for use with LaTeX means the opposite: the font may have the glyph but no mapping exists in the 8-bit input layer, which is the pdfLaTeX wall — and the way over it is the engine switch, not another package.
One hybrid case is worth naming, because it is the most common in scientific text: a symbol that exists in no text font at all — a checkmark, an arrow, an estimated sign. The maths fonts carry many of them (\checkmark from amssymb, \leftarrow and friends), which is why \ensuremath{\checkmark} inside a text macro is a legitimate pattern, and why pifont's \ding{51} and \ding{55} exist for the ones maths does not have.
The table, and three rules that end most of these bugs
The listing in this section is the whole escape set in one compilable file: the eight backslash escapes, the three \textasci… commands, the text-comp extras for bar and angle brackets, every accent command against its own output, a ligature-breaking \textcompwordmark, and two file names quoted with \verb so that their underscores and backslashes survive. Keep it as the starting point of a document rather than as a page to scroll.
Rule one: when a character disappears, is swapped or comments out your line, it is one of the ten, and the fix is the escape for that character, not a package. Rule two: when a character error mentions a font or a category code, it is not one of the ten and the fix is encoding, engine or font. Rule three: when the text is code rather than prose, do not escape it at all — \verb, verbatim or listings, chosen by whether it needs to live inside an argument.
The two failure modes that follow from ignoring those rules are the ones in the mistakes list: a percent sign that eats the end of a line and produces an error two lines away, and an unescaped & or # that TeX interprets as a real request — the first inside a table cell that then reports a wrong column count, the second inside a \newcommand body that then demands a doubled hash.
And if your document has a mixture of all of this — currency, a few accented names, a code fragment, a Cyrillic quotation — write it in UTF-8, load T1 with lmodern, keep amssymb and textcomp available, and compile with latexmk. Every one of the three listings on this page is a complete file on that preamble and compiles as it stands.
\documentclass[11pt]{article}
\usepackage[T1]{fontenc} % precomposed accented glyphs, hyphenation works
\usepackage{lmodern} % a vector font with the T1 glyphs filled in
\usepackage{textcomp} % \texteuro, \textbar, \textless, \textcompwordmark
\usepackage{amssymb} % \checkmark
\usepackage{hyperref}
% UTF-8 input has been the default since the 2018 LaTeX release, so this
% document can contain é, ç, ø and 漢字 directly; the \accent forms below are
% shown because they still work and because diffs are easier to read.
\begin{document}
\section*{The escapes}
100\% of the invoice is \$100; the contract is between Smith \& Jones; the
file \verb|report_final.pdf| lives in
\texttt{C:\textbackslash tmp\textbackslash work}. Note the caret
\textasciicircum, the tilde \textasciitilde, the grave \textasciigrave, the
bar \textbar{} and the angle brackets \textless x \textgreater.
Curly braces need a backslash: \{ \} --- and the em dash is three hyphens,
the en dash two. Quotation marks open with two backticks and close with two
apostrophes: ``like this'', and a single quote is `like this'.
\section*{Accents, both routes to the same glyph}
The left column is quoted with \verb|\verb| so that it prints as source; the
right column is the same characters let through by the encoding layer.
\begin{tabular}{@{}ll@{\hspace{2em}}ll@{}}
\textbf{Source} & \textbf{Result} & \textbf{Source} & \textbf{Result} \\
\verb|\'e| & \'e & \verb|\={a}| & \={a} \\
\verb|\`a| & \`a & \verb|\b{o}| & \b{o} \\
\verb|\^o| & \^o & \verb|\d{n}| & \d{n} \\
\verb|\"u| & \"u & \verb|\H{o}| & \H{o} \\
\verb|\~n| & \~n & \verb|\r{a}| & \r{a} \\
\verb|\v{c}| & \v{c} & \verb|\u{g}| & \u{g} \\
\verb|\c{c}| & \c{c} & \verb|\k{a}| & \k{a} \\
\verb|\.z| & \.z & \verb|\t{oo}| & \t{oo} \\
\verb|\o| & \o & \verb|\L| & \L{} \\
\verb|\ss| & \ss & \verb|\ae| & \ae \\
\verb|é ç ø í| & UTF-8 input & \verb|f\textcompwordmark i| & no ligature: f\textcompwordmark i \\
\end{tabular}
\section*{Where they bite}
A percent sign with no backslash comments out the rest of the line, so an
unclosed brace two lines later is the error you get: % this is invisible now
Ampersands belong to tables: \& in prose, and in maths only inside an
alignment. A hash sign outside a macro definition is never what you meant,
and inside a \verb|\newcommand| body a literal hash has to be doubled.
\end{document}
Lesson cheat sheet
One key line of code from each section — jump to it or copy it straight from here.
\begin{array}{c|l|l}\text{source} & \text{role in TeX} & \text{to print it} \\\hline \texttt{\textbackslash\%} & \text{comment} & \text{percent} \\ \texttt{\textbackslash\$} & \text{math shift} & \text{dollar} \\ \texttt{\textbackslash\&} & \text{alignment tab} & \text{ampersand} \\ \texttt{\textbackslash\#} & \text{parameter} & \text{hash} \\ \texttt{\textbackslash\_} & \text{subscript} & \text{underscore} \\ \texttt{\textasciicircum} & \text{superscript} & \text{caret} \\ \texttt{\textasciitilde} & \text{active} & \text{tilde} \\ \texttt{\textbackslash\{\ \textbackslash\}} & \text{grouping} & \text{braces}\end{array}
\documentclass[10pt]{article}
\acute{e} \quad \grave{a} \quad \hat{o} \quad \check{c} \quad \tilde{n} \quad \bar{a} \quad \dot{x} \quad \ddot{u} \quad \mathring{a} \quad \breve{g}
\documentclass[11pt]{article}
Common mistakes
- A bare
%in running text or in a formula: it comments out the rest of the line, so the error TeX reports is on a later line about an unclosed brace. Write\%. - Typing
&in prose and readingMisplaced alignment tab character &as a table bug. Outside an alignment the character is illegal, and\&is the answer. - A literal
#inside a\newcommanddefinition: the body is scanned for parameter tokens, so the hash must be doubled as##. In ordinary text it is\#. - Expecting
\_to work inside maths. It is a text-mode command, so print the character from maths with\text{\_}(amsmath) — and note the other direction too, because a bare_in running text is silently dropped rather than set as an underscore. - Putting
\verb|...|in a section heading, a caption or a footnote: it is not robust in moving arguments, so either use\texttt{}with escapes or loadcprotect. - Typing quotes as
"this"and dashes as-, then blaming the font.``this'',--and---are the intended forms. - Adding
\usepackage[utf8]{inputenc}to fix an accented-letter problem on a modern installation: UTF-8 is already the default, and the real gap is usually the font encoding —\usepackage[T1]{fontenc}pluslmodern.
Questions about this lesson
How do I type a percent sign in LaTeX?
How do I type an underscore in LaTeX?
\text{\_} with amsmath loaded. The same asymmetry applies to ^ versus \textasciicircum.Why does LaTeX say misplaced alignment tab character?
How do I write accents in LaTeX, like é or ñ?
\hat, \tilde, \bar, \vec, \dot, \ddot, \check, \breve, \mathring.How do I print LaTeX code inside a LaTeX document?
\verb|...|, blocks in the verbatim environment, and anything that needs a frame, line numbers or a language goes in listings. The limitation to remember is that \verb cannot live in the argument of another command, so headings, captions and footnotes need \texttt{...} with escaped characters instead.Why is my less-than sign missing from the PDF?
\usepackage[T1]{fontenc} with lmodern, or use the encoding-independent \textless and \textgreater from textcomp. In maths the symbols are \lt and \text{<}, not the raw character.Live playground: edit the LaTeX, see it render
The preview updates as you type — break it on purpose to see how errors look.
Keep reading
Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.