Why am I getting infinite recursion here?

I am using two different fonts for math mode as for my main text, so numbers in math mode and in the text do not match in font, which looks bad. Therefore, I want to change the font of only numbers in the text.

I found a way to do this with this answer: https://tex.stackexchange.com/a/10266/127289

Here is my entire project, being compiled on Overleaf with 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
\XeTeXcharclass0=\CharNumbers
\XeTeXcharclass1=\CharNumbers
\XeTeXcharclass2=\CharNumbers
\XeTeXcharclass3=\CharNumbers
\XeTeXcharclass4=\CharNumbers
\XeTeXcharclass5=\CharNumbers
\XeTeXcharclass6=\CharNumbers
\XeTeXcharclass7=\CharNumbers
\XeTeXcharclass8=\CharNumbers
\XeTeXcharclass9=\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}


However, this seems not to work. I keep getting the same error:

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

Since my document is tiny, this is obviously some kind of recursion bug. The log file contains nothing particularly enlightening, to me. There is no repeating error message indicative of a recursion loop.

However, if I change Latin Modern Mono to Arial, then I get this printed in the log file hundreds of times:

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


which suggests that the recursion may be happening in the call

\TokSetfont={\begingroup\fontspec{Latin Modern Mono}}? Maybe not related.

Does anyone have any insight into what is going on? Can someone else try to run this in some other environment and tell me the results? No one in the old question that I linked to seemed to be having any problem.

The syntax of the fontspec command has changed. It can now have an optional argument behind the main argument and it runs into the loop while looking for it. With an empty optional argument or a \relax behind the code compiles again.

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


But I would define a font family and use this:

\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
\XeTeXcharclass0=\CharNumbers
\XeTeXcharclass1=\CharNumbers
\XeTeXcharclass2=\CharNumbers
\XeTeXcharclass3=\CharNumbers
\XeTeXcharclass4=\CharNumbers
\XeTeXcharclass5=\CharNumbers
\XeTeXcharclass6=\CharNumbers
\XeTeXcharclass7=\CharNumbers
\XeTeXcharclass8=\CharNumbers
\XeTeXcharclass9=\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}

• This seems to override the font changes that should be applied by \texttt{} (numbers in string args passed to \texttt{} are rendered with Latin Modern Mono rather than Courier), which is a problem. Can this answer be amended to fix that? – Anonymous Mar 1 '18 at 20:23
• Ask a new question with an example. – Ulrike Fischer Mar 1 '18 at 20:34
• Nevermind, I just have to call wrap any call to texttt{} like so: \newfontfamily\numfontfam{Courier} \texttt{...} \newfontfamily\numfontfam{Latin Modern Mono}`... which is a bit of a pain but okay – Anonymous Mar 1 '18 at 20:54