latex3 / babel

The babel system for LaTeX, LuaLaTeX and XeLaTeX
LaTeX Project Public License v1.3c
124 stars 34 forks source link

Clash between East Asian scripts and \settowidth in LuaLaTeX #247

Closed marcschulder closed 1 year ago

marcschulder commented 1 year ago

I am running into an error when trying to compile a document in LuaLaTeX that both imports East Asian script support for babel and uses the \settowidth command.

Minimal example:

\documentclass{article}
\usepackage{babel}
\babelprovide[import]{japanese}
\newlength{\testwidth}
\settowidth{\testwidth}{.}

\begin{document}
Test
\end{document}

Error message:

warning  (hyphenation): bad specification: [\directlua]:1: attempt to index a nil value (local 'props')
.
\@settodim #1#2#3->\setbox \@tempboxa \hbox {{#3}}
                                                  #2#1\@tempboxa \setbox \@t...

l.5 \settowidth{\testwidth}{.}

I have confirmed this for Japanese, Chinese and Korean. Removing either the \settowidth or the \babelprovide command resolves the issue. I tried some other language imports with different script needs, such as Hebrew and Arabic, but those compile fine. It also compiles under XeLaTeX and pdfLaTeX.

I'm running the MacTeX 2023 distribution and updated all packages on 29 June. Babel is at version 3.90. I have also reproduced the issue on Overleaf, using their TeXLive 2022 distribution (although Overleaf initially did not report the issue until I turned off force-compile).

u-fischer commented 1 year ago

you shouldn't do typesetting in the preamble, not all font and language related setup is already done there (\settowidth stores the content in a box and so does typesetting). If you move it into the begindocument/end hook it works:

\documentclass{article}
\usepackage{babel}
\babelprovide[import]{japanese}
\newlength{\testwidth}

\AddToHook{begindocument/end}{\settowidth{\testwidth}{.}}
\begin{document}
Test \the\testwidth
\end{document}
marcschulder commented 1 year ago

Thank you, I didn't realise that! This has indeed resolved the issue.