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.

  • I know this is already answered. But i have to point out that the tilde character (Unicode 0x7E) just so happens to have a "high" glyph in the Computer Modern font fontsquirrel.com/fonts/computer-modern. That's a deliberate design choice for the tilde glyph. (Like how some fonts have a low J glyph.) The glyph you are looking for (to access and typeset that character, which is already there) is an other font with a "middle" glyph for tilde, or to "fake" it by using \raisebox. But +1 as i don't like the default design of ~ in CM either. – garyF Mar 1 at 20:51
up vote 40 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
  • 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
  • 2
    Can I ask again "In which context to you want the symbol to appear?" – Juan A. Navarro Jul 27 '10 at 10:00
  • 1
    @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
  • 11
    Uh, scratch that. For TTF/OTF fonts, \char`~ works. – Konrad Rudolph Jul 27 '10 at 14:01

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.
  • I'm using a font called iwona which I think is T1 and I can't get either \texttildelow or \char`~ to work. – gghuffer Apr 6 '16 at 2:52
  • Case 2 is wrong, just use \textasciitilde for T1 fonts, no package needed. This works with all T1 fonts, in particular with lmodern. – Mateus Araújo Apr 11 '17 at 11:26
  • @MateusAraújo I meant to write “Type1” (see the link! — I was talking about font formats, not encodings). Indeed it works if you set the font encoding to T1 (which isn’t done by default; instead, OT1 is used, and the tilde looks wonky). Anyway, I’ll edit this now. – Konrad Rudolph Apr 11 '17 at 12:29
  • Oh I see, I thought a T1 font necessarily had to use a T1 font encoding, but this is not the case, as these two "T1" are unrelated concepts that share the same name. Anyway, it's nice of you to fix your old answer. – Mateus Araújo Apr 11 '17 at 13:35

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.

  • 1
    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
  • 4
    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

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

  • a list of available font name can be found here – jdhao Feb 16 '17 at 8:44

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}
  • After \usepackage{newtxtext}, \midtilde sits far too low in either Roman or sans-serif text. I fixed this locally by adding t1ntxrx and t1ntxss to the longer of the two lists of font families. But perhaps this is better handled by reporting it as a bug against the newtxtext package? – Ben Liblit Oct 17 '14 at 23:06
  • It fails if you load cfr-lm. I think it is reasonable for cfr-lm to follow lmodern by default, so if you want this to be a general solution, I think the families defined by cfr-lm need to be accommodated in the same way as the other CM/LM variants. – cfr Feb 3 '15 at 1:50
  • Updating my first comment above, newtxtext 1.4 and later has switched to using TeXGyre Termes fonts, which put me back to seeing a \midtilde that sat far too low in either Roman or sans-serif text. I fixed this locally by adding t1ntxrj, t1qhv, and ts1qhv to the longer of the two lists of font families. – Ben Liblit Nov 19 '15 at 16:12
  • It is a good idea anyway to not use the OT1 encoding, as it causes several other problems. – Mateus Araújo Apr 11 '17 at 11:36

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

I had the same difficulties. In my case, the context was indeed an URL but the existing suggestions didn't quite work as I expected and the \url{} command was reducing the size of the font (making the text weird compared to the rest of the paragraph).

Then, I found this suggestion that worked like a charm! :)

% [...]
\newcommand{\urlwofont}[1]{\urlstyle{same}\url{#1}}

\begin{document}

\urlwofont{http://www.site/~person}
% [...]

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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