36

This question already has an answer here:

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.

marked as duplicate by Schweinebacke, Mensch, Troy, TeXnician, Mike Dec 4 '17 at 19:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

31

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}
  • 10
    Thank you both Werner and Thorsten Donig! Your answers were very useful. I've solved by using: \overset{H_0}{\sim} – Luigi Feb 3 '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 6:20
16

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

Result:

enter image description here

  • 3
    Welcome to TeX.SX! This is not about markdown, but about LaTeX. Please elaborate on how your solution solves the problem. – TeXnician Jul 2 '17 at 11:31
  • 1
    Please familiarize with answering – Bobyandbob Jul 2 '17 at 11:54
  • 2
    Why is this voted down?? It works beautifully in latex. Such a simple elegant solution. – davidparks21 Dec 4 '17 at 17:27
  • @davidparks21 The "In Markdown" at the beginning is very confusing. – Schweinebacke Dec 4 '17 at 17:37
  • 3
    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 4 '17 at 18:00

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