Math-Mode – How to Redefine \prime with unicode-math

accentsmath-modeunicode-math

I'm using unicode-math (with XeLaTeX) to set my text and math fonts. However, using Garamond-math as a math font, I see the \prime symbol is too close to the letter:

enter image description here

I am trying to redefine the \prime command, and if possible I would like to remap ' in math mode so that it gives the correct spacing. However, the "usual" solution to redefine \prime, that is (MWE)

\documentclass{article}
\usepackage{unicode-math}
\setmathfont{Garamond-Math.otf}[StylisticSet={5,7,9}]
\let\originalprime\prime
\def\prime{\mkern3mu\originalprime\mkern-3}
\begin{document}

$f'(x)$

\end{document}

but this solution does not seem to work in this context. I also tried

\usepackage{newunicodechar}
\AtBeginDocument{\newunicodechar{′}{\mkern3mu\prime\mkern-3u}}

but again it had no effect.

Best Answer

In unicode-math the command \prime is defined in terms of an internal function, which is used when f'(x) is processed. So redefining \prime does nothing, unless you do use it explicitly.

There is no interface for modifying the behavior, unfortunately. But the following works with the current version of unicode-math. However, due to accessing private functions, this is not guaranteed to remain stable in future releases.

\documentclass{article}
\usepackage{unicode-math}
\setmathfont{Garamond-Math.otf}[StylisticSet={5,7,9}]

\ExplSyntaxOn
\cs_set:Npn \__um_prime_single_mchar { \mkern2mu\Umathchar"0"0"2032\mkern-2mu~}
\cs_set:Npn \__um_prime_double_mchar { \mkern2mu\Umathchar"0"0"2033\mkern-2mu~}
\cs_set:Npn \__um_prime_triple_mchar { \mkern2mu\Umathchar"0"0"2034\mkern-2mu~}
\ExplSyntaxOff

\begin{document}

$f'(x)+f''(x)+f'''(x)$

$g'(x)+g''(x)+g'''(x)$

$h'(x)+h''(x)+h'''(x)$

\end{document}

enter image description here

Related Question