7

Trying to get the following to work:

\documentclass{scrbook}

\usepackage[x11names]{xcolor}

\usepackage{unicode-math}

\setmainfont[%
    Numbers=Lowercase,% Hanging/OldStyle numbers through Lowercase numbers
    Color=Orange4,% Toggle color for debugging (should be xcolor name)
]{TeX Gyre Pagella}% Load existing font, see also https://tex.stackexchange.com/a/351100/120853

\setmathfont[%
    Color=Green4,% Toggle color for debugging (should be xcolor name)
]{TeX Gyre Pagella Math}% Load existing font, see also https://tex.stackexchange.com/a/351100/120853

\newfontfamily{\unitnumberfont}[
    Numbers=Uppercase,% Lining numbers (as opposed to OldStyle) for quantities
    Color=Blue4,% Toggle color for debugging (should be xcolor name)
]{TeX Gyre Pagella}% Load existing font, see also https://tex.stackexchange.com/a/351100/120853

\usepackage{siunitx}% Typesetting physical units correctly
    \sisetup{%
        % For siunitx v3, the following block replaces what used to be
        % `detect-all` (removed in that release, see
        % https://github.com/josephwright/siunitx/blob/main/CHANGELOG.md#v300 ).
        % It allows the `siunitx` output to match surrounding text/math (italics/bold/...,
        % roman/sans/... etc. as much as possible)
        % mode=match,%
        propagate-math-font=true,
        reset-math-version=false,
        reset-text-family=false,
        reset-text-series=false,
        reset-text-shape=false,% Not part of official fix, but still added (see https://collaborating.tuhh.de/alex/latex-git-cookbook/-/issues/8)
        text-family-to-math=true,
        text-series-to-math=true,
        %
        % Requires siunitx v3+, see also https://tex.stackexchange.com/a/468031/120853,
        % https://github.com/josephwright/siunitx/issues/532 :
        text-font-command=\unitnumberfont,
    }%

    \DeclareSIUnit{\watthour}{Wh}%

    \DeclareSIQualifier{\testingqual}{test}

\begin{document}
Rest of document has hanging numerals (123456789) thus use \texttt{unitnumberfont} as a solution, see https://tex.stackexchange.com/questions/468027/siunitx-and-xelatex-with-oldstyle-in-text-but-lining-in-math .

\newcommand*{\testunit}{\mega\watthour\per\meter\cubed}
% \renewcommand*{\testunit}{\mega\watthour\testingqual\per\meter\cubed}% Toggle this line on and off

\newcommand{\testtext}{%
    Hello \qty[per-mode=fraction]{123.456}{\testunit} World

    \textbf{Hello \qty[per-mode=fraction]{123.456}{\testunit} World}

    \textit{Hello \qty[per-mode=fraction]{123.456}{\testunit} World}

    \textit{\textbf{Hello \qty[per-mode=fraction]{123.456}{\testunit} World}}
}

\subsection*{Text mode}

Has matching capability, but \texttt{SIQualifier} breaks.

\sisetup{mode=text}

\testtext{}% This line errors out if `\testunit` contains `\testingqual`

\subsection*{Math mode}

Loses matching capability, but \texttt{SIQualifier} works.

\sisetup{mode=math}

\testtext{}
\end{document}

Which produces (related topic):

enter image description here

siunitx matching the surrounding font is the desired behaviour and works inside text mode (related).


However, including \testingqual via

\DeclareSIQualifier{\testingqual}{test}

in the unit (the renewcommand line) breaks the text mode:

! LaTeX Error: \mathrm allowed only in math mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.68 \testtext
            {}% This line errors out if `\testunit` contains `\testingqual`
? 

math mode continues to work (text mode line commented out for this to compile):

enter image description here


I can confirm this broke in v3.0.33, since that's the version I'm using to compile the above snippets, whereas the project this is used in is still based on v3.0.32 (compiled via Docker, image alexpovel/latex). It works there.

The whole mode switching and matching is a bit confusing to me, so couldn't fix this myself.

1 Answer 1

3

An earlier change has an oversight in one of the internals. You should ad

\ExplSyntaxOn
\cs_gset_protected:Npn \__siunitx_print_text_scripts_one:Nn #1#2
  {
    \group_begin:
      \tl_set:Nn \l__siunitx_print_tmp_tl {#2}
      \__siunitx_print_text_replace:N \l__siunitx_print_tmp_tl
      \__siunitx_print_replace_font:N \l__siunitx_print_tmp_tl
    \exp_args:NNV \group_end:
    #1 \l__siunitx_print_tmp_tl
  }
\ExplSyntaxOff

after loading siunitx until a fix hits CTAN.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .