1032
  • How does one insert a "\" (backslash) into the text of a LaTeX document?
  • And how does one insert a "~" (tilde)? (If you insert \~, it will give you a tilde as an accent over the following letter.)

I believe \backslash may be used in math formulae, but not into text itself. Lamport's, Kopka's, and Mittelbach's texts have said as much (but no more), and so left me hanging on how to get a backslash into regular text.

6

10 Answers 10

1055

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.

6
  • 34
    This is pretty awesome and helped me a lot. I just want to add that in case you use these in a text, do like so: bar\textasciitilde{}foo.
    – Till
    May 14, 2011 at 20:53
  • 5
    When Zotero exports a bibtex file, it replaces some tildes with {\textasciitilde}. When using biblatex (using the bibtex backend in texlive2007 [don't ask]), this causes an error (Missing $ inserted). The workaround is to use {\textasciitilde{}}.
    – naught101
    Jul 18, 2012 at 3:38
  • 13
    There's also detexify.
    – beatgammit
    Sep 7, 2012 at 3:23
  • 4
    And there is write-math.com. I've just tried it: Tilde and backslash. It works :-) (it's my bachelors thesis - currently still in progress) May 19, 2014 at 18:54
  • 3
    This answer is misleading, \textascitilde will produce a tilde from the current font in font encodings that have tilde. It only produces the tilde accent over an empty base in the legacy OT1 encoding Jan 4, 2022 at 19:27
329

Canonical answer

There’s now an extensive discussion with a canonical answer on this website. Use the solution described there. tl;dr:

For a URL or file path, use url (or hyperref). Otherwise:

  1. If you are using the standard Computer Modern font, txfonts or pxfonts: use this workaround:

     \usepackage{textcomp}
     \newcommand{\textapprox}{\raisebox{0.5ex}{\texttildelow}}
    

    … or use Latin Modern fonts instead (see next point).

  2. If you are using a PostScript/Type1 font via a package such as mathptmx or lm: Set the font encoding to T1 (via \usepackage[T1]{fontenc}) and use \textasciitilde.

  3. If you are using a TTF or OTF font via XeTeX or LuaTeX: Use \char`~ to insert a tilde.

Obsolete answer, preserved for posterity

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.

17
  • 5
    @nnyby: The best solution for that is to use the “raw” glyph, i.e. \texttt{\char`\\}. Mar 11, 2010 at 9:42
  • 182
    all of this: {\raise.17ex\hbox{$\scriptstyle\sim$}} just to type a proper ~. And LaTeX is supposed to be allow you to focus on contents...
    – Vivi
    Jun 6, 2010 at 9:15
  • 45
    @Vivi: The point of (La)TeX is that you can focus on content by defining macros. You never need to (or should!) type the above – except once, in a macro definition. You could even define an active character so that ~ will actually insert a proper tilde. That said, you’re certainly right about this particular instance: not providing a 1:1 mapping to all Unicode characters (heck, not even ASCII) is a major weakness of LaTeX. Jun 6, 2010 at 9:50
  • 27
    This was a great solution. Thank you. Though I would recommend putting it into its own new command like: \newcommand{\mytilde}{\raise.17ex\hbox{$\scriptstyle\mathtt{\sim}$}} so you can simple write \mytilde. Jul 29, 2011 at 3:25
  • 3
    To define ~ as an active character so that you can use it directly add the following to your preamble: \catcode`~ = 13 \def~{{\raise.17ex\hbox{$\scriptstyle\sim$}}}
    – MoRe
    Sep 11, 2019 at 7:37
106

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`\\}
9
  • 2
    rather \newcommand{\backslash}{\char`\\}
    – Svante
    Nov 3, 2008 at 0:39
  • 24
    Eh? The macro name can be whatever you like. The braces around it are optional. And \backslash is already defined as a math entity. Nov 3, 2008 at 22:17
  • 3
    This is my preferred solution for backslash, as it suits the font of the surrounding environment (for example, you can use it in \texttt).
    – mgiuca
    Sep 30, 2011 at 3:21
  • 1
    @Greg — no need for the braces. Aug 10, 2019 at 22:17
  • 1
    For backslash, I get different results running this in math mode versus out of math mode. In normal mode it outputs a double quote instead of a backslash. This is with a fresh install & a minimal tex file, so there must be something else I'm not doing.
    – jpaugh
    Dec 7, 2020 at 11:52
67

I occurs to me that you might be trying to type URLs. In that case, the url package takes care of everything for you:

\usepackage{url}
...
\url{somewhere\home\~will}

For paths (i.e., local files), there is \path, working the same way as \url, just providing the correct links in case hyperref is loaded.

If you are loading hyperref there is no reason to load url as well.

4
  • Thank you Will, I was trying to typeset teletype backslash (thick one) in new command. I've found way how to solve it but using url package is much more elegant.
    – Crowley
    Jun 2, 2011 at 11:38
  • Unfortunately the produced output is not clickable anymore, neither in DVI nor PDF. Any way to make it clickable? Dec 3, 2017 at 19:50
  • @DmitriZaitsev — I don't know what you mean by clickable. Are you loading the hyperref package? You don't need to load url separately in that case. Dec 4, 2017 at 4:35
  • @WillRobertson Thank you for clarifying, I have managed to make it clickable (so that clicking on it opens the page in a browser) by loading the hyperref package instead of the url package but still using \url{...} rather than href{...} Dec 4, 2017 at 13:05
63

Well if that isn't annoying:

 \textbackslash
 \texttt{\char`\~} or $\sim$
3
  • BMH or someone else: please edit if you have better answers for the tilde.
    – Federico Ramponi
    Nov 2, 2008 at 4:27
  • 3
    Don't use \verb, because it will break in fragile arguments. \texttt{\char`\~} would be the better choice. (See my answer elsewhere on this page.) Jan 21, 2011 at 0:32
  • @Will: Fixed that. Jan 21, 2011 at 0:47
18

Hmm; \textbackslash (mentioned by others) isn't in my reference book (Kopka and Daly).

At any rate, math mode provides \sim, \backslash, and \setminus (the latter two appear to look the same and differ only by spacing in math mode).

My LaTeX book – which, as you would expect, features the \ extensively – seems to use the verbatim environment. For example, this code:

\begin{verbatim}
 \addtocounter{footnote}{-1}\footnotetext{Small insects}
 \stepcounter{footnote}\footnoteext{Large mammals}
\end{verbatim}

Produces this text in the book:

 \addtocounter{footnote}{-1}\footnotetext{Small insects}
 \stepcounter{footnote}\footnoteext{Large mammals}

The \verb command is similar, but the argument must be on one line only. The first character after the b is the delimiter; for example:

\verb=\emph{stuff}=

will produce

\emph{stuff}

So you could presumably get your backslash by typing:

\verb=\=

You can also add a * – i.e. \verb* or \begin{verbatim*} – to make whitespace visible.

It is interesting to speculate how you would get an example of a verbatim environment into a document.. (using \verb to do the last line, I guess)

3
  • 4
    I guess the problem with using \verb is that it breaks inside macro arguments. You can't write \section{\verb=~=}, for example. Nov 3, 2008 at 22:20
  • Kopka's LaTeX books are very old. Originally they were written for LaTeX 2.09 and the later editions are only less modified for LaTeX2e. So they are not the best description of LaTeX2e and at least not of packages available for LaTeX2e. \textbackslash for example has been described at LaTeX2e for authors by the LaTeX team. Nov 20, 2011 at 13:01
  • \textbackslash is on page 536 of the fourth edition of Kopka and Daly Jul 25, 2015 at 10:50
9

For the tilde, you can use empty curly brace pair. That puts the "over the letter" tilde over an "empty" letter, so it's placed upward.

My tilde\~{}here
1
  • 1
    I guess it depends what the OP is intending to do with the tilde. Sometimes a "naked" tilde is a little unobtrusive. Nov 3, 2008 at 22:19
6

From https://faculty.math.illinois.edu/~hildebr/tex/bibliographies.html

"... tilde symbol (~), which without special coding gets interpreted as a blank space, and therefore needs to be escaped by a backslash (\~) or replaced by the math "twiddle" symbol $\sim$. Fortunately, there is a package, url, that provides a painless way to typeset URL's. To use this package available, add

\usepackage{url}

near the beginning of the document, and enclose any web and email addresses in the document in \url{...}:

\url{http://www.math.drofnats.edu/~gauss}
\url{[email protected]}

..."

I hope this could help you in typewriting a regular tilde character.

5

Personally, I learned more actually changing the catcodes myself :)

\begingroup
  \catcode `~=11
  \gdef\mytilde{~}
  \catcode `\|=0
  \catcode `\\=11
  |gdef|mybs{\}
|endgroup

and then something like

This is a tilde: \mytilde
This is a backslash: \mybs
3
  • 2
    \def\mybs{\char092} does the same for the backslash
    – user2478
    Aug 29, 2012 at 15:03
  • @Herbert: True, but more cryptic ...
    – Jonathan
    Aug 29, 2012 at 15:23
  • 1
    ok, then we use \string\ for non cryptic ...
    – user2478
    Aug 29, 2012 at 15:38
2

$\sim$ works for me in Plain TeX.

Eg. ~70 can be rendered as

$\sim$70

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .