# Kerning of pairs with LuaTeX for babel shorthands

I want to make use of the pairwise kerning of this answer: https://tex.stackexchange.com/a/370469/75284 using fonts.handlers.otf.addfeature to adjust the kerning of character pairs with the hyphen -. With the hyphen entered directly this works fine, but I'd also like to use the babel shorthands "= and "~ and have them adjust their kerning as well. How could this be done?

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}

{
name = "ktest",
type = "kern",
data = {["-"] = { ["V"] =  -300 }}}
}

\setmainfont[RawFeature=+ktest]{Times New Roman}

\begin{document}\obeylines
Test-Versuch
Test"=Versuch
Test"~Versuch
\end{document}


Any other solution for kerning pairs with lualatex and the babel shorthands would also be welcome.

• That is not possible. The shorthand "= will expand to roughly \penalty10000-\hskip0pt. The \hskip is an explicit glue and any kerning accross it is discarded. – Henri Menke Apr 19 '18 at 11:49
• The "~ shorthand is approximately \textormath{\leavevmode\hbox{-}}{-}, i.e. \hbox{-} in text mode. There is no kerning between boxes and glyphs for the simple reason that TeX cannot look inside boxes (unless you unhbox it but that would negate the effect "~ is trying to achieve). – Henri Menke Apr 19 '18 at 11:52
• @Henri Menke: Thanks. Could "= and "~ be possibly redefined to allow kerning? – lblb Apr 19 '18 at 12:19
• I can't think of a way to do this without breaking the functionality that "= and "~ are trying to achieve. I'll have a look how ConTeXt does it. – Henri Menke Apr 19 '18 at 21:09

I have managed to propagate the regular kerning between the hyphen and V across the discretionary. Unfortunately, I don't know how to access the user-defined kerns :( That's why this is not really an answer.

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{fontspec}

\directlua{
local function is_hyphen(item)
return item.id == node.id("glyph") and item.char == 45
end
%
if is_hyphen(item) then
% Get current char and font
local char = item.char
local font = item.font
% Get next char and font
local next = node.next(item)
local nextchar = next.char
local nextfont = next.font
% Determine required skip size but only if fonts are equal
local hskip = node.new("glue",0) % subtype 0 = "userskip"
if font == nextfont then
local data = fonts.hashes.identifiers[font]
local kern = fonts.handlers.otf.getkern(data,char,nextchar)
hskip.width = kern
end
end
end
end
%
end,
"insert_breakpoints")
}

\setmainfont{TeX Gyre Termes}

\setlength\parindent{0pt} % Just for demonstration!!!

\begin{document}
Test"=Versuch

% Propagate kerning across the hyphen
Test-Versuch

% Inserted negative hskip facilitates line-breaking of next word
{\hsize=10pt Test-Versuch\par}
\end{document}


The kern is barely visible because it is only -52429sp (-0.8pt) wide.