How does one insert a backslash or a tilde (~) into LaTeX?

TL;DR

\textbackslash produces a backslash in text-mode. The math-mode $\sim$ and \texttildelow (from textcomp package) are options for a lower tilde (while ~{} and \textasciitilde produce a raised tilde in text-mode)


Long Answer:

The Comprehensive LaTeX Symbol List is your friend. The correct link seems to keep changing, but if you have a complete TeX Live installation, the command texdoc symbols-a4 will display your local copy.

\textbackslash and \textasciitilde are found in several places in the document, but the LaTeX 2e ASCII Table (Table 529 as of this writing) and the following discussion are a convenient resource for all ASCII characters. In particular, the discussion notes that ~{} and \textasciitilde produce a raised tilde, whilst the math-mode $\sim$ and \texttildelow are options for a lower tilde; the latter is in the textcomp package, and looks best in fonts other than Computer Modern. If you are typesetting file names or urls, the document recommends the url package.

Remember to delimit TeX macros from surrounding text, e.g. bar\textasciitilde{}foo.


Canonical answer

There’s now an extensive discussion with a canonical answer on this website. Use the solution described there. The text below should be considered obsolete.

Old answer, preserved for posteriority

textcomp’s \texttildelow is actually quite a bad choice: it’s too low for most fonts.

A much better rendering can be achieved by the following, which tweaks the appearance of the (otherwise too wide) $\sim$:

{\raise.17ex\hbox{$\scriptstyle\sim$}}

This was taken from the Arbitrary LateX reference … the page also provides a good comparison sheet:

Different tilde renderings

When used in \texttt, I would add a \mathtt around the tilde, to make it fit the font better:

{\raise.17ex\hbox{$\scriptstyle\mathtt{\sim}$}}

The difference is small but noticeable.


You can also use the "plain TeX" method of indexing the actual ascii character in the current font:

\char`\\
\char`\~

I often use the former for writing macros that need the backslash in the typewriter font; \textbackslash will sometimes still use the roman font depending on the font setup. Of course, if you're using these a lot you should define your own macro for them:

\newcommand\SLASH{\char`\\}

Tags:

Symbols