# Tweaking \tilde{} command

When one wants to typeset tilde over a character, one uses the \tilde{} command. Suppose I want to tweak that command so that it prints tilde and some additional character. How would I do that?

Ideally, I would like to know how \tilde{} is defined in TeX internals, but I do not know how to find it.

Edit based on comments below: The additional character should be in accent position as well. That is, next to the tilde.

Namely, I would like to produce something akin to the figure below, which is produced with the help of the accents package using $\accentset{\sim i}{X}$, except for the \sim replaced by tilde. (When I include tilde using \textasciitilde or \texttildelow the tilde is either too high or too low and the size of i is changed.)

• According to texdef, \tilde is \mathaccent "707E\relax – user31729 Mar 9 '17 at 19:22
• It’s a macro defined in the LaTeX kernel: its definition is simply \mathaccent "707E\relax, and \mathaccent is a TeX primitive. – GuM Mar 9 '17 at 19:22
• To see how a macro is defined, run latex without file name, enter \relax at the ** prompt, and then \show\tilde at the * prompt. You will get the information that \tilde is a macro defined as \mathaccent "707E\relax. Or write \show\tilde into a text file and run latex on it. – gernot says Reinstate Monica Mar 9 '17 at 19:22
• More precisely, its definition can be found at line 412 of file fontmath.ltx. – GuM Mar 9 '17 at 19:26
• Do you mean that this “something additional” should be in the accent position too? – GuM Mar 9 '17 at 22:07

Maybe you want something like this:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newsavebox{\jan@xtilde@base}
\newsavebox{\jan@xtilde@top}
\newcommand{\xtilde}[2][]{%
\if\relax\detokenize{#1}\relax
\tilde{#2}%
\else
{\jan@xtilde{#1}{#2}}%
\fi
}
\newcommand{\jan@xtilde}[2]{%
\sbox\jan@xtilde@top{$\m@th\scriptscriptstyle#1$}%
\sbox\jan@xtilde@base{$\m@th#2$}%
\kern-.25\wd\jan@xtilde@top
\tilde{%
\kern.25\wd\jan@xtilde@top
#2%
}%
\kern-.75\wd\jan@xtilde@top
\raisebox{\dimexpr\ht\jan@xtilde@base+\dp\jan@xtilde@top+1.5pt}{\copy\jan@xtilde@top}%
\kern-.25\wd\jan@xtilde@top
}
\makeatother

\begin{document}

$X\xtilde{X}\xtilde[i]{X}X\xtilde[f]{X}$

\end{document}


• I’m really sorry, but I cannot (yet) upvote, for two reasons: (1) Hard-coding a vertical gap of +1.5pt is out of question, what if the document is typeset at 12pt or the formula occurs in a footnote? (2) Try \xtilde[i]{X}^{2}, compare it with \tilde{X}^{2}, and recall Rule 12, sixth sentence (“If the nucleus is a single character, … between the new and old values of h(x).”). – GuM Mar 11 '17 at 1:02
• I forgot to add a third reason: try \xtilde[i]{xy^{2}} for example. The accented symbol is typeset in cramped style (again Rule 12, second sentence), so you should use mathtools and say \sbox\jan@xtilde@base{$\m@th\cramped{#2}$}. – GuM Mar 11 '17 at 1:54
• @GustavoMezzetti All interesting comments, but in absence of examples from the OP, I'm not going to spend more time on this. – egreg Mar 11 '17 at 9:12
• Thank you very much. This is very nice solution. Gustavo's examples are interesting. After seeing what \tilde{x^{2}} produces I would not hold his third reason against your solution. However, $\xtilde[i]{X}^{2}$ puts the exponent unnaturally high and I have no idea how to 'bring it back' (I understand what your code does but my knowledge of TeX is limited to be able to tweak it in order to get what I want). – Jan Mar 13 '17 at 10:44
\documentclass{minimal}

\begin{document}
\let\origtilde\tilde
\def\tilde#1{\origtilde{#1}Hello!}
$\tilde{a}$
\end{document}


Update

After you edited the post I understood what you wanted.

This solution uses accents for the \setaccent command and mathtools for \mathrlap (I want the tilde to be centered on the character). Also I used \sim rather than tilde since it looks better with added char

\documentclass{article}
\usepackage{mathtools,accents}
\thispagestyle{empty}
\begin{document}
$\accentset{\sim\mathrlap{i}}{a}$
\end{document}


• I was not clear enough and I apologize for that. Your example works, but suppose I want to put 'i' next to the tilde in \tilde{}. That is, at the same level, in, say, double-subscript \scriptscriptstyle size. – Jan Mar 9 '17 at 21:59
• It’s a bit embarassing to tell this to a user with a reputation like yours, but you shouldn’t be using the minimal class, especially in an answer given to an unexperienced user. – GuM Mar 9 '17 at 22:34
• Thanks. I used minimal just to skip the line \pagestyle{empty}, and then use pdfcrop to get the mwe. – Boris Mar 9 '17 at 23:14
• Thank you Boris. This is almost what I wanted. I managed to change the \sim to tilde (surprisingly tricky, I am still not sure which one I like better) by getting tilde via \raisebox{0.075ex}{\texttildelow}. – Jan Mar 13 '17 at 10:41