# Minus-like sign with stuff on top

I want to create an operator that works like a minus sign (including the spacing), but has the characters res on top (in \textrm mode). The length of the minus sign should be adjusted to look like underlining the res characters, and it needs to work in a subscript. Currently I am using

\newcommand{\residue}[2]{#1\overset{\text{res}}{\relbar\mathrel{\mkern-3mu}\relbar\rule{0pt}{2pt}}#2}


which means in particular the operator I am using is

\relbar\mathrel{\mkern-3mu}\relbar\rule{0pt}{2pt}


but it leaves a bit of space between the two dashes (especially in subscripts), and in inline or display mode, there's too much line to the left of the letters res.

What is a better way to produce this symbol?

-
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Apr 25 at 4:31

The following example defines macro \res with an optional argument for a different text than res:

• The size of the text is the size of a superscript (\scriptsize or \scriptscriptsize). Smaller sizes are not recommended to avoid the need for magnifying glasses.

• The vertical distance between the text and the minus sign is approximately the distance between the text and the underline by \underline.

• A longer minus sign is created by appending minus signs with some overlapping.

\documentclass{article}

\usepackage{amsmath}

% test for different fonts
%\usepackage{MnSymbol}
%\usepackage{mathabx}
%\usepackage{txfonts}

\makeatletter
\newcommand*{\res}[1][res]{%
\mathbin{%
\mathpalette\@res{\mathrm{#1}}%
}%
}
\newmuskip\res@sidebearing
\setlength{\res@sidebearing}{1.5mu}% e.g. 1.5mu/CM, 1mu/MnSymbol
\newcommand*{\@res}[2]{%
% #1: math style
% #2: text
\sbox0{$% \m@th \res@style#1% #2%$}%
\sbox2{$\m@th#1\mkern-\res@sidebearing-\mkern-\res@sidebearing$}%
\setbox4=\copy2 %
\ifdim\wd4<\wd0 %
\@whiledim\wd4<\wd0\do{%
\dimen@=\dimexpr\wd4+.8\wd2\relax
\ifdim\dimen@<\wd0 %
\sbox4{\unhcopy4\hss\kern-.2\wd2\unhcopy2}%
\else
\setbox4=\hbox to \wd0{%
\unhcopy4\hss\kern-.2\wd2\unhcopy2%
}%
\fi
}%
\fi
\sbox6{$#1\vcenter{}$}%
% \sbox8{$#1\mathop{}\limits^{}$}% alternative
\sbox8{$\res@style#1\underline{}$}% ht8 + dp8 = 5 theta; sep = 3 theta
\mkern1.5mu %
\rlap{\copy4}%
\hbox to \wd4{%
\hfill
% \raisebox{\dimexpr\dp0+\ht6+.5\ht8\relax}{\copy0}% alternative
\raisebox{\dimexpr\dp0+\ht6+.7\dimexpr\ht8+\dp8\relax\relax}{\copy0}%
\hfill
}%
\mkern1.5mu %
}
\newcommand*{\res@style}[1]{%
\ifx#1\displaystyle
\scriptstyle
\else
\ifx#1\textstyle
\scriptstyle
\else
\scriptscriptstyle
\fi
\fi
}
\makeatother

\begin{document}
% Test: "res" and long word "garbage" with descender
\begin{gather*}
\displaystyle      A \res B \res[garbage] C \res[r] D \\
\textstyle         A \res B \res[garbage] C \res[r] D \\
\scriptstyle       A \res B \res[garbage] C \res[r] D \\
\scriptscriptstyle A \res B \res[garbage] C \res[r] D
\end{gather*}
\end{document}


Difficulties and properties of the minus sign:

• The line thickness is not known. Thus the line thicknesses of \underline or \frac might be different.

• In many fonts, the line ends are rounded. The lines of \underline and \frac are not, because they are rules or rectangles.

• The side bearings, horizontal space at both ends, are not known. The implementation defines a math skip register \res@sidebearing for fine tuning.

• At least the vertical position is known, this should be the math axis.

Remarks:

• Box 0 contains the test in superscript size.

• Box 2 contains the minus sign minus side bearings.

• Box 4 is constructed with as many boxes 2 as needed to get a line that is long enough for the text.

• The height of box 6 is the math axis.

• Box 8: \underline{...} is similar to:

\vtop{
\hbox{...}
\kern 3θ
\hrule height θ
\kern θ
}


Box 8 contains an empty \underline{} and allows the calculation of the separation space 3θ: 0.6\dimexpr\ht8+\dp8\relax.

Also the half height of the minus rule needs to be added and it is approximated by θ/2 (the part above the math axis).

• \m@th is short for \mathsurround=0pt to avoid additional horizontal spacing, if \mathsurround is set.

• \mathpalette helps to set the formula for the current math style.

-

Perhaps the following works for you; I changed the notation slightly from \residue{A}{B} to A \residue B:

\documentclass{article}
\usepackage{amsmath,graphicx}
\begin{document}

\noindent
\begin{tabular}{@{}*{3}{p{.3333\linewidth}@{}}}
\texttt{Original} & \texttt{Choice 1} & \texttt{Choice 2} \\
\newcommand{\residue}[2]{#1\overset{\text{res}}{\relbar\mathrel{\mkern-3mu}\relbar\rule{0pt}{2pt}}#2}

$\residue{A}{B}$ \par
$C^{\residue{A}{B}}$ \par
$C^{D^{\residue{A}{B}}}$

&

\makeatletter
\newcommand{\residue@}[1]{#1\overset{\text{\raisebox{.15em}{res}}}{\smash{-\mkern-5mu-}}}
\newcommand{\residue}{\mathbin{\mathpalette\residue@\relax}}
\makeatother

$A \residue B$ \par
$C^{A \residue B}$ \par
$C^{D^{A \residue B}}$

&

\newcommand{\residue}{\mathbin{\mathchoice
{\overset{\text{\raisebox{.15em}{res}}}{\smash{-\mkern-5mu-}}}% \displaystyle
{\overset{\text{\raisebox{.15em}{res}}}{\smash{-\mkern-5mu-}}}% \textstyle
{\overset{\text{\raisebox{.15em}{res}}}{\smash{-\mkern-5mu-}}}% \scriptstyle
{\overset{\text{\raisebox{.1em}{\scalebox{.8}{res}}}}{\smash{-\mkern-5mu-}}}% \scriptscriptstyle
}}

$A \residue B$ \par
$C^{A \residue B}$ \par
$C^{D^{A \residue B}}$

\end{tabular}
\end{document}

• Choice 1 uses \mathpalette to provide the switching between math styles (see The mysteries of \mathpalette).

• Choice 2 employs \mathchoice directly to allow for a better scaling of res at the \scriptscriptstyle level since there is no \scriptscriptscriptstyle (as an \overset of a \scriptscriptstyle argument - in a super-super/sub-sub script technically needs that).

-
The OP mentioned that the spacing around the new composite symbol should be like that of a minus sign, i.e., of type mathbin. You may want to mention that if that's what the OP really wants, he/she should use \mathbin rather than \mathrel in the definition of the \residue macro. –  Mico Apr 25 at 5:59
@Mico: Absolutely correct. Imma gonna stick with Charles' original request of type \mathbin`. –  Werner Apr 25 at 6:03
I'm accepting Heiko Oberdiek's answer, but let it be known that Werner's answer would have been entirely adequate for my needs and should be upvoted. –  Charles Staats Apr 25 at 16:10
@Mico: Thanks for pointing that out; you are entirely correct. –  Charles Staats Apr 25 at 16:10