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):
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}