201

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.

3
  • 8
    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
    Commented Mar 1, 2018 at 20:51
  • Subset of symbols - How does one insert a backslash or a tilde (~) into LaTeX? - TeX - LaTeX Stack Exchange, for cross-linking purpose.
    – user202729
    Commented Aug 9, 2022 at 11:26
  • @user202729 Ah, that's annoying: based on votes the other question/answers see a lot more traffic, yet the answers here are lot more comprehensive (and, importantly, better) than on the other question. (EDIT: hmm and apparently I knew that already, given that I answered the other question.) Commented Aug 9, 2022 at 11:56

8 Answers 8

57

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
3
  • 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. Commented Jul 27, 2010 at 13:54
  • 18
    Uh, scratch that. For TTF/OTF fonts, \char`~ works. Commented Jul 27, 2010 at 14:01
  • Note that for newpx and lmodern, \textasciitilde is the correct command to use to get a vertically centered ~.
    – inavda
    Commented Apr 28 at 22:53
96

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.
4
  • I'm using a font called iwona which I think is T1 and I can't get either \texttildelow or \char`~ to work.
    – gghuffer
    Commented Apr 6, 2016 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. Commented Apr 11, 2017 at 11:26
  • 1
    @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. Commented Apr 11, 2017 at 12:29
  • 1
    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. Commented Apr 11, 2017 at 13:35
45

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 encoding, 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.

4
  • 1
    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). Commented Jul 8, 2013 at 8:28
  • 7
    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. Commented Jul 30, 2013 at 13:25
  • This solution overrides the main font of the document.
    – Dan
    Commented Aug 7, 2022 at 6:30
  • Remark, do not use this for XeLaTeX/LuaLaTeX
    – user202729
    Commented Aug 9, 2022 at 11:30
24

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}
4
  • 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
    Commented Oct 17, 2014 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
    Commented Feb 3, 2015 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
    Commented Nov 19, 2015 at 16:12
  • It is a good idea anyway to not use the OT1 encoding, as it causes several other problems. Commented Apr 11, 2017 at 11:36
14

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

1
  • a list of available font name can be found here
    – jdhao
    Commented Feb 16, 2017 at 8:44
9

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}
3
  • 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. Commented Jul 8, 2013 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
    Commented Jul 8, 2013 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. Commented Jul 8, 2013 at 14:03
4

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.

3
  • This even maintains the syntax highlighting of the tilde for language = Python, which is great! Thanks :) Commented Oct 2, 2019 at 20:29
  • Actually, I've changed my mind (sorry!) If you use the hyperref package, then \url{~} is turned into a link to ~, which doesn't make sense, and also changes the colour of ~ in the code. Instead I now use literate={~}{{\raisebox{-.25em}{\textasciitilde}}}{1}. Commented Apr 3, 2020 at 22:37
  • good points; when i come back to the template using this, i'll have a re-look
    – Carl
    Commented Apr 4, 2020 at 8:07
2

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}
% [...]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .