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 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):

tilde symbols

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

share|improve this question

7 Answers 7

up vote 23 down vote accepted

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
share|improve this answer
    
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
1  
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
1  
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
4  
Uh, scratch that. For TTF/OTF fonts, \char`~ works. –  Konrad Rudolph Jul 27 '10 at 14:01

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.)

share|improve this answer

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.
share|improve this answer
    
+1 for XeLaTeX! –  Farticle Pilter May 28 at 15:33

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.

share|improve this answer
    
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 '13 at 8:28
3  
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 '13 at 13:25

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}
share|improve this answer
    
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 '13 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 '13 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 '13 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.

share|improve this answer

None of the existing answers really do the trick for me. In particular,

  • switching to another font (e.g. {\fontfamily{ptm}\selectfont\texttildelow}) hurts my stickler's heart;
  • using $\sim$ doesn't look good, especially in bold typewriter;
  • I'd really like a macro that typesets a vertically centred tilde regardless of the font and encoding used.

Here is a table summarising the tests I ran (mostly on TeX fonts):

enter image description here

The \midtilde macro is my attempt at typesetting a vertically centred tilde, regardless of the font being used. I haven't tested it on any other font than those listed in the table. Please let me know (in the comments) if you find fonts for which \midtilde doesn't produce a vertically centred tilde.

\documentclass{article}

\usepackage{booktabs}
\usepackage{pifont}
\usepackage[dvipsnames]{xcolor}
\newcommand{\cmark}{\textcolor{ForestGreen}{\ding{51}}}%
\newcommand{\xmark}{\textcolor{red}{\ding{55}}}%

\usepackage{textcomp}

\makeatletter

\newcommand\midtilde@raisedtilde[1][.5]{\raisebox{#1ex}{\texttildelow}}
\def\midtilde@normaltilde{\texttildelow}

\newcommand\midtilde%
{%
  \expandafter\in@\expandafter{\f@family}%
    {cmr,cmss,cmtt,cmm,cmsy,cmx,%
    lmr,lmss,lmtt,lmm,lmsy,lmx,%
    pxr,pxss,pxm,pxsy,pxx,%
    txr,txss,txm,txsy,txx}%
  \ifin@%
    \midtilde@raisedtilde%
  \else%
    \expandafter\in@\expandafter{\f@family}%
    {pxtt,txtt}%
    \ifin@%
      \midtilde@raisedtilde[.35]%
    \else%
      \midtilde@normaltilde%
    \fi%
  \fi%
}


\begin{document}

\section{Tests}

\newcommand\fontrow[2]{%
  \fontfamily{#1}\selectfont\f@family         &
  \fontfamily{#1}\selectfont A\texttildelow a & 
  #2                                          &
  \fontfamily{#1}\selectfont A\midtilde a      \\
}

\begin{tabular}{cccc}
  \toprule
    \texttt{Font family} & A\texttt{\string\texttildelow} a & centred? & A\texttt{\string\midtilde} a \\
  \midrule
    \fontrow{cmr}{\xmark}
    \fontrow{cmss}{\xmark}
    \fontrow{cmtt}{\xmark}
    \fontrow{cmm}{\xmark}
    \fontrow{cmsy}{\xmark}
    \fontrow{cmx}{\xmark}
  \midrule
    \fontrow{lmr}{\xmark}
    \fontrow{lmss}{\xmark}
    \fontrow{lmtt}{\xmark}
    \fontrow{lmm}{\xmark}
    \fontrow{lmsy}{\xmark}
    \fontrow{lmx}{\xmark}
  \midrule
    \fontrow{pxr}{\xmark}
    \fontrow{pxss}{\xmark}
    \fontrow{pxtt}{?}
    \fontrow{pxm}{\xmark}
    \fontrow{pxsy}{\xmark}
    \fontrow{pxx}{\xmark}
  \midrule
    \fontrow{txr}{\xmark}
    \fontrow{txss}{\xmark}
    \fontrow{txtt}{?}
    \fontrow{txm}{\xmark}
    \fontrow{txsy}{\xmark}
    \fontrow{txx}{\xmark}
  \midrule
    \fontrow{ptm}{\cmark}
    \fontrow{phv}{\cmark}
    \fontrow{pcr}{\cmark}
  \bottomrule
\end{tabular}
\makeatother

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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