Correctly typesetting a tilde

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. If you are using the standard Computer Modern font, txfonts or pxfonts: use DocBucket’s 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.

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

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.

Tags:

Symbols

Tilde