Change kerning between math characters (prime and left parenthesis) in ConTeXt

I use ConTeXt and Lucida OTF fonts, and I would like to adjust the kerning in math between the prime and the left parenthesis. Let us look at this example:

\startluacode
name = "kerntest",
type = "kern",
data = {
[0x61] = { [0x62] = -150 }, -- a and b in text
[0x1d44e] = { [0x1d44f] = -150 }, -- mathematical italic small a and b
[0x02032] = { [0x00028] = -150 }, -- prime and left parenthesis
}
}
\stopluacode

\definefontfeature[kerntest][kerntest=yes]

\definefontfamily[lucidaopentype][rm][Lucida Bright OT]
\definefontfamily[lucidaopentype][mm][Lucida Bright Math OT]

\setupbodyfont[lucidaopentype,10pt]

\startTEXpage[offset=3pt]
abba $abba f'(x)$

abba $abba f'(x)$

I prefer $f'\mkern-3mu(x)$
\stopTEXpage


The output with a new version of ConTeXt MKIV is the following:

As you can see I am able (after reading on pages 178 and 179 of this font manual) to adjust the kerning between the characters a and b, if in text, but not in math. Also, and more important, the spacing between the prime and the left parenthesis is unchanged (I admit that my approach was a bit naive).

Question I there a way, without messing things up, to change the kerning between prime and left parenthesis in math mode automatically through the whole document?

You might assume that I never use constructions like f'\left( or f'\bigl( or similar, but of course it is extra good if the solution also works for them. Just to emphasize, I work in ConTeXt, so I am looking for a solution that works in ConTeXt. Probably the solution will be lua based, and then there is of course the bonus that it will also work in lualatex.

Additional question This is probably opinion based, so I prefer if we focus on the first question. Could this issue perhaps be considered as a bug in the Lucida OpenType font?

I have tried some other open type fonts, both with lualatex and ConTeXt, and many of them have, in my opinion, non-optimal space between the prime and the parenthesis. Still, I imagine this is not an engine (luaotfload?) problem, but something that could/should be fixed in the fonts.

Scroll down to the update of July 31st, the issue is now considered to be solved.

I have now got some replies from the mailing list thread. There is something called "goodies" to adjust details in open type math fonts. For lucida, there is a file called lucida-opentype-math.lfg that can be edited.

In the goodie files one can set things like kerning for a character, kerning pairs (which I only got working for characters like a and b and not for prime and parenthesis) and width and offset of characters. Since I did not succeed in setting kerning pairs completely, I will use the last option for now.

In my lucida-opentype-math.lfg I changed the dimensions variable to the following (and also saved the file in the same folder as I have my tex file for the moment):

dimensions = {
default = {
[0x2032] = { xoffset = 50, width = 250, yoffset=-10}, -- prime
[0x2033] = { xoffset = 50, width = 650 , yoffset=-10}, -- double prime
[0x2034] = { xoffset = 50, width = 1050 , yoffset=-10}, -- triple prime
[0x2057] = { xoffset = 50, width = 1450 , yoffset=-10}, -- quadruple prime
},
},


Now, the minimal document (the lucidaot typescript automatically loads this goodie file)

\setupbodyfont[lucidaot,10pt]

\startTEXpage[offset=3pt]
$f'(x)f''(x)f'''(x)f''''(x)$

$\bigl(f'\bigr)^2=f''+f^{(4)}$
\stopTEXpage


gives the following result:

I don't know about others, but to me this looks better than the output shown in the question (it might still have to be tuned).

A possible drawback with this solution is that the prime(s) might "collide" with some other characters, but in the current document I work on, this does not seem to be the case.

Update, June 26

This method does not work anymore. Hans did change the way that primes are handled in LuaTeX 1.05, and the output (with that modified lucida-opentype-math.lfg) looks like this (i.e. nothing changes):

My guess is that still one could solve this with the goodie file lucida-opentype-math.lfg, but I do not know how.

Update, July 31

The issue is now solved, thanks to Hans Hagen's help on the mailing list. When introducing the new mechanism for primes, their unicode slots got remapped (I don't understand the details). Thus, I have now added the following lines in my lucida-opentype-math.lfg:

 dimensions = {
default = {
[0xFE932] = { width = 150 }, -- prime
[0xFE933] = { width = 435 }, -- double prime
[0xFE934] = { width = 720 }, -- triple prime
[0xFE957] = { width = 1005 }, -- quadruple prime
},
},


Below is the example from above with these settings.

1) The values above are the values I got by testing myself. I will write an email to the ConTeXt mailing list to suggest them (or other similar values) to be added to the font goodie file. If we are lucky, they will be inserted into the distribution.

2) It does not seem possible to use the parameter kernpairs to just change the kerning between the primes and the left parenthesis. I guess that the reason is that those symbols are kind of special.

• Note that it is better to rename the goodies file (and create a new typescript that uses the modified goodies file). Otherwise, when you upgrade your distribution, the goodies file will be overwritten. – Aditya Jun 2 '17 at 19:17
• @Aditya I agree with you that it might be better. But since I keep the new goodies file in the same directory as the tex file, this wont happen. On the other hand I will not get a new version of the goodie file either. I decided to answer like this, not to have to include a typescript and so on. – mickep Jun 2 '17 at 19:19

Not really an answer but too long for a comment. In LaTeX kerning works with the math fonts (I don't have lucida so I used latin moderm math). But you should be aware that math typesetting is two dimensional. Not every spacing can be handled as kerning between two adjacent glyph.

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
name = "kerntest",
type = "kern",
data = {
[0x61] = { [0x62] = -150 }, -- a and b in text
[0x1d44e] = { [0x1d44f] = -150 }, -- mathematical italic small a and b
[0x02032] = { [0x00028] = -150 }, -- prime and left parenthesis
}
}
\end{luacode}

\usepackage{unicode-math}

\setmathfont{latinmodern-math.otf}[RawFeature=+kerntest]

\begin{document}
\loggingoutput
\showoutput
$ab f\prime(a$

$ab f'(a$

\end{document}


As one can see there is a difference between the input with \prime and an apostroph. The reason why the second doesn't kern can be seen in the log. When the apostroph is used then the glyph are from two different fonts (7pt and 10pt):

.....\TU/latinmodern-math.otf(0)/m/n/7 ′
....\TU/latinmodern-math.otf(0)/m/n/10 (


When \prime is used both are from the 10pt font:

....\TU/latinmodern-math.otf(0)/m/n/10 ′
....\kern-1.5
....\TU/latinmodern-math.otf(0)/m/n/10 (


When you use \biggl( instead of a simple parenthese than you get in the log-file a complicated construction consisting of various hbox: kerning with the prime is out of question here.

• Thank you very much for your extended comment. So, there seem to be a difference between LaTeX and ConTeXt here. In ConTeXt I see no change when using a \prime instead of the apostroph. – mickep Jun 2 '17 at 8:39
• context math (and font) handling is a quite dark art. You could ask on the context list. Perhaps you can extract some meaningful clues from Hans. – Ulrike Fischer Jun 2 '17 at 8:49
• Thank you for the suggestion. I did not want to disturb Hans (again), but maybe that is the only person who knows about this "dark art". I will update my question with a link to the mail thread, once it is there. – mickep Jun 2 '17 at 8:52
• @Ulrich. would this work if one character is inside math-mode and the other is not? – Paulo Ney Nov 28 '18 at 6:36
• @PauloNey My name is Ulrike not Ulrich. And not, it won't work. Kerning will only work between adjacent chars of the same font. – Ulrike Fischer Nov 28 '18 at 7:48