Pgfplots fails to display greek prefix when using unicode-math in LuaLaTeX/XeLaTeX

pgfplotsunicode-mathunits

I'm experiencing difficulties trying to use the "Units in Labels" feature of pgfplots (pages 525-528 of the v1.17 manual). It works well in most circumstances except when the prefix is the greek letter "mu" and the "unicode-math" package is used. In this case, no prefix is displayed. I've tested it with various Math typefaces (Libertinus Math, TeX Gyre xx Math, STIX Math, etc.), with identical results.

Here is a MWE with 3 cases (considering only the X axis):

  1. The first one shows the error: Although the "micro" SI prefix is specified, no "μ" character is shown using LuaLaTeX (using XeLaTeX a "tofu" character is displayed).
  2. The second plot shows the correct behaviour when a prefix other than "micro" is used.
  3. The third one shows that it's still possible to use the "x unit prefix" option to manually insert the desired prefix, but this is not an ideal solution.
% !TeX TS-program = lualatex
\documentclass[tikz,border=5pt,convert={density=150x150}]{standalone}

\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont{Libertinus Serif}
\setmathfont{Libertinus Math}

\usepackage{siunitx}
\usepackage{pgfplots}
\usepgfplotslibrary{units}

\begin{document}
    % Example 1: Greek letter mu doesn't get displayed as a prefix
    \begin{tikzpicture}
    \begin{axis}[
        width=.5\textwidth,
        xlabel=$t$, x unit=s, change x base, x SI prefix=micro,
        ylabel=$v_g$, y unit=V,
        ]
        \addplot coordinates {(0,0) (100e-6,1) (200e-6,0.5) (300e-6,0.5)};
    \end{axis}
    \end{tikzpicture}
    % Example 2: No problem with "millis" or other non-greek prefix
    \begin{tikzpicture}
    \begin{axis}[
        width=.5\textwidth,
        xlabel=$t$, x unit=s, change x base, x SI prefix=milli,
        ylabel=$v_g$, y unit=V,
        ]
        \addplot coordinates {(0,0) (100e-6,1) (200e-6,0.5) (300e-6,0.5)};
    \end{axis}
    \end{tikzpicture}
    % Example 3: manually set prefix
    \begin{tikzpicture}
    \begin{axis}[
        width=.5\textwidth,
        xlabel=$t$, x unit=s, change x base, x SI prefix=micro,
        x unit prefix=μ, % $\mu$ doesn't work
        ylabel=$v_g$, y unit=V,
        ]
        \addplot coordinates {(0,0) (100e-6,1) (200e-6,0.5) (300e-6,0.5)};
    \end{axis}
    \end{tikzpicture}
\end{document}

Error: "micro" prefix not displayed
No problem with "milli" prefix
Manually inserted prefix

Finally, if I comment out the "\usepackage{unicode-math}" and the corresponding "\setmathfont" everything works as expected (although the math font is Computer Modern, of course):

With unicode-math package commented out

The question is: It's a bug or am I doing something wrong?

Best Answer

According to the manual, "[b]y default units are typeset as \mathrm{<unit prefix><unit>}". In unicode-math this switches to your current text font which will generally not have mathematical characters like \mu. You can use \symrm instead to get an upright font while still using your selected math typeface by changing unit code, either as an argument to the affected axis or once with \pgfplotsset:

% !TeX TS-program = lualatex
\documentclass{article}
\usepackage{tikz}

\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont{Libertinus Serif}
\setmathfont{Libertinus Math}

\usepackage{siunitx}
\usepackage{pgfplots}
\usepgfplotslibrary{units}

\begin{document}
   % \pgfplotsset{unit code/.code 2 args={\symrm{#1#2}}} % This line could be added once to apply the change to all `axis` environments in the document.
    \begin{tikzpicture}
    \begin{axis}[
        width=.5\textwidth,
        xlabel=$t$, x unit=s, change x base, x SI prefix=micro,
        ylabel=$v_g$, y unit=V,
        unit code/.code 2 args={\symrm{#1#2}},% Alternatively this applies it only once
        ]
        \addplot coordinates {(0,0) (100e-6,1) (200e-6,0.5) (300e-6,0.5)};
    \end{axis}
    \end{tikzpicture}
\end{document}