Tell me more ×
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

4 Answers

up vote 20 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
2  
Uh, scratch that. For TTF/OTF fonts, \char`~ works. – Konrad Rudolph Jul 27 '10 at 14:01
show 1 more comment

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

For completeness’ sake: the accepted solution only seems to work with T1 fonts. When using XeTeX with the fontspec package, it once again displays a tilde at baseline level.

Luckily, there’s a simple workaround: When using a TTF or OTF font with fontspec, \char`~ works as expected. Unfortunately, this solution only works for fontspec, not with T1 fonts.

share|improve this answer

If you want to write in computer modern and still use the tilde, just load lmodern; them nice folks seem to consider it a bug, and \textasciitilde produces the vertically centred tilde.

share|improve this answer
This is the correct answer. – Mk12 Aug 25 '12 at 18:07

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.