# How to automatically remove additional space introduced by primes of left superscripts?

I need to use left superscripts. I know that there are packages as mathtools, tensor and leftidx. But they all suffer from the same typographical problem.

If I typeset a left superscript and this superscript has a prime, asterisk or something similar attached to its right, then the left superscript is moved away from its base symbol. LaTeX does introduce this gap, because LaTeX believes it would need this additional space for the right superscript of the left superscript, but it does not, because actually both symbols do not interfere with each other.

Now here is a MWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}

% Symbol for Turing machine and Oracle
\newcommand{\tmF}{\mathcal{F}}
\newcommand{\oO}{\mathcal{O}}

% Left superscript, copied from "leftidx"-package
% How to improve this command?
\newcommand{\append}[2]{{\protect\vphantom{#1}}^{#2}\!#1}

% Code from: https://www.tug.org/TUGboat/tb22-4/tb72perlS.pdf
\def\mathrlap{\mathpalette\mathrlapinternal}
\def\mathrlapinternal#1#2{\rlap{$\mathsurround=0pt#1{#2}$}}

\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{0.25pt}

\begin{document}

\section{Problem visualization}

\begin{itemize}
\item This looks OK: $\append{\tmF}{\oO}$
\item This looks typographically wrong: $\append{\tmF}{\oO'}$
\item This looks better: $\append{\tmF}{\oO\mathrlap{'}}$
\end{itemize}

\section{Problem visualization with boxes}

\begin{itemize}
\item This looks OK: $\append{\fbox{$\tmF$}}{\fbox{$\oO$}}$
\item This looks typographically wrong: $\append{\fbox{$\tmF$}}{\fbox{$\oO$}'}$
\item This looks better: $\append{\fbox{$\tmF$}}{\fbox{$\oO$}\mathrlap{'}}$
\end{itemize}

\end{document}


The append macro works as follows: First, the base symbol is typeset invisible such that the following superscript is moved to the correct height in case that the base symbol itsself has a different height. Then a small negative space is introduced to eat up the normal space between two consecutive noun symbols in math mode. At last the actual base symbol is typeset.

I produce the typographical correct version by manually telling LaTeX that the prime symbol does not any space. But I would like to avoid using \mathrlap and would rather prefer an append macro that automatically figures out how much extra space is needed.

• Since TeX works on boxes, the adjustment will depend on the actual shape of the char, and this it is probably not possible to do this automatically. – daleif Mar 3 '16 at 12:25
• Interesting problem, but as daleif says, it's no easy (if at all possible) getting an “automatic” version. – egreg Mar 3 '16 at 12:57

The “visual hole” depends of course on the relative shapes of the symbols. You can remove the \scriptspace on the left superscript, which will reduce the gap.

In a product version you will of course change \trylength to \z@ (that is, 0pt) in the definition of \app@@nd.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}

% Symbol for Turing machine and Oracle
\newcommand{\tmF}{\mathcal{F}}
\newcommand{\oO}{\mathcal{O}}

% Left superscript, copied from "leftidx"-package
% How to improve this command?
\makeatletter
\DeclareRobustCommand{\append}[2]{%
\mathpalette\app@nd{{#1}{#2}}#1%
}
\newcommand{\app@nd}[2]{\app@@nd#1#2}
\newcommand{\app@@nd}[3]{%
\mbox{\scriptspace\trylength\m@th$#1{\vphantom{#2}}^{#3}$}%
}
\newdimen\trylength
\makeatother

% Code from: https://www.tug.org/TUGboat/tb22-4/tb72perlS.pdf
\def\mathrlap{\mathpalette\mathrlapinternal}
\def\mathrlapinternal#1#2{\rlap{$\mathsurround=0pt#1{#2}$}}

\setlength{\fboxsep}{-0.25pt}
\setlength{\fboxrule}{0.25pt}

\begin{document}

\section{Normal scriptspace}
\trylength=\scriptspace
\begin{itemize}
\item $\append{\tmF}{\oO}$ $\append{\mathcal{H}}{\oO}$
\item $\append{\tmF}{\oO'}$ $\append{\mathcal{H}}{\oO'}$
\item $\append{\fbox{$\tmF$}}{\fbox{$\scriptstyle\oO$}}$
\item $\append{\fbox{$\tmF$}}{\fbox{$\scriptstyle\oO'$}}$
\end{itemize}

\section{Zero scriptspace}
\trylength=0pt
\begin{itemize}
\item $\append{\tmF}{\oO}$ $\append{\mathcal{H}}{\oO}$
\item $\append{\tmF}{\oO'}$ $\append{\mathcal{H}}{\oO'}$
\item $\append{\fbox{$\tmF$}}{\fbox{$\scriptstyle\oO$}}$
\item $\append{\fbox{$\tmF$}}{\fbox{$\scriptstyle\oO'$}}$
\end{itemize}

\end{document}


• Thanks, but if understand correctly, it only reduces the script space to zero, it does not make the prime horizontally overlap with the base symbol. Right? – nagmat84 Mar 3 '16 at 13:23
• @nagmat84 Indeed. If you want to make the prime overlap the base, use \mathrlap{'}. In case there's a subscript to the left superscript, you don't want overlapping, do you? Note also that mathtools already features the \mathrlap macro. – egreg Mar 3 '16 at 13:26
• OK. Just one more question that perhaps is a little bit out of scope of a comment. I try to understand your code but do not really understand why you need this double indirection append -> app@end -> app@@nd. append calls app@end with two arguments whereby the fist argument is the token {{#1}{#2}} and the second is #1. Then app@end calls app@@nd with three arguments which after substitution by the original arguments are {#1}{#2}#1. Why does append not directly call app@@nd with {#1}{#2}#1? I guess it has something do to with expansion of tokens but I am a newbie. – nagmat84 Mar 3 '16 at 13:36
• @nagmat84 \mathpalette takes two arguments, not three; \mathpalette\app@nd{{#1}{#2}} will do the four \app@nd<style>{{#1}{#2}} calls, which are so transformed into \app@@nd<style>{#1}{#2}. – egreg Mar 3 '16 at 13:43