Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
  • 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.

share|improve this question

migrated from stackoverflow.com Jan 20 '11 at 17:54

This question came from our site for professional and enthusiast programmers.

1  
For the special case where a backslash or tilde must be written to an auxiliary file or shell escape see: How can I provide a verbatim (unescaped) commandline for executing with \write18? –  Martin Scharrer Apr 13 '12 at 10:28

9 Answers 9

up vote 349 down vote accepted

The Comprehensive LaTeX Symbol List is your friend.

\textbackslash and \textasciitilde are found in Table 2 of the list, and page 101 has some other options for the tilde:

$\sim$ and \texttildelow from the textcomp package, possibly using some font other than Computer Modern to get a nice vertically centered tilde

It also suggests using the url package if you are typesetting URL's or Unix file names.

share|improve this answer
3  
Brilliant reference; thanks you! –  Brian M. Hunt Nov 2 '08 at 14:47
8  
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
1  
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 3:38
6  
There's also detexify. –  tjameson Sep 7 '12 at 3:23
    
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) –  moose May 19 at 18:54

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.

share|improve this answer
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? –  nnyby Mar 11 '10 at 4:35
2  
@nnyby: The best solution for that is to use the “raw” glyph, i.e. \texttt{\char`\\}. –  Konrad Rudolph Mar 11 '10 at 9:42
60  
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 '10 at 9:15
24  
@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 6 '10 at 9:50
7  
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 3:25

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}
share|improve this answer
    
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 '11 at 11:38

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`\\}
share|improve this answer
    
rather \newcommand{\backslash}{\char`\\} –  Svante Nov 3 '08 at 0:39
10  
Eh? The macro name can be whatever you like. The braces around it are optional. And \backslash is already defined as a math entity. –  Will Robertson Nov 3 '08 at 22:17
2  
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 '11 at 3:21

Well if that isn't annoying:

 \textbackslash
 \texttt{\char`\~} or $\sim$

Thank for reading. :)

Edit: added code for tilde.

share|improve this answer
    
BMH or someone else: please edit if you have better answers for the tilde. –  Federico Ramponi Nov 2 '08 at 4:27
2  
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.) –  Will Robertson Jan 21 '11 at 0:32
    
@Will: Fixed that. –  Brian M. Hunt Jan 21 '11 at 0:47

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)

share|improve this answer
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 3 '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

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
share|improve this answer
1  
I guess it depends what the OP is intending to do with the tilde. Sometimes a "naked" tilde is a little unobtrusive. –  Will Robertson Nov 3 '08 at 22:19

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
share|improve this answer
2  
\def\mybs{\char092} does the same for the backslash –  Herbert Aug 29 '12 at 15:03
    
@Herbert: True, but more cryptic ... –  Jonathan Aug 29 '12 at 15:23
1  
ok, then we use \string\ for non cryptic ... –  Herbert Aug 29 '12 at 15:38

From http://www.math.uiuc.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.

share|improve this answer

protected by Martin Scharrer Apr 5 '12 at 7:01

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.