I want to write an X with a bar on top. The X should be upright, but in headings it should be italics. The bar should be in the correct position. So my question is a variant of Aligning \overline to "italics" font. I started to play with jfbu's solution but cannot get the original upshape position to work.
\documentclass{article}
\usepackage{xspace}
\newbox\usefulbox
\makeatletter
\def\getslant #1{\strip@pt\fontdimen1 #1}
\def\skoverline #1{\mathchoice
{{\setbox\usefulbox=\hbox{$\m@th\displaystyle #1$}%
\dimen@ \getslant\the\textfont\symletters \ht\usefulbox
\divide\dimen@ \tw@
\kern\dimen@
\overline{\kern-\dimen@ \box\usefulbox\kern\dimen@ }\kern-\dimen@ }}
{{\setbox\usefulbox=\hbox{$\m@th\textstyle #1$}%
\dimen@ \getslant\the\textfont\symletters \ht\usefulbox
\divide\dimen@ \tw@
\kern\dimen@
\overline{\kern-\dimen@ \box\usefulbox\kern\dimen@ }\kern-\dimen@ }}
{{\setbox\usefulbox=\hbox{$\m@th\scriptstyle #1$}%
\dimen@ \getslant\the\scriptfont\symletters \ht\usefulbox
\divide\dimen@ \tw@
\kern\dimen@
\overline{\kern-\dimen@ \box\usefulbox\kern\dimen@ }\kern-\dimen@ }}
{{\setbox\usefulbox=\hbox{$\m@th\scriptscriptstyle #1$}%
\dimen@ \getslant\the\scriptscriptfont\symletters \ht\usefulbox
\divide\dimen@ \tw@
\kern\dimen@
\overline{\kern-\dimen@ \box\usefulbox\kern\dimen@ }\kern-\dimen@ }}%
{}}
\makeatother
\newcommand{\xbar}{$\skoverline{X}$\xspace}
\newcommand{\xbarup}{\mbox{$\overline{\mbox{X}}$}\xspace}
\newcommand{\xbarupsk}{\mbox{$\skoverline{\mbox{X}}$}\xspace}
\begin{document}\thispagestyle{empty}
\xbar-Theory
\emph{\xbar-Theory} (correct)
\xbarup-Theory (correct)
\emph{\xbarup-Theory}
\xbarupsk-Theory
\emph{\xbarupsk-Theory}
\end{document}

\=isn't long enough. what is needed is a diacritic equivalent to unicode U+0305. this may be available in thexitsfonts. – barbara beeton Mar 01 '18 at 14:35\emphor not, I could use either of the macros. – Stefan Müller Mar 02 '18 at 07:00