为什么我会在这里陷入无限递归?

为什么我会在这里陷入无限递归?

我在数学模式中使用了两种不同的字体,而主文本也是如此,因此数学模式中的数字和文本中的数字在字体上不匹配,这看起来很糟糕。因此,我想只更改文本中数字的字体。

我找到了一种方法来做到这一点,答案如下: https://tex.stackexchange.com/a/10266/127289

这是我的整个项目,正在 Overleaf 上使用 XeTeX 进行编译:

\documentclass{article}
\usepackage{fontspec}

\XeTeXinterchartokenstate=1
\chardef\CharNormal=0
\makeatletter
% Test for old and new versions of the latex kernel
\ifx\e@alloc@intercharclass@top\@undefined
    \chardef\CharBound=255
\else
    \chardef\CharBound=\e@alloc@intercharclass@top
\fi
\makeatother
\newXeTeXintercharclass\CharNumbers
\XeTeXcharclass`0=\CharNumbers
\XeTeXcharclass`1=\CharNumbers
\XeTeXcharclass`2=\CharNumbers
\XeTeXcharclass`3=\CharNumbers
\XeTeXcharclass`4=\CharNumbers
\XeTeXcharclass`5=\CharNumbers
\XeTeXcharclass`6=\CharNumbers
\XeTeXcharclass`7=\CharNumbers
\XeTeXcharclass`8=\CharNumbers
\XeTeXcharclass`9=\CharNumbers
\newtoks\TokSetfont
\TokSetfont={\begingroup\fontspec{Latin Modern Mono}}
\XeTeXinterchartoks\CharNormal\CharNumbers=\TokSetfont
\XeTeXinterchartoks\CharBound\CharNumbers=\TokSetfont
\XeTeXinterchartoks\CharNumbers\CharNormal={\endgroup}
\XeTeXinterchartoks\CharNumbers\CharBound={\endgroup}

\begin{document}

test test 12345

\end{document}

但是,这似乎不起作用。我一直收到相同的错误:

TeX capacity exceeded, sorry [save size=80000].

由于我的文档很小,这显然是某种递归错误。日志文件对我来说没有什么特别的启发性。没有重复的错误消息表明存在递归循环。

但是,如果我更改Latin Modern MonoArial,那么我会在日志文件中打印数百次:

.................................................
. fontspec info: "no-scripts"
. 
. Font Arial does not contain any OpenType `Script' information.
.................................................

这表明递归可能发生在调用中

\TokSetfont={\begingroup\fontspec{Latin Modern Mono}}? 可能不相关。

有人知道发生了什么吗?其他人可以尝试在其他环境中运行它并告诉我结果吗?我链接到的旧问题中似乎没有人遇到任何问题。

答案1

fontspec 命令的语法已更改。它现在可以有一个可选参数在后面主要参数,并在查找该参数时进入循环。如果可选参数为空,或者代码后面有 \relax,则会再次编译。

   \TokSetfont={\begingroup\fontspec{Latin Modern Mono}[]}

但我会定义一个字体系列并使用它:

\documentclass{article}
\usepackage{fontspec}
\newfontfamily\numfontfam{Latin Modern Mono}
\XeTeXinterchartokenstate=1
\chardef\CharNormal=0
\makeatletter
% Test for old and new versions of the latex kernel
\chardef\CharBound=\e@alloc@intercharclass@top
\makeatother
\newXeTeXintercharclass\CharNumbers
\XeTeXcharclass`0=\CharNumbers
\XeTeXcharclass`1=\CharNumbers
\XeTeXcharclass`2=\CharNumbers
\XeTeXcharclass`3=\CharNumbers
\XeTeXcharclass`4=\CharNumbers
\XeTeXcharclass`5=\CharNumbers
\XeTeXcharclass`6=\CharNumbers
\XeTeXcharclass`7=\CharNumbers
\XeTeXcharclass`8=\CharNumbers
\XeTeXcharclass`9=\CharNumbers
\newtoks\TokSetfont
\TokSetfont={\begingroup\numfontfam}
\XeTeXinterchartoks\CharNormal\CharNumbers=\TokSetfont
\XeTeXinterchartoks\CharBound\CharNumbers=\TokSetfont
\XeTeXinterchartoks\CharNumbers\CharNormal={\endgroup}
\XeTeXinterchartoks\CharNumbers\CharBound={\endgroup}

\begin{document}

test test 12345

\end{document}

相关内容