6

In the code below, the kerning is different if I apply color and if I don't. I understand why it's happening, but how do I change the definition of the command \C so that the kerning is the same as when the color is black? I'm asking for a general solution; I understand that I can fix all kerning manually.

The differences are not obvious in the font used here, but with e.g. mtpro2, it can get pretty ugly. As an aside, the widths of the two boxes are 12.496pt and 12.13722pt respectively; with mtpro2 the numbers are 13.9092 and 13.30093.

enter image description here

\documentclass{article}

\usepackage{amsmath}
\usepackage{xcolor}
\newcommand{\C}[1]{{\color{blue}#1}}
\newsavebox{\Cbox}
\newsavebox{\Obox}

\begin{document}


\begin{equation}
 \C y_{jt}^* \qquad \text{is kerned differently than} \qquad y_{jt}^*
\end{equation}

\savebox{\Cbox}{$\C y_{jt}^*$}
\showthe\wd\Cbox
\savebox{\Obox}{$y_{jt}^*$}
\showthe\wd\Obox

\end{document}
  • 1
    Interestingly, the difference in kerning doesn't arise if y is replaced with x, at least if the Computer Modern math font is used. Very puzzling. – Mico Mar 14 '17 at 21:39
  • 1
    @Mico is there any kerning with x anyway? the issue is the negative kerning to bring things closer to the y, so x_i is pretty much like x{}_i but y_i and y{}_i are different which makes adding anything and not affecting thespacing hard:-) – David Carlisle Mar 14 '17 at 21:45
  • 1
    @DavidCarlisle - After doing some more investigating, it turns out that while there's no difference in the widths of $\C{x}^*_{jt}$ and $x^*_{jt}$ if Computer Modern is used, there is a difference if mtpro, newtxmath, newpxmath, or kpfonts is loaded -- with the uncolored item always being less wide. Oh, and the differences in width, with either x or y as the "main" character, go away if there's just a superscript term but no subscript term. – Mico Mar 14 '17 at 21:52
  • 1
    @Mico: The relevant parameter is the italic correction of the character bearing the sub/superscript (see The TeXbook, p. 445, the sentence that spans lines 23–25: the value that δ gets is what counts, see the following sentence in Rule 17 and Rule 18f). In cmmi10, for example, the character “x” has no italic correction, whereas “y” has an italic correction of 0.35879pt. In general, the difference in width should be exactly equal (within rounding errors) to the said italic correction. For the case of an isolated supercript, the end of Rule 17 and Rule 18d yield again a net kern = δ. – GuM Mar 15 '17 at 15:13
7

enter image description here

wherein we learn tex outputs the superscript before the subscript:-)

The issue is the color restore after y breaks the kerning, so with a bit of care you can reset the colour somewhere else...

\documentclass{article}

\usepackage{amsmath}
\usepackage{xcolor}
\makeatletter
\def\unrestoredcolor{\aftergroup\@gobble\color}
\let\restorecolor\reset@color
\makeatletter

\newcommand{\C}[1]{\begingroup\unrestoredcolor{blue}#1\endgroup}
\newsavebox{\Cbox}
\newsavebox{\Obox}

\begin{document}

color
\begin{equation}
 \C y_{jt}^{\restorecolor*}\qquad \text{is kerned differently than} \qquad y_{jt}^*
\end{equation}


\savebox{\Cbox}{$\C y_{jt}^{\restorecolor*}$}
\showthe\wd\Cbox
\savebox{\Obox}{$y_{jt}^*$}
\showthe\wd\Obox



\end{document}
  • 1
    @JPi but beware if you get the color push and color pop in the wrong order (because tex does some internal box shuffling for example) you will corrupt the pdf stack and the pdf output may not work at all. the default definition is somewhat safer, at the price of sometimes affecting spacing. – David Carlisle Mar 14 '17 at 22:27
  • @DavidCarlisle: This seems to work fine in spite of the fact that the color push and color pop wind up at different boxing levels: I feel a bit unconfortable about this, but perhaps I shouldn’t. (BTW: come on, we already knew that TeX outputs \vboxes from top to bottom ;-) – GuM Mar 15 '17 at 9:22
  • @GustavoMezzetti yes it's a feature (by the way as you are there check the ho-tex github issue, I checked in some code yesterday) – David Carlisle Mar 15 '17 at 9:47
4

This is a known problem (cf. https://tex.stackexchange.com/a/183496/29873). It can be fixed by using LuaTeX and luacolor:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{luacolor}

\newcommand{\C}[1]{{\color{blue}#1}}
\newsavebox{\Cbox}
\newsavebox{\Obox}

\begin{document}


\begin{equation}
 \C y_{jt}^* \qquad \text{is kerned differently than} \qquad y_{jt}^*
\end{equation}

\savebox{\Cbox}{$\C y_{jt}^*$}
\showthe\wd\Cbox
\savebox{\Obox}{$y_{jt}^*$}
\showthe\wd\Obox

\end{document}
  • 2
    +1. Interestingly, loading unicode-math (with or without loading luacolor) also fixes the kerning issue. – Mico Mar 14 '17 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.