# Correctly typesetting a tilde

How do I set the tilde (~) character correctly in LaTeX?

There’s a question discussing that on Stack Overflow, with a lot of answers. But dare I say it? They all suck:

• \textasciitilde is too high,
• \texttildelow is too low.
• $\sim$ can be tweaked to look more or less acceptable: {\raise.17ex\hbox{$\scriptstyle\sim$}} – but even that is a hack and uses the wrong symbol.

The point is – the tilde character is definitely an existing glyph in any font I am tempted to use. How can I just access and typeset that character, please? Without resorting to some other, similar-looking glyph that first needs to be nudged into place using a cunning combination of font size and raised boxes?

There must be a possibility to access that glyph directly from the font definition.

To demonstrate, look at this screenshot which attempts simply to display a C++ destructor of a type T (~T):

Compare this with the symbol we get in the browser: ~that’s what I want.

-

It all depends on what do you want this glyph to stand for.

If you want to use it in an url then add \usepackage{url} (or hyperref) to your preamble and then use \url{http://example.com/~user}.

If you want to use it inside a math formula as some kind of relation then use $a \sim b$.

According to the "Comprehensive Symbols List", to get a vertically centered ~ you can use \texttildelow in any font other than Computer Modern, txfonts, and pxfonts. For example the following does the trick for typesetting a C++ destructor

\usepackage{times}
\usepackage{textcomp}

\texttildelow T

-
Yes, I should have excluded URIs and math mode from the question. But the \text… variants are definitely not using the correct symbol. –  Konrad Rudolph Jul 27 '10 at 9:40
Can I ask again "In which context to you want the symbol to appear?" –  Juan A. Navarro Jul 27 '10 at 10:00
@juan: well, in normal text. To give a concrete example, consider source code, e.g. when using a C++ destructor, ~T(). –  Konrad Rudolph Jul 27 '10 at 10:37
Thanks. I see now that I need to ask a new question – because this solution still doesn’t work with XeTeX in conjunction with TrueType fonts. –  Konrad Rudolph Jul 27 '10 at 13:54
Uh, scratch that. For TTF/OTF fonts, \char~ works. –  Konrad Rudolph Jul 27 '10 at 14:01
show 1 more comment

Here’s a list for completeness’ sake.

If you want to typeset a URL or file path, use the url (or hyperref) package with the appropriate command and be done with. Otherwise, there are three distinct cases:

1. You are using the standard Computer Modern font, txfonts or pxfonts: use DocBucket’s workaround:

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

2. You are using a T1 font via a package such as times: use texttildelow from the textcomp package (no workaround needed; in particular, do not load lmodern, otherwise this won’t work any longer).

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

This is a bug in Computer Modern. \textasciitilde is the correct character and should be rendered vertically centred. To fix it, the best way is to use Latin Modern with T1 enconding, as in


\documentclass{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}

\begin{document}
\textasciitilde T
\end{document}


This also works with any font that I bothered to try, like mathpazo, times, berasans, inconsolata, etc.

EDIT: Rewrote answer in response to comment.

-
This is the correct answer. –  Mk12 Aug 25 '12 at 18:07
Actually this doesn’t seem to work. An MWE, loading the article document class, lmodern and trying to typeset \textasciitilde{}T produces the wrong tilde (high up). –  Konrad Rudolph Jul 8 at 8:28
You're right. The problem is that I have never thought about using Latin Modern with legacy enconding. You must use \usepackage[T1]{fontenc} to have correct results. –  Mateus Araújo Jul 30 at 13:25

according to the Comprehensive LaTeX Symbol List, \texttildelow "produces a vertically centered “~” in most fonts but a baseline-oriented “~” in Computer Modern, txfonts, pxfonts, and various other fonts originating from the TEX world." So to produce a centered Tilde, one can use something like

{\fontfamily{ptm}\selectfont\texttildelow}T()


(Taken from the symbol list source code. A different font might give a better result, depending on what the main font of your text is.)

-

Every time I google the tilde problem, this question comes up. I cannot be the only one finding the link this way.

In that case, here's the simple fix (edit: for the Computer Modern font) that I cannot believe I have yet to see anywhere else. Note that most of the code below is a MWE; the fix itself is only two lines (one line if you already use textcomp)

\documentclass{article}

\usepackage{textcomp}

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

\begin{document}

\section{Unsatisfying fixes}

Using \verb+\texttildelow{}12+ (with the \texttt{textcomp} package) gives:
\begin{center}
My dog weighs \texttildelow{}12~kg.
\end{center}
Using \verb+\url{~12}+ (with the \texttt{url} package) gives:
\begin{center}
My dog weighs \url{~12}~kg.
\end{center}
Using \verb+$\sim$12+  gives:
\begin{center}
My dog weighs $\sim$12~kg.
\end{center}

\section{My fix}
In the preamble:
\begin{verbatim}
\usepackage{textcomp}
\newcommand{\textapprox}{\raisebox{0.5ex}{\texttildelow}}
\end{verbatim}
Using \verb+\textapprox{}12+ gives:
\begin{center}
My dog weighs \textapprox{}12~kg.
\end{center}
That looks about right to me. If you want to change the height, it's easy to do so globally by tweaking the \texttt{0.5ex} value.
\end{document}

-
Actually, \texttildelow by itself should work just fine with T1, no “raisebox” needed, as long as you are not using the CM font (your solution works for CM though). If you are using a TTF font, then \char~ should work. It’s not very clear but it’s written in a comment above. –  Konrad Rudolph Jul 8 at 7:06
Oh, I should have mentioned that I was specifically talking about CM. Do most people switch into a different font? Maybe it's because I'm new to LaTeX, but I still rather like CM so I'm currently writing with it. –  DocBuckets Jul 8 at 14:00
Well I cannot judge, I dislike the look of CM so I never use it. But my comment wasn’t meant as discouragement. This is actually a solid answer, in particular since the lmodern workaround, mentioned above, doesn’t work. –  Konrad Rudolph Jul 8 at 14:03

This problem was also driving me crazy for typesetting code in an lstlisting. I solved it with

\usepackage{url}
\usepackage{listings}
\lstset{literate=%
{~}{\url{~}}1
}
`

Most of the other answers here either don't work in a listing, or require more gymnastics than I was ready to undertake.

-