47

I'd need a special symbol used in statistics.

What I need is a tilde with something written upon it.

I can get the tilde by using \sim, but I need to add a superscript over it (H_0). If I use \sim^{H_0}, I get a tilde with H_0 over it and on the right, not simply upon it.

Werner
  • 603,163
Luigi
  • 901

2 Answers2

37

The following minimal example provides two possibilities: \distas{<stuff>} and \distras{<stuff>}. The former uses amsmath's \overset{<top>}{<bottom>} which sets <top> over top of <bottom>. The latter uses a resized version of <bottom> in case <top> is wider than <bottom>. In fact, it stretches <bottom> to 6pt wider than <top> (3pt on either side). In both instances, <bottom> is fixed to \sim and the symbol is spaced as a binary operator via \mathbin.

enter image description here

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\makeatletter
\newcommand{\distas}[1]{\mathbin{\overset{#1}{\kern\z@\sim}}}%
\newsavebox{\mybox}\newsavebox{\mysim}
\newcommand{\distras}[1]{%
  \savebox{\mybox}{\hbox{\kern3pt$\scriptstyle#1$\kern3pt}}%
  \savebox{\mysim}{\hbox{$\sim$}}%
  \mathbin{\overset{#1}{\kern\z@\resizebox{\wd\mybox}{\ht\mysim}{$\sim$}}}%
}
\makeatother
\begin{document}

$X\distas{H_0}Y \quad X\distras{H_0}Y$
\end{document}
Werner
  • 603,163
  • 13
    Thank you both Werner and Thorsten Donig! Your answers were very useful. I've solved by using:

    \overset{H_0}{\sim}

    – Luigi Feb 03 '12 at 22:12
  • A variant for the first command, with an optional argument (and correct me if I'm wrong, it seems to work, but not extensively tested), is \newcommand{\dist}[1][]{\mathbin{\overset{#1}{\kern\z@\sim}}}% (with a [] after [1]) – PatrickT Nov 18 '17 at 06:20
32

\stackrel{H_0}{\sim} works for me.

Result:

enter image description here

Aditya
  • 62,301
dersu
  • 431
  • 3
    Welcome to TeX.SX! This is not about markdown, but about LaTeX. Please elaborate on how your solution solves the problem. – TeXnician Jul 02 '17 at 11:31
  • 1
    Please familiarize with answering – Bobyandbob Jul 02 '17 at 11:54
  • 3
    Why is this voted down?? It works beautifully in latex. Such a simple elegant solution. – davidparks21 Dec 04 '17 at 17:27
  • @davidparks21 The "In Markdown" at the beginning is very confusing. – Schweinebacke Dec 04 '17 at 17:37
  • 6
    A minor inconvenience when compared against the simplicity and the fact that it does work, and is a lot easier to use than defining a new command. I was quite happy to plug and chug with this answer, but almost ignored it on account of the downvotes. It's the best technical answer to the question, and should be voted up on that account. – davidparks21 Dec 04 '17 at 18:00