# Kerning of subscripts

I have a large document that's full of subscripted variables that represent points and vectors. I want to typeset the points and vectors upright and bold. I'm using Unicode Math with Latin Modern, and typesetting using XeLaTeX. I get results like this:

I was hoping for much better kerning of the subscript n on my letter \mathbf{P}. I realize that I can correct each occurrence individually, but that doesn't really seem like the right approach.

My bold \mathbf{P} is actually defined by a macro, so my next attempt was to put the correction in this macro. But that didn't work because the correction is only needed when the \mathbf{P} is followed by a subscript.

I'm wondering if I have a somehow corrupted version of Latin Modern Math, because the subscript kerning isn't working properly in Microsoft Word, either. The kerning does work if I use the Cambria font in MS Word.

Here is a sample document:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Latin Modern Roman}
\setmathfont{Latin Modern Math}

\def\A {\mathbf{A}}
\def\P {\mathbf{P}}

\begin{document}
$\P(x) = \P_n + \A^n$
\end{document}


Can I fix this problem without editing thousands of formulae individually?

My Solution (For Now)

It seems that the best I can do for now is something like:

\def\Ps #1{\P_{ \mkern-3.5mu  {#1}}}
...
$\P(x) = \Ps{1} + \Ps{2} + \cdots + \Ps{n}$


This gives results that look good, to me:

And it's easy to remove this hack if/when the Latin Modern fonts get fixed.

• Also, you should have \mathbf{P}, etc., rather than \mathbf P. – Joseph Wright Sep 21 '13 at 7:33
• \def with short command names is very dangerous. Important macros can be redefined without previous warning. – Heiko Oberdiek Sep 21 '13 at 8:22

The \mathbf alphabet of Latin Modern Math (the version shipped with TeX Live 2013) does not have any provisions for correctly positioning super or subscripts, neither italic correction (the old TeX way, still supported in OpenType math) or math cut-ins (the new, more versatile way). So this is a font issue and the most straightforward way to fix it, apart from using a different font, is to fix the font itself.

• Thanks. I don't want to abandon Latin Modern Math. Or, at least, I don't want to abandon the Computer Modern "look". So, if I understand correctly, the "right" solution is to fix the Latin Modern Math fonts so that they support the "cut-in" approach to subscript positioning. I suppose this means putting suitable entries into the OpenType "Math" table(s) in the font. This could be done using FontForge, couldn't it? I'd be happy if just the "P" character worked, for now. Would you say this is a large job? Hours? days? weeks? – bubba Sep 21 '13 at 12:00
• Also, if I were to try this, is there any helpful documentation, anywhere? Or could I just copy/adapt the data in the XITS fonts, which I assume were done correctly :-) – bubba Sep 21 '13 at 12:08
• It is a couple of minutes work for someone who knows how to do it (not counting any “test → edit the font → test again” cycles). No tutorials AFAIK, but you can check other fonts. – Khaled Hosny Sep 21 '13 at 13:00
• Thanks, Khaled. I think I'll use some easily reversible macro hack, for now. I can remove it if/when the Latin Modern folks get a few minutes of spare time to implement the fix you mentioned. – bubba Sep 21 '13 at 13:26
• Just as a warning to the wise for anyone who travels down this path, the cut-in feature isn't implemented in free OTF math fonts because Microsoft was nice enough to patent it. I'm not sure if providing the information in your font file counts as infringing on the patent, but there is little point since only Microsoft licensed products could use the data (legally). – John Colanduoni May 14 '15 at 14:00

## TeXs without OpenType

(|pdf)TeX only sees boxes. It does not know, how the glyph is shaped inside the character box. The implicit kerning does not apply between the base character and its sub- or superscript.

The kerning can manually be fixed:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Latin Modern Roman}
\setmathfont{Latin Modern Math}

\begin{document}
$\mathbf P(x) = \mathbf P_n + \mathbf A^n$
$\mathbf P(x) = \mathbf P_{\!n} + \mathbf A^{\!n}$
\end{document}


P and A could be made active in math mode. Then they could look for a sub- or superscript. But it does not help here, because P and A are hidden inside \mathbf. An easy way is to make \mathbf P a macro that can check the following token for a subscript:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Latin Modern Roman}
\setmathfont{Latin Modern Math}

\usepackage{ltxcmds}[2011/04/14]
\makeatletter
\newcommand*{\bfP}{%
\mathbf{P}%
\ltx@ifnextchar@nospace_{\@CatchSubScriptNegKern}{}%
}
\newcommand*{\bfA}{%
\mathbf{A}%
\ltx@ifnextchar@nospace^{\@CatchSuperScriptNegKern}{}%
}
\def\@CatchSubScriptNegKern_#1{%
_{\!#1}%
}
\def\@CatchSuperScriptNegKern^#1{%
^{\!#1}%
}
\makeatother

\begin{document}
$\bfP(x) = \bfP_n + \bfA^n$
\end{document}


## LuaTeX/XeTeX

Package unicode-math seems to have a feature to replace some superscripts and subscripts to get a better placement. With xits-math.otf this works:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Latin Modern Roman}
%\setmathfont{Latin Modern Math}
\setmathfont{xits-math.otf}

\begin{document}
$\mathbf P(x) = \mathbf P_n + \mathbf A^n$
$\mathbf A^n = \mathbf A^^^^207f \mathrel{;} \mathbf A^{nn}$
$\mathbf P_n \ne \mathbf P_^^^^2099 \mathrel{;} \mathbf P_{nn}$
\end{document}


Remarks:

• Image generated by LuaLaTeX.

• latinmodern-math.otf and Asana-Math.otf do not work.

• If the sub-/superscript is not a single character, it does not work for LuaLaTeX as can be seen in the image.
• Sub- and superscripts of n are also available as Unicode characters:

U+2099: LATIN SUBSCRIPT SMALL LETTER N
U+207F: SUPERSCRIPT LATIN SMALL LETTER N


Only the latter case of a superscript works in the example. The subscript case failed for latinmodern-math.otf, Asana-Math.otf and xits-math.otf.

• Thanks. That's bad news. I know that TeX didn't know anything about glyph ink in the old days, but I thought modern versions could leverage the extra info held in "math" fonts like Latin Modern and Cambria. – bubba Sep 21 '13 at 7:53
• As I said, I know how to fix each formula individually. Is there any way to build this correction into my "P" macro, so that it gets applied whenever P has a subscript? – bubba Sep 21 '13 at 7:54
• The use of U+2099 or U+207F is wrong here. The Unicode super or subscript characters are encoded for certain linguistic uses and should not be used in place styled super or subscripts. unicode-math has a mis-feature to use those characters as shorthands for ^x or _x, but I don’t see how that is relevant here; OP issue is the lack of proper positioning in Latin Modern Math, that is simply a font issue. – Khaled Hosny Sep 21 '13 at 11:33
• @Heiko -- thanks for all your effort. As I said, I'm using Latin Modern and XeteX. So, what you wrote about pdfTex is not applicable, is it? And the solution you described for XeteX doesn't work with Latin Modern (apparently because of some deficiencies in that font family). Did I understand correctly? Thanks again. – bubba Sep 21 '13 at 12:24