tags:

views:

70117

answers:

9
  • How does one insert "\" (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.

Thanks!

Edit: added question for tilde.

+10  A: 

Well if that isn't annoying:

  • \textbackslash
  • \verb+~+ (small one, at the top), or $\sim$ (big one)

Thank for reading. :)

Edit: added code for tilde.

Brian M. Hunt
BMH or someone else: please edit if you have better answers for the tilde.
Federico Ramponi
+53  A: 

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), and a suggestion to use the url package if you are typesetting URLs or Unix file names.

Jouni K. Seppänen
Brilliant reference; thanks you!
Brian M. Hunt
+5  A: 

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`\\}
Will Robertson
rather \newcommand{\backslash}{\char`\\}
Svante
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
+1  A: 

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

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

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)

John Fouhy
I guess the problem with using \verb is that it breaks inside macro arguments. You can't write \section{\verb=~=}, for example.
Will Robertson
+2  A: 

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
I guess it depends what the OP is intending to do with the tilde. Sometimes a "naked" tilde is a little unobtrusive.
Will Robertson
+4  A: 

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}
Will Robertson
A: 

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
This should be a comment on Will Robertson's answer.
Brian M. Hunt
I'm sorry for that, but it was and still is for me and other newbies only way to comment anything in this track.And I also thought my comment would be useful for others who has same problem as me.
Crowley
Fair point - thank you for posting it.
Brian M. Hunt
A: 

I was trying to put a tilde into a website using

\url{http://somewhere.com/~myusername/}

but adobe reader would automatically make a link that would be broken because it would use the wrong tilde character. I ended up using

\usepackage{textcomp}
...
\texttt{http://somewhere.com/{\texttildelow}myusername/}

That did the trick for what I was doing. Of course if one wanted to make an actual link then one would use the hyperref package.

Steve
+10  A: 

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
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
@nnyby: The best solution for that is to use the “raw” glyph, i.e. \texttt{\char`\\}.
Konrad Rudolph
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
@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
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