Special characters in LaTeX tables: escaping %, &, _, $ and #
Ten characters have a special meaning in LaTeX: # $ % & _ { } ~ ^ and the backslash. In a table cell, type them as \# \$ \% \& \_ \{ \} \textasciitilde{} \textasciicircum{} and \textbackslash{}. Put maths such as p < 0.05 or a real minus sign inside $...$. Characters pasted from PDFs, such as the Unicode minus U+2212, can also stop pdfLaTeX.
Convert a table from a PDF or screenshot →By TableToLatex · · Examples tested with pdfLaTeX, TeX Live 2023, article class
The characters that break a table
Ten characters have a special job in LaTeX, so they cannot be typed as plain text. In a table cell the most damaging are &, which separates columns, and %, which starts a comment. The others are $ # _ { } ~ ^ and the backslash. The table further down shows how to write each one, and what LaTeX does if you do not.
% preamble: \usepackage[T1]{fontenc} \usepackage{booktabs}
\begin{table}[htbp]
\centering
\caption{Escaped specials and inline math.}
\label{tab:escaped}
\begin{tabular}{lrr}
\toprule
Model & Params\_M & Gain (\%) \\
\midrule
R\&D baseline & 25.6 & $-0.5$ \\
Ours ($p < 0.05$) & 25.9 & $\geq 3.2$ \\
Rank \#1 & \textasciitilde{}30 & 50\% \\
Cost (\$5--\$10) & \{1, 2\} & $\pm 0.4$ \\
\bottomrule
\end{tabular}
\end{table}Maths needs dollar signs. A real minus sign, ≥, ± and an inequality such as p < 0.05 all go inside $...$. Characters pasted from a PDF are a separate problem. Several Unicode characters make pdfLaTeX stop with "Unicode character ... not set up for use with LaTeX". In tests the minus sign U+2212, ≥, the Greek letter alpha and the thin space U+2009 failed, while ±, ×, the degree sign, the en dash and curly quotes compiled.
% pasted from a PDF: stops pdfLaTeX write instead
% −0.5 (U+2212 minus) $-0.5$
% ≥ 5 (U+2265) $\geq 5$
% α = 0.1 (U+03B1) $\alpha = 0.1$
% 5 kg (U+2009 thin space) 5\,kgTwo cases fail silently. A tilde typed as ~3 prints only a space, and < and > in text print as inverted punctuation unless you use maths or the T1 font encoding. Spreadsheet exports are a common source of these; see Excel to LaTeX. Numbers in siunitx columns have their own rules, in the decimal alignment guide, and digits misread by OCR are covered in the proofreading checklist.
Clean a table in six steps
- 1
Escape the text characters
In every text cell and header, change & % $ # _ { } to \& \% \$ \# \_ \{ \}. Do the % and & first, because they cause the most confusing errors.
- 2
Put maths in dollar signs
Wrap p < 0.05, a real minus sign, ≥ and ± in $...$, as in $-0.5$ or $\geq 3.2$. Do not escape characters inside the dollars.
- 3
Replace pasted Unicode
Swap the minus sign U+2212, ≥, Greek letters and thin spaces for the LaTeX commands in the second listing.
- 4
Fix the angle brackets
Put < and > in math mode, or load \usepackage[T1]{fontenc}, so they do not print as ¡ and ¿.
- 5
Compile and read the first error
Later errors often cascade from the first. Look for a raw character in the row it names.
- 6
Search for survivors
Search for a bare %, & or _ you missed, and recompile until the log is clean.
Special characters and their fixes
| Character | Write it as | If you leave it raw |
|---|---|---|
| & | \& | Extra alignment tab has been changed to \cr |
| % | \% | The rest of the line becomes a comment, so the row's \\ is lost |
| $ | \$ | Missing $ inserted, or Extra }, or forgotten $ |
| # | \# | You can't use macro parameter character # in restricted horizontal mode |
| _ | \_ | Missing $ inserted |
| { } | \{ \} | Missing } inserted, or Missing { inserted |
| ~ | \textasciitilde{} or $\sim$ | No error: it prints a space |
| ^ | \textasciicircum{} | Missing $ inserted |
| \ | \textbackslash{} | Undefined control sequence |
| < > | | $<$ $>$ $|$, or fontenc T1 | Default encoding prints ¡ ¿ and a dash |
What goes wrong
- Percentages.
50%comments out the rest of the line. The error appears on a later line, which makes it hard to trace. - Dollar signs in one cell.
$5-$10is read as maths between the dollars, so in a test it compiled but printed 5−10 with the dollar signs gone. A single raw$stops the compile. Escape each dollar sign that means money. - Underscores in names.
Params_Mand identifiers with underscores need\_in text. - A short hyphen as a minus. A hyphen before a number looks too short. Use
$-0.5$. - Escaping inside maths. Escape only the text around dollar signs, not what is between them.
- A backslash in a path or name.
C:\tempgives "Undefined control sequence". Use\textbackslash.
How TableToLatex helps
TableToLatex escapes \ & % $ # _ { } ~ ^ in cells and headers, so Params_M and 50% arrive as Params\_M and 50\%, and text already inside $...$ is left as written. Common Unicode characters taken from the source are converted before escaping: a minus sign U+2212 becomes a hyphen-minus, an en dash between numbers becomes --, thin spaces become spaces, and ≥, ≤, ±, ×, ≈, →, the degree sign, µ and Greek letters become math commands such as $\geq$ and $\alpha$. Rarer symbols pass through unchanged and can still stop pdfLaTeX, and a cell with two dollar signs, such as $5-$10, is kept as maths and prints without them. Check for both before you compile. Try the LaTeX table generator. The table tool is free and runs in your browser. Paste a tab-separated range from a spreadsheet. See pricing.
Questions about special characters
How do I write a percent sign in a LaTeX table?+
Type \% in text. A bare % starts a comment, so everything after it on that line, including the \\ that ends the row, is ignored.
How do I type an ampersand in a table cell?+
Write \&. A bare & is the column separator, so LaTeX reports "Extra alignment tab has been changed to \cr".
Why does an underscore give Missing $ inserted?+
Outside maths, _ means subscript, which is only legal in math mode. Write \_ in text such as the header Params\_M. Inside $...$ it is a real subscript.
How do I write a negative number or minus sign?+
In a table of results put it in maths, $-0.5$, so LaTeX prints a proper minus rather than a short hyphen. In a siunitx S column a plain -0.5 is read as a number and printed with a minus.
How do I write less-than or greater-than in text?+
Use math mode, as in $p < 0.05$, or load fontenc with the T1 option. In a test, the default font encoding printed < as an inverted exclamation mark and > as an inverted question mark.
Why does pdfLaTeX reject a character copied from a PDF?+
Some Unicode characters, such as the minus sign U+2212, ≥ and Greek letters, are not set up in the default setup, and the error reads "Unicode character ... not set up for use with LaTeX". Replace them with commands, or try XeLaTeX or LuaLaTeX with a font that has the glyph.
How do I write a tilde that means about?+
A bare ~ is a non-breaking space, so ~3 printed only a space in a test. Write \textasciitilde{}3 or $\sim 3$.
Keep reading
- Excel to LaTeX
Export paths from spreadsheets to LaTeX and the pitfalls on each.
- Proofreading OCR tables
A checklist for digits, decimals, minus signs and columns in OCR'd tables.
- Decimal alignment
S columns, table-format, signs and ± without misaligned decimals.
Have a table in a spreadsheet? Paste it into the table tool, or see all LaTeX table guides.