Superscripts can sit very far from large parentheses, more for some fonts than others. I am seeking a Lua-based solution to adjust the kerning of math superscripts on delimiters.
Here is an example of this kind of wide placement of superscripts in Latin Modern. The effect is more noticable for larger parentheses.
In another thread, Mico gave a Lua solution for adjusting superscript and subscript placement was given, and that method can be used directly when the delimiter size is known (see below). However, a number of LaTeX environments (such as pmatrix) generate delimiters automatically, and so the delimiter size may not be known a priori. In those cases, a kern adjustment that works on a large size may fail on a small size. It seems that a better solution would ask "Which specific glyph is being superscripted?"
Here is an MWE with the results shown. For known delimiter size, good results are obtained, but for variable sizes (especially the last, nested example) things aren't so good!
% !TEX TS-program = lualatex
\documentclass[11pt]{article}
\usepackage{luacode, mathtools, unicode-math}
\usepackage{mleftright}
%% Adapted from Mico's answer here:
%% https://tex.stackexchange.com/questions/432200/ugly-horizontal-spacing-with-some-symbol-subscript-superscript-combinations
\begin{luacode}
function suppl_math_kerning ( s )
-- \Bigr)
s = s:gsub ("(\\Bigr%))%s-%^%s-(%b{})", "%1^{\\mkern-4mu %2}" )
s = s:gsub ("(\\Bigr%))%s-%^%s-(%w)" , "%1^{\\mkern-4mu %2}" )
-- \biggr)
s = s:gsub ("(\\biggr%))%s-%^%s-(%b{})", "%1^{\\mkern-5mu %2}" )
s = s:gsub ("(\\biggr%))%s-%^%s-(%w)" , "%1^{\\mkern-5mu %2}" )
-- \Biggr)
s = s:gsub ("(\\Biggr%))%s-%^%s-(%b{})", "%1^{\\mkern-6.5mu %2}" )
s = s:gsub ("(\\Biggr%))%s-%^%s-(%w)" , "%1^{\\mkern-6.5mu %2}" )
-- \mright)
s = s:gsub ("(\\mright%))%s-%^%s-(%b{})", "%1^{\\mkern-6mu %2}" )
s = s:gsub ("(\\mright%))%s-%^%s-(%w)" , "%1^{\\mkern-6mu %2}" )
-- pmatrix*
s = s:gsub ("(\\end{pmatrix%*})%s-%^%s-(%w)" , "%1^{\\mkern-5mu %2 }" )
s = s:gsub ("(\\end{pmatrix%*})%s-%^%s-(%b{})", "%1^{\\mkern-5mu %2 }" )
return s
end
\end{luacode}
\newcommand\SupplKernOn{\directlua{luatexbase.add_to_callback ("process_input_buffer" , suppl_math_kerning , "suppl_math_kerning" ) }}
\newcommand\SupplKernOff{\directlua{luatexbase.remove_from_callback ("process_input_buffer" , "suppl_math_kerning" ) }}
%% End adapted code
\begin{document}
\SupplKernOn
\[ \verb|\Bigr):| \quad\cos\Bigl(\frac{a}{b}\Bigr)^2 \qquad
\verb|\biggr):| \quad\cos\biggl(\frac{a^2}{b_1}\biggr)^2 \qquad
\verb|\Biggr):| \quad\cos\Biggl(\frac{a^2}{b_1}\Biggr)^2\]
\[ \verb|\end{pmatrix*}:| \quad \begin{pmatrix*}[r] 1 & 0 \\0 & 1 \\ -1 & 0 \end{pmatrix*}^T
\begin{pmatrix*}[r] 1 & 1 & 0 \\- 1 & 1 & 1 \end{pmatrix*}^T \]
\[ \verb|\mright):|\quad\cos\mleft(\frac{a_1}{\mleft(b_1+c_1\mright)^2}\mright)^{t_r} \]
\end{document}
So, I have two questions:
- Can the Lua approach in my MWE be adjusted to deal with the cases for which the delimiter size is unknown and variable; or
- Can an appropriate kerning feature be created via
fonts.handlers.otf.addfeature
to adjust the kerning of superscripts on the basis of the specific delimiter glyph being called, e.g., along the lines given by Thérèse in this thread. I'm wondering whether a specific set of kerns might be defined for each of several specific parentheses glyphs, or whether the superscripting process itself might make that impossible.
mlist_to_hlist
callback and check whether aclose_noad
is followed by amath_sup_list
and insert kerning depending on the type offence_noad
.