[Tex/LaTex] \widetilde{} alternative for formula over the tilde

math-modetilde

I'd like to put some formula over the tilde in math mode and adjust the tilde length to the length of that formula. It seems that \widetilde{} is almost what I need, however it puts the formula below the tilde. Is there a way to actually put the formula above?

An example of the formula to put above is:

\widetilde{(p_1',p_2') \in C}

Edit: I wasn't exactly clear with the expected effect. I'd like to have tilde in the middle of line (like \sim does), however with the formula above.

Best Answer

I took my answer at Big tilde in math mode and made two changes: 1) I changed it from an overstack to an understack; and 2) I increased the vertical stacking gap to \LMpt, which is 1pt scaled to the local mathstyle.

\documentclass{article}
\usepackage{scalerel}[2014/03/10]
\usepackage{stackengine}

\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{\LMpt}{$\SavedStyle#1$}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.7\ht0}%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{U}{c}{F}{T}{S}%
}}

\def\test#1{$%
  \reallywidetilde{#1}\,
  \scriptstyle\reallywidetilde{#1}\,
  \scriptscriptstyle\reallywidetilde{#1}
$\par}

\parskip 1ex
\begin{document}
$x = \reallywidetilde{(p_1',p_2') \in C}$

$\scriptstyle x = \reallywidetilde{(p_1',p_2') \in C}$

$\scriptscriptstyle x = \reallywidetilde{(p_1',p_2') \in C}$
\end{document}

enter image description here

If, by some chance, you actually wanted the text over the tilde (rather than the tilde under the text), that is also easy:

\documentclass{article}
\usepackage{scalerel}[2014/03/10]
\usepackage{stackengine}

\newcommand\reallywidetilde[1]{\ThisStyle{%
  \setbox0=\hbox{$\SavedStyle#1$}%
  \stackengine{\LMpt}{%
    \stretchto{\scaleto{\SavedStyle\mkern.2mu\sim}{.5467\wd0}}{.7\ht0}%
    }{$\SavedStyle#1$%
%    .2mu is the kern imbalance when clipping white space
%    .5467++++ is \ht/[kerned \wd] aspect ratio for \sim glyph
  }{O}{c}{F}{T}{S}%
}}

\def\test#1{$%
  \reallywidetilde{#1}\,
  \scriptstyle\reallywidetilde{#1}\,
  \scriptscriptstyle\reallywidetilde{#1}
$\par}

\parskip 1ex
\begin{document}
$xyz \reallywidetilde{(p_1',p_2') \in C} abc$

$xyz {\scriptstyle\reallywidetilde{(p_1',p_2') \in C}} abc$

$xyz {\scriptscriptstyle\reallywidetilde{(p_1',p_2') \in C}} abc$
\end{document}

enter image description here