latex3 / unicode-math

XeLaTeX/LuaLaTeX package for using unicode/OpenType maths fonts
http://ctan.org/pkg/unicode-math
LaTeX Project Public License v1.3c
241 stars 28 forks source link

problem with math versions and operator font #468

Open John02139 opened 6 years ago

John02139 commented 6 years ago

Description

When changing the math font using \mathversion under unicode-math, an error message appears stating "math alphabet identifier \mathrm is undefined in math version..." (although three clicks past the error prompts will get to correct output). The error message is eliminated by setting the operator font, e.g., as \setoperatorfont\symup. The MWE below illustrates this behavior.

Check/indicate

Minimal example demonstrating the issue

% !TEX TS-program = lualatex

\documentclass[11pt]{article}
\usepackage{mathtools} 
\usepackage{xparse}
\usepackage[math-style=TeX]{unicode-math}

\setmainfont[]{STIX Two Text}
\setmathfont[Scale=MatchUppercase]{STIX Two Math} 

\setmathfont[version = GFS, Scale=MatchUppercase]{GFS Neohellenic Math}

\DeclareDocumentEnvironment{GFSsans}{}{%
   \setmainfont{GFS Neohellenic}[Scale=MatchUppercase, WordSpace=0.7]\mathversion{GFS}}{%\setoperatorfont\symup}{ % commented out for illustration
}

\DeclareDocumentCommand{\Equation}{}{%
   \[ \lim_{n\rightarrow\infty} \lim_{m\rightarrow\infty} \cos^{2n}\left( m!\pi x \right) = \begin{cases} 1 & \text{if $x$ is rational} \\ 0 & \text{if $x$ is irrational} \end{cases} 
   \]
}

\begin{document}

\Equation

\begin{GFSsans}
   \Equation
\end{GFSsans}

\end{document}

Further details

wspr commented 6 years ago

Thanks for reporting this. I won’t be able to address in the near-term.

I am guessing that whenever a new maths font is specified for a particular mathversion I need to loop over all maths alphabets and set them explicitly. I’m not sure how LaTeX’s hooks or data structures will allow me to automate this.

jfbu commented 6 years ago

Something looks strange in the status of \mv@normal, \mathrm<space> and \alpha@list after unicode-math executes a \setmainfont. From this

\documentclass{article}

\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont[]{STIX Two Text}

\makeatletter

\typeout{\meaning\mv@normal}

\typeout{\expandafter\meaning\csname mathrm \endcsname}

\typeout{\meaning\alpha@list}

\begin{document}
\end{document}

we get (I added empty lines for readability)

mv@normal: macro:->\getanddefine@fonts \symoperators \OT1/cmr/m/n \install@math
alphabet \mathrm  {\select@group \mathrm  \M@TU \TU/STIXTwoText(1)/m/n }\getand
define@fonts \symletters \OML/cmm/m/it \getanddefine@fonts \symsymbols \OMS/cms
y/m/n \getanddefine@fonts \symlargesymbols \OMX/cmex/m/n \install@mathalphabet 
\mathbf  {\select@group \mathbf  \M@TU \TU/STIXTwoText(1)/bx/n }\install@mathal
phabet \mathsf  {\select@group \mathsf  \M@OT1 \OT1/cmss/m/n }\install@mathalph
abet \mathit  {\select@group \mathit  \M@TU \TU/STIXTwoText(1)/m/it }\install@m
athalphabet \mathtt  {\select@group \mathtt  \M@OT1 \OT1/cmtt/m/n }

mathrm<space>: macro:->\no@alphabet@error \mathrm 

alpha@list: macro:->\alpha@elt \mathbf  \M@OT1 \OT1/cmr/bx/n \alpha@elt \mathsf
  \M@OT1 \OT1/cmss/m/n \alpha@elt \mathit  \M@OT1 \OT1/cmr/m/it \alpha@elt \mat
htt  \M@OT1 \OT1/cmtt/m/n \alpha@elt \mathrm  \no@alphabet@error \no@alphabet@e
rror 

Notice that the \alpha@list seems to contradict the \install@mathalphabet\mathrm in mv@normal.

but commenting out unicode-math and keeping only fontspec, we get this which of course ignores Stix font but at least shows coherent structure.

mv@normal: macro:->\getanddefine@fonts \symoperators \OT1/cmr/m/n \getanddefine
@fonts \symletters \OML/cmm/m/it \getanddefine@fonts \symsymbols \OMS/cmsy/m/n 
\getanddefine@fonts \symlargesymbols \OMX/cmex/m/n \install@mathalphabet \mathb
f  {\select@group \mathbf  \M@OT1 \OT1/cmr/bx/n }\install@mathalphabet \mathsf 
 {\select@group \mathsf  \M@OT1 \OT1/cmss/m/n }\install@mathalphabet \mathit  {
\select@group \mathit  \M@OT1 \OT1/cmr/m/it }\install@mathalphabet \mathtt  {\s
elect@group \mathtt  \M@OT1 \OT1/cmtt/m/n }

mathrm<space>: macro:->\relax \ifmmode \else \non@alpherr \mathrm  \fi \use@mat
hgroup \M@OT1 \symoperators 

alpha@list: macro:->\alpha@elt \mathbf  \M@OT1 \OT1/cmr/bx/n \alpha@elt \mathsf
  \M@OT1 \OT1/cmss/m/n \alpha@elt \mathit  \M@OT1 \OT1/cmr/m/it \alpha@elt \mat
htt  \M@OT1 \OT1/cmtt/m/n 
jfbu commented 6 years ago

ah ok that bit of source2e (code of \SetMathAlphabet@, File r: ltfssdcl.dtx Date: 2016/02/18 Version v3.0r 213):

If the math alphabet was defined via \DeclareSymbolFontAlphabet we have remove
its external definition and add it as a normal math alphabet to every version
before trying to change it in one version.

seems to be the code branch taken and it also has this comment

Put it into the \alpha@list with default `error'

Thus the situation seems to originate from

\__fontspec_setmathrm_hook:nn #1#2->\SetMathAlphabet \mathrm {normal}\g_fontspe
c_encoding_tl \g__fontspec_mathrm_tl \mddefault \updefault \SetMathAlphabet \ma
thit {normal}\g_fontspec_encoding_tl \g__fontspec_mathrm_tl \mddefault \itdefau
lt \SetMathAlphabet \mathbf {normal}\g_fontspec_encoding_tl \g__fontspec_mathrm
_tl \bfdefault \updefault 
#1<-,
#2<-STIX Two Text

taking into account that \mathrm was initially defined via \DeclareSymbolFontAlphabet.

alchem0x2A commented 4 years ago

A (maybe dirty) work-around is to explicitly specify \usepackage[mathrm=sym]{unicode-math} as pointed out in the documentation of Fira Math font (https://github.com/firamath/firamath). It seems to be at least give consistent results for your example.

However as a newbie to typesetting I would like some experts looking into the issue in unicode-math

yushiyangk commented 3 years ago

Did this behaviour get changed? Using LuaLaTeX I no longer get this error when using \sin or \exp with \mathversion; however, now when I use these operators in a section heading it breaks when creating pdf bookmarks, even with hyperref's psdextra option.

On the other hand, writing \mathrm{exp} explicitly with \mathversion still gives the error "math alphabet identifier \mathrm is undefined in math version..."