Open KuznetsovAleksey opened 2 years ago
well unicode-math is doing a lot of setup in begindocument and imho it does't expect that you set or change the fonts in the middle of the document. Do your setup in the preamble.
On the contrary, unicode-math expects this.
The package manual 2020/01/31 0.8q 4.4.1 Default ‘text math’ fonts "When selecting document fonts using fontspec commands such as \setmainfont, unicode-math inserts some additional code into fontspec that keeps the current default fonts ‘in sync’ with their corresponding \mathrm commands, etc. For example, in standard L A TEX, \mathsf doesn’t change even if the main document font is changed using \renewcommand\sfdefault{...}. With unicode-math loaded, after writing \setsansfont{Helvetica}, \mathsf will now be set in Helvetica."
Seems the manual should be corrected.
The bug is noted in my second point. More detail description is following.
When fontspec is loaded and the main fonts (rm, sf and tt) are not explicitly set in the preamble, Latin Modern fonts are used. One can replace them in the middle of the document via \setmainfont etc.
Since unicode-math automatically loads fontspec, one expects the same managing of text fonts, but the following error
! Extra \else. \SetMathAlphabet ...g #1\space \endcsname #1\else \@latex@error {Encoding sc...
occurs in some cases when the text fonts are replaced.
I hope that the samples help to locate the bug.
1) All is OK when the main fonts are explicitly set before formulae:
\usepackage{unicode-math} \setmainfont{.....} \begin{document}
or
\usepackage{unicode-math} \begin{document} ....... \setmainfont{.....} ..... $...$
2) All is OK when the math alphabet bound to the text font is not used in formula:
\usepackage{unicode-math} \begin{document} $ x=y $ \setmainfont{.....}
3) Error occurs after math aplphabet is used in formula and hereafter the proper text font is replaced:
\usepackage{unicode-math} \begin{document} $ x=y \mathrm{adcdefffi} $ \setmainfont{......}
! Extra \else. \SetMathAlphabet ...g #1\space \endcsname #1\else \@latex@error {Encoding sc...
This is due to "unicode-math inserts some additional code into fontspec that keepsv the current default fonts ‘in sync’ with their corresponding \mathrm commands".
As mentioned above, 'in sync' is also absent.
Best regards, Alexey
Sorry, formulae in examples were compiled by github, so the examples are attached in pdf. Alexey
On the contrary, unicode-math expects this.
Sorry but nowhere in this quote unicode-math speaks about changing the font after \begin{document}
.
All is OK when the main fonts are explicitly set before formulae: \usepackage{unicode-math} \setmainfont{.....} \begin{document}
Exactly. So why don't you this and setup the fonts in the preamble?
"Sorry but nowhere in this quote unicode-math speaks about changing the font after \begin{document}."
This directly follows from the fontspec manual.
"Exactly. So why don't you this and setup the fonts in the preamble?"
Yes I can to do this, but there are no reasons that I have to do this. This requirement is absent in the unicode-math manual.
All is OK when only fontspec is used:
\usepackage{fontspec} \begin{document}
\begin{equation} \mathrm x \end{equation} \setmainfont{Times New Roman}....
Error occurs when unicode-math is added:
\usepackage{fontspec} \usepackage{unicode-math} \begin{document}
\begin{equation} \mathrm x \end{equation} \setmainfont{Times New Roman}....
Such a behavior is either bug or feature. If this is the feature, it should be described in the manual.
This directly follows from the fontspec manual.
I don't think that the fontspec manual actively promotes the setting of fonts in the document body. But it explicitly tells you that math fonts must be set in the preamble:
When
\setmainfont
,\setsansfont
and\setmonofont
are used in the preamble, they also define the fonts to be used in maths mode inside the\mathrm
-type commands. This only occurs in the preamble because LATEX freezes the maths fonts after this stage of the processing.Such a behavior is either bug or feature.
Well the error is a bug. unicode-math
is effectively using \SetMathAlphabet
in the document and that is not allowed, it errors already in standard LaTeX. unicode-math
should empty the relevant hooks at the begin of the document. But if it corrects this error, you still won't be able to setup your fonts in the document.
\documentclass{article}
\usepackage{fontspec}
\usepackage{unicode-math}
\ExplSyntaxOn
\AddToHook{begindocument/end}{
\cs_set_eq:NN \__fontspec_setmainfont_hook:nn \use_none:nn
\cs_set_eq:NN \__fontspec_setsansfont_hook:nn \use_none:nn
\cs_set_eq:NN \__fontspec_setmonofont_hook:nn \use_none:nn
\cs_set_eq:NN \__fontspec_setmathrm_hook:nn \use_none:nn
\cs_set_eq:NN \__fontspec_setmathsf_hook:nn \use_none:nn
\cs_set_eq:NN \__fontspec_setmathtt_hook:nn \use_none:nn
\cs_set_eq:NN \__fontspec_setboldmathrm_hook:nn \use_none:nn
}
\ExplSyntaxOff
\begin{document}
adcdefffi \textsf{adcdefffi} \texttt{adcdefffi}
\begin{equation}
\mathrm{adcdefffi}\ \mathit{adcdefffi}\ \mathbf{adcdefffi}\ \mathsf{adcdef}\ \mathtt{adcdef}
\end{equation}
\setmainfont{STIX Two Text}
\begin{equation}
\mathrm{adcdefffi}\ \mathit{adcdefffi}\ \mathbf{adcdefffi}\ \mathsf{adcdef}\ \mathtt{adcdef}
\end{equation}
\end{document}
This bug occurs in very specific case, so I think it is enough to mention in the unicode-math manual that text fonts should be explicitly set in the preamble when this package is used.
Description
1) Manual of unicode-math 0.8q:
4.4.1 Default ‘text math’ fonts .... With unicode-math loaded, after writing \setsansfont{Helvetica} , \mathsf will now be set in Helvetica.
In the example bellow text fonts are replaced by \set... commands, but math fonts becomes unchanged (see pdf-attachment). TestMathFonts-crop.pdf
2) Commenting of \setmainfont{Latin Modern Roman} \setsansfont{Latin Modern Sans} \setmonofont{Latin Modern Mono} in the preamble provides the error:
! Extra \else. \SetMathAlphabet ...g #1\space \endcsname #1\else \@latex@error {Encoding sc... l.14 \setsansfont{Lato}
All is OK when \usepackage{unicode-math} is commented.
Add info or delete as appropriate:
Minimal example demonstrating the issue
Further details
TeXLive 2022 LuaHBTeX, Version 1.13.0 (TeX Live 2021) LaTeX2e <2020-10-01> patch level 4 L3 programming layer <2021-02-18> fontspec-luatex {2022/01/15}{2.8a} unicode-math-luatex {2020/01/31}{0.8q}