Special characters in LaTeX: percent, underscore, ampersand, backslash
Ten characters have a special meaning in LaTeX: #, $, %, &, _, {, }, ~, ^ and the backslash. Seven of them are printed with a backslash in front, for example \% and \_. The backslash, the tilde and the caret have commands of their own.
Seven characters with a backslash
\#, \$, \%, \&, \_, \{ and \} print the character itself.
50\% of \$20, item \#3, R\&D, file\_name and \{braces\}Backslash, tilde and caret
These three need a command, because \\ is a line break and \~ and \^ put an accent on the next letter. \textbackslash prints a backslash, \textasciitilde a tilde and \textasciicircum a caret.
C:\textbackslash Users, \textasciitilde user and 2\textasciicircum 10Less than, greater than and the vertical bar
In the default font encoding, <, > and | in normal text print an inverted exclamation mark, an inverted question mark and a dash. Use \textless, \textgreater and \textbar, or load \usepackage[T1]{fontenc}, after which the keys print the right characters. In math mode the three keys always work.
a \textless{} b \textgreater{} c \textbar{} d and $a < b > c$Print code and paths unchanged
\verb prints everything between two equal delimiter characters as typed, in typewriter font. The delimiter can be any character that does not occur in the text, here |.
The file is \verb|C:\Users\name_1\notes#2.txt|.Pitfalls
- An unescaped
%gives no error: LaTeX silently drops the rest of the line as a comment. - The other characters stop the run:
&with Misplaced alignment tab character &,#with You can't use `macro parameter character #' in horizontal mode, and_or^with Missing $ inserted. \verbdoes not work inside the argument of another command such as\sectionor\footnote. See Inline code in LaTeX.- For web addresses use
\url{...}from theurlorhyperrefpackage. Inside it,_,~,&and#need no backslash.
Compiled with pdfLaTeX from TeX Live 2023.