1031
  • 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.

Scott H.
  • 11,047
Brian M. Hunt
  • 14,277

10 Answers10

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.

Skillmon
  • 60,462
Jouni K. Seppänen
  • 11,970
  • 2
  • 18
  • 11
  • 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 '11 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 '12 at 03:38
  • 13
  • 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) – Martin Thoma May 19 '14 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 – David Carlisle Jan 04 '22 at 19:27
  • whohoo it was so satisfying to push the votes from 999 to 1000, congrats! – math May 25 '22 at 08:03
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.

Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
  • 1
    I'm trying to get a backslash in a monospaced font. \texttt{\textbackslash} is giving me the wrong glyph. Do you have any suggestions? –  Mar 11 '10 at 04:35
  • 5
    @nnyby: The best solution for that is to use the “raw” glyph, i.e. \texttt{\char`\\}. – Konrad Rudolph Mar 11 '10 at 09: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 06 '10 at 09: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. – Konrad Rudolph Jun 06 '10 at 09:50
  • 1
    Don't get me wrong, I love LaTeX (I am learning Emacs because of LaTeX, and I spent quite some time playing with the package TikZ)!! I just found it ironic in this instance... – Vivi Jun 06 '10 at 13:11
  • 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. – Matthew Sowders Jul 29 '11 at 03:25
  • I would say \sim is not good to use in text mode. we can get a vertically centered tilde by using \raisebox{.4ex}{\hbox{\texttildelow}} or {\fontfamily{pcr}\selectfont\texttildelow}. These two command can get better rendered tilde in text mode. At least provide a comparison between the rendered results of these command for reader to choose from. – jdhao Feb 16 '17 at 09:02
  • @Hao That’s also a bad solution. There’s a canonical question/answer for this now: http://tex.stackexchange.com/q/312/42 – Konrad Rudolph Feb 16 '17 at 10:09
  • 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 '19 at 07:37
  • @KonradRudolph could an "active character" be used to solve this problem: https://tex.stackexchange.com/q/592688/143861 – minseong Apr 20 '21 at 13:18
  • @theonlygusti It could (see the immediately preceding comment), but ~ already is an active character with an altogether different meaning, it’s probably not a good idea to override that meaning since it’s fairly widely used. – Konrad Rudolph Apr 20 '21 at 13:22
  • @KonradRudolph I think you missed the question link in my comment, probably the active character would have to be ! with variable behaviour depending on what text is prefixed to it – minseong Apr 20 '21 at 13:28
  • @theonlygusti I didn’t miss the link, I just didn’t understand that you were seeking a solution to the issue in that question, because if so, why post it here? Where is the connection to this answer? At any rate, no, making ! an active character is unlikely to solve your issue since you can’t generally inspect preceding characters inside a macro expansion, as far as I know (if this is possible at all it’s bound to be very inefficient in the general case). That said, I haven’t programmed in LaTeX in a looong time. I might misremember. – Konrad Rudolph Apr 20 '21 at 13:36
  • {\raise.17ex\hbox{$\scriptstyle\sim$}} gave me the perfect result. – vahid-dan Feb 17 '22 at 04:03
  • @vahid-dan Hm. That’s actually the obsolete answer. It really shouldn’t be used any more — it was purely a hacky workaround before the old, buggy fonts were fixed. – Konrad Rudolph Feb 18 '22 at 09:46
  • @KonradRudolph How can I get the same output using non-hacky standard methods? I tried several other methods and the tilde is either too high, too low, or too wide. – vahid-dan Feb 19 '22 at 13:43
  • @vahid-dan Depending on your circumstances, one of the three initially described ways should work, and give the rendering of the tilde as intended by the authors of the typeface. The hack will give an imprecise approximation. – Konrad Rudolph Feb 19 '22 at 16:33
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`\\}
66

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.

  • 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 02 '11 at 11:38
  • Unfortunately the produced output is not clickable anymore, neither in DVI nor PDF. Any way to make it clickable? – Dmitri Zaitsev Dec 03 '17 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. – Will Robertson Dec 04 '17 at 04: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{...} – Dmitri Zaitsev Dec 04 '17 at 13:05
63

Well if that isn't annoying:

 \textbackslash
 \texttt{\char`\~} or $\sim$
Cadoiz
  • 213
Brian M. Hunt
  • 14,277
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)

yo'
  • 51,322
  • 4
    I guess the problem with using \verb is that it breaks inside macro arguments. You can't write \section{\verb=~=}, for example. – Will Robertson Nov 03 '08 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. – Schweinebacke Nov 20 '11 at 13:01
  • \textbackslash is on page 536 of the fourth edition of Kopka and Daly – Phil Molyneux Jul 25 '15 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
hayalci
  • 6,489
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{gauss@math.drofnats.edu}

..."

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
Jonathan
  • 2,310
2

$\sim$ works for me in Plain TeX.

Eg. ~70 can be rendered as

$\sim$70