6

Today, I wanted to compile a document that worked flawlessly earlier that year. However, LuaLaTex was complaining about some code lines inside of a C++ listing. I boiled the problem down to the following minimal example:

test.tex:

\documentclass{article}
\usepackage{unicode-math}
\usepackage{listings}
\lstset{basicstyle=\scriptsize}
\begin{document}
\lstinputlisting{test.cpp}
\end{document}

test.cpp:

int main()
{
  return (int)(-1.l);
}

If I compile with lualatex test.tex I get the following :

(./test.cpp                                                                                                                                
! Missing number, treated as zero.
<to be read again> 
global 
l.3   return (int)(-1.l
                     );
?

If I uncomment the unicode-math package or the lstset command or use xelatex, it compiles just fine. What's wrong?

  • Welcome to TeX.SX! What is your lualatex --version? It works fine for me :/ – Phelype Oleinik Mar 28 '18 at 15:32
  • I can reproduce the problem on MikTeX's LuaLaTeX 1.07.0 (Win 10). An even shorter MWE is \documentclass{article} \usepackage{unicode-math} \usepackage{listings} \lstset{basicstyle=\scriptsize} \begin{document} \begin{lstlisting} - \end{lstlisting} \end{document} – moewe Mar 28 '18 at 15:32
  • 1
    @PhelypeOleinik it fails with texlive 2018 pretest – David Carlisle Mar 28 '18 at 15:33
  • @PhelypeOleinik I'm using LuaTeX, Version 1.0.4 (TeX Live 2017/Arch Linux). – Lycanthropist Mar 28 '18 at 19:10
  • 1
    What's wrong? No idea :) github.com/wspr/unicode-math/issues/454 – Will Robertson Mar 29 '18 at 5:15
7

It works if you make sure that scriptsize math is set up before listings starts:

enter image description here

\documentclass{article}
\usepackage{unicode-math}
\usepackage{listings}
\lstset{basicstyle=\scriptsize}

\begin{document}
\sbox0{\scriptsize $x$}
\lstinputlisting{test.cpp}
\end{document}
  • 3
    This walks, swims and quacks like a bug. Any idea what is to blame here? The minus seems to be the problematic character here. But using XeLaTeX solves the issue and so does not loading unicode-math. – moewe Mar 28 '18 at 15:45
  • @moewe see Ulrike's answer – David Carlisle Mar 29 '18 at 11:03
3

The problem is that fontspec/unicode-math reads font dimens with luacode. And while catcodes in the tex code are frozen when the code is read the first time this is not true for values you get from directlua: there the catcode settings of the execution time matters. So when trying to setup the font in the middle of lstlisting (which activates all chars) things explodes.

A similar problem was here Who's at fault with this error concerning csquotes and LuaLaTeX?

The problem can be resolved by explicitly setting the catcode table in the first argument of tex.sprint when reading the dimens. I added this suggestion to the github tracker of unicode-math.

\documentclass{article}
\usepackage{unicode-math}
\RequirePackage{luacode}
\begin{luacode}
local latexcatcodetable = luatexbase.registernumber("catcodetable@latex")
function fontspec.mathfontdimen(fnt, str)
    local mathdimens = luaotfload.aux.get_math_dimension(fnt, str)
    if mathdimens then
        tex.sprint(latexcatcodetable,mathdimens)
        tex.sprint(latexcatcodetable,"sp")
    else
        tex.sprint(latexcatcodetable,"0pt")
    end
end
\end{luacode}

\usepackage{listings}

\lstset{basicstyle=\scriptsize}
\begin{document}


\begin{lstlisting}
-
\end{lstlisting}

\end{document}
  • you can use latex defined as local latex = luatexbase.registernumber("catcodetable@latex") instead of luatexbase.catcodetables.latex then you don't need luatexbase (or luacode) packages. – David Carlisle Mar 29 '18 at 9:28
  • @DavidCarlisle I edited the answer (and I need luacode for the example ...) – Ulrike Fischer Mar 29 '18 at 9:35
  • I find it's usually simpler to use \directlua directly – David Carlisle Mar 29 '18 at 9:55
  • @DavidCarlisle Hm. yes. But I normally avoid directlua for more than three lines -- I'm getting to easily confused when I want to comment parts of the code. – Ulrike Fischer Mar 29 '18 at 10:47
  • Ohhhh of course. This isn’t the first bug like this to have reared its head. Thanks! – Will Robertson Mar 29 '18 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.