# Automatically give correct spacing for superscripts *before* a symbol

For a paper I am writing, I frequently need to use a superscript * before certain symbols (in math mode). I am currently using the command {}^* to do this. The spacing between the * and the symbol is often not right: for lowercase or slanted symbols (e.g. A), the spacing is far too much, whereas for other symbols it is pretty close (e.g. T). I could remove the spacing with a \!, but sometimes this will be too much, or not enough. Plus, I'd rather not have to manually set the spacing on a case by case basis.

Is there a way to automatically adjust the spacing between them based on the shape of the symbol? I tried wrapping the superscript in braces, and using the \prescript command from mathtools, but neither seemed to make a difference. See the image below.

Here's a MWE:

\documentclass[12pt]{article}

\usepackage{amssymb}
\usepackage{mathtools}

\newcommand{\N}{\mathbb{N}}
\newcommand{\h}{{}^*}
\newcommand{\hhh}[1]{\prescript{*}{}{#1}}

\begin{document}

$\begin{array}{cccc} \h A & \h B & \h T & \h \N \\ {\h} A & {\h} B & {\h} T & {\h} \N \\ \hhh{A} & \hhh{B} & \hhh{T} & \hhh{\N} \\ \h\! A & \h\! B & \h\! T & \h\! \N \\ \end{array}$

\end{document}


Notice how in the first 3 rows, the * is generally too far from the following symbol. I would like it about as close as it is to the T. Using \! to remove space, it is a good distance from the B, but still too far from the A, and now too close for T and N.

• Does the \prescript command from mathtools give any help? Jul 20 '20 at 18:45
• @murray: no help at all. That is the 3rd row in the table Jul 31 '20 at 6:23

pdftex can not access the shapes of glyphs. But assuming that you have a small, finite set of arguments that you want to handle you can easily setup a list of corrections (only needed for the cases where the default doesn't work):

\documentclass[12pt]{article}

\usepackage{amssymb}
\usepackage{mathtools}
\newcommand{\N}{\mathbb{N}}
\newcommand{\h}{{}^*}
\newcommand{\hhh}[1]{\prescript{*\csname prescriptcorrection\detokenize{#1}\endcsname}{}{#1}}
\newcommand\prescriptcorrectionA{\mkern-5mu\relax}
\newcommand\prescriptcorrectionB{\!}
%wrong value, only to show how to handle commands like \N:
\makeatletter\@namedef{prescriptcorrection\detokenize{\N}}{\mkern-15mu\relax}\makeatother

\begin{document}

$\begin{array}{cccc} \h A & \h B & \h T & \h \N \\ {\h} A & {\h} B & {\h} T & {\h} \N \\ \hhh{A} & \hhh{B} & \hhh{T} & \hhh{\N} \\ \end{array}$

\end{document}


• Thanks. Can XeTeX, LuaTeX, etc access the shape of glyphs? I don't mind using those engines instead. Jul 8 '20 at 22:01
• open type math fonts theoretically have values that describes the shape (math kerns) and luatex could use them (perhaps xelatex too, but I don't know if this is implemented). Practically support is a bit sketchy, Jul 8 '20 at 22:08

I have been looking for a solution to this problem for years, too. The issue is that TeX (or any other math rendering system for that matter, like MathML with OpenType Math fonts) has no metrics for positioning of left indices. It seems nobody ever really cared much about the issue. So the only thing you can really do is take the negative indentation of the right subscript and use that same value for the indentation of the left superscript, and hope for the best.

Based on egreg’s answer, I have now (yesterday) added the package leftindex to CTAN. It provides the command

\leftindex^{<left superscript>}_{<left subscript>} {<symbol>}


which does exactly what I described above. This works well for some symbols, but notably, it works badly for A. Therefore, \leftindex takes a couple of optional arguments. The only one that is relevant for us here is the first one:

\leftindex[<slanting phantom>]^{<left superscript>}_{<left subscript>}
{<symbol>}


If this argument is used, the package will instead use the metrics of the <slanting phantom> to position the left superscript:

\documentclass[12pt]{article}

\usepackage{amssymb,leftindex}

\newcommand{\N}{\mathbb{N}}
\newcommand{\h}{{}^*}
\newcommand{\hhh}[1]{\leftindex^{*}{#1}}
\newcommand{\hhhhh}[1]{\leftindex[I]^{*}{#1}}

\begin{document}

$\begin{array}{cccc} \h A & \h B & \h T & \h \N \\ {\h} A & {\h} B & {\h} T & {\h} \N \\ \hhh{A} & \hhh{B} & \hhh{T} & \hhh{\N} \\ \hhhhh{A} & \hhhhh{B} & \hhhhh{T} & \hhhhh{\N} \\ \leftindex[T]^{*} {A} & \leftindex[I]^{*} {B} & \leftindex[I]^{*} {T} & \leftindex^{*} {\N} \\ \h\! A & \h\! B & \h\! T & \h\! \N \\ \end{array}$

\end{document}


It’s not perfect, but part of the reason is that Computer Modern actually has rather bad metrics (add a subscript to P or \Gamma to see what I mean). Trying another font, like kpfonts, yields a better result:

• Nice. I ended up manually defining a command \hA for every symbol A I wanted to precede by a superscript, and manually adjusting the spacing for each symbol. Was not too time-intensive in the end. Aug 26 '20 at 11:29
• @JordanMitchellBarrett Sounds like the right approach. The solution with \leftindex will never be anything more than ad-hoc at best. In fact, leftindex was written mainly to be the companion package of my real project, the package semantex, see ctan.org/pkg/semantex. With this combination, you can pre-program additional indentation for each variable you are using. Aug 26 '20 at 19:14

Package tensor deals with mixed upper and lower indices in which the correct horizontal spacing must be observed.

\documentclass{article}
\usepackage{tensor}
\usepackage{amssymb}
\def\sstar#1{\tensor*[^*]{#1}{}}

\begin{document}
${}^*T\quad{}^*{\mathbb{N}}$

$\sstar{T}\quad\sstar{\mathbb{N}}$
\end{document}


As @Ulrike Fischer has said, if you have only a small set of characters to typeset, here is a solution with some keys to decrease the space.

\documentclass{article}
\usepackage{tensor}
\usepackage{array}
\usepackage{amssymb,amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\fp_new:N \g_mskip_fp
\keys_define:nn {sstar} {
a.fp_gset:N=\g_mskip_fp,
a.default:n=2,
b.fp_gset:N=\g_mskip_fp,
b.default:n=4,
c.fp_gset:N=\g_mskip_fp,
c.default:n=6,
}
\NewDocumentCommand {\sstarset} { m } {
\keys_set:nn {sstar} {#1}
}
\NewDocumentCommand {\sstar} { O{} m } {%
\fp_gset:Nn \g_mskip_fp {0}
\keys_set:nn {sstar} {#1}
\tensor*[^{*\mskip-\fp_use:N \g_mskip_fp mu}]{#2}{}
}
\ExplSyntaxOff

\begin{document}
$\begin{array}{lll} \sstar[a]{A} &\sstar[a]{B} &\sstar{\mathbb{N}}\\ \sstar[b]{A} &\sstar[b]{B} &\sstar{\mathbb{N}}\\ \sstar[c]{A} &\sstar[c]{B} &\sstar{\mathbb{N}}\\ \end{array}$
\end{document}

• you are ignoring the problematic cases of A and B: tensor doesn't adjust the spaces there. Jul 8 '20 at 8:37