latex3 / latex2e

The LaTeX2e kernel
https://www.latex-project.org/
LaTeX Project Public License v1.3c
1.9k stars 263 forks source link

local math alphabets reset incorrectly after nested math #1101

Open davidcarlisle opened 1 year ago

davidcarlisle commented 1 year ago

Brief outline of the bug

https://tex.stackexchange.com/questions/689560/many-instances-of-declaresymbolfont-leads-to-odd-behavior

This is more or less the same issue as #1028 but without an explicit change of math version. After a nested math list, the local math alphabet assignments in the outer list are set incorrectly.

Minimal example showing the bug

\RequirePackage{latexbug} 
\documentclass{article}

\DeclareSymbolFont{abc1}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abc2}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abc3}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abc4}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abc5}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abc6}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abc7}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abc8}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abc9}{OT1}{cmr}{m}{n}
\DeclareSymbolFont{abca}{OT1}{cmr}{m}{n}
\DeclareMathAlphabet\mathscr{OT1}{cmr}{m}{it}
\DeclareMathAlphabet\mathfrak{U}{euf}{m}{n}

\def\x#1{\showthe\textfont14 }
\begin{document}
% incorrect
\x0
$\mathscr{F}_x
\hbox{$M\x1$\x2}
 \mathfrak{p}\x3$
\x4

% correct
$\mathscr{F}_x M \mathfrak{p}$
\x5

\end{document}

image

Log file (required) and possibly PDF file

dd840.log

davidcarlisle commented 1 year ago

I'm not sure how best to fix this.

In the test, the nested math is just \hbox{$x$} so the most efficient thing to do would be to do nothing so that as far as math font allocations go, the nested math just acts as if it were in the outer formula, so $x$ does nothing and if it did have \mathxxx it would add to the mathalphabet count of the outer formula.

The alternative is to arrange that it effectively does a \mathversion reset so the nested math has its own local math alphabet setup that then has to be restored at the end of the group. This has some cost overhead (especially as these things usually in \mathchoice so get done 4 times), but would for example mean that you could use different \mathxx in different array cells with each cell acting as a local math alphabet scope.

for bold symbols in #1028 / #1030 we reset the math version but there you are explicitly changing math version so the costs seem more reasonable.

I haven't actually got either strategy working as I'd like for the test case above but would be interested to know others thoughts on strategy.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity.

davidcarlisle commented 9 months ago

related

https://tex.stackexchange.com/a/706491/1090

github-actions[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity.

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity.

u-fischer commented 1 week ago

related (extracted from https://tex.stackexchange.com/q/727625/2388)

\documentclass{article}

\DeclareSymbolFont{symbols1}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols2}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols3}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols4}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols5}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols6}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols7}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols8}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols9}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{symbols10}     {OMS}{cmsy}{m}{n}

\DeclareMathAlphabet{\pazocal}{OMS}{zplm}{m}{n} % 

\begin{document}

$\mathsf{Enc}  \mbox{$m$} \mbox{$\pazocal{M}$} $
\end{document}

image