I am using Archlinux with texlive. After updating my installation on 2022-01-02 to the newest Archlinux texlive-packets, some features of LuaLaTeX seem to have changed significantly. For example, tex.sprint(STRING)
does no longer work inside of the file name argument of \lstinputlisting{FILENAME}
.
Before the update, the following MWE was working:
\documentclass{minimal}
\usepackage{luacode}
\usepackage[procnames]{listings}
\begin{filecontents*}{before-file1_test_label.lst}
\LaTeX~macros work
\end{filecontents*}
\newcommand{\lstSHAName}[2]{before-\directlua{
local string = "#1"
tex.print(string .. '_test')
}_#2.lst}
\begin{document}
%\lstinputlisting{\lstSHAName{file1}{label}} % was working before, now crashes
\lstinputlisting{before-file1_test_label.lst} % still works, but requires setting path manually
\end{document}
If I compile with the new texlive version, the compilation halts and the following error message is shown:
! Missing \endcsname inserted.
<to be read again>
global
l.16 \lstinputlisting{\lstSHAName{file1}{label}}
What additionally raises the suspicion that the issues are related to the update is the fact that my co-worker with MikTeX on Windows still can compile the old code without any issues.
Work-around attempt
The idea was to use the tex.sprint(STRING)
-functionality of LuaLaTeX to print the \lstinputlisting
command with all options and afterwards let it get executed by LaTeX.
\documentclass{minimal}
\usepackage{luacode}
\usepackage[procnames]{listings}
\begin{filecontents*}{before-file1_test_label.lst}
\LaTeX~macros work
\end{filecontents*}
\newcommand{\printListing}[3]{%
\directlua{
local strA = "#1"
local strB = "#2"
local strC = "#3"
local string = "\\lstinputlisting[caption={" .. strC .. "}]{before-" .. strA .. "_test_" .. strB .. ".lst}"
tex.sprint(string)
}
}
\begin{document}
\printListing{file1}{label}{Test caption} % works
%\printListing{file1}{label}{Test caption with \LaTeX~macro} % throws an error due to expansion
\printListing{file1}{label}{Test caption with \\LaTeX{} macro} % works, but tilde would have also to be escaped differently
\end{document}
Passing LaTeX macros into \directlua
without escaping (line 22) leads to the error
! Undefined control sequence.
\S@10 ->\gdef \tf@size
{10}\gdef \sf@size {7}\gdef \ssf@size {5}
l.22
Escaping manually is somewhat possible. However, the original idea was to provide a wrapper macro for inexperienced LaTeX users. Having to know how to escape in LaTeX so that lua will process it correctly defeats the original purpose.
Questions
- Can this issue be solved by type conversion/expansion/escaping before piping the file path to
\lstinputlisting
? And if so, how (could not get it to work; always got the same error)? - Is there for my work-around attempt a way with
expl3
to pre-process the arguments before passing them on to\directlua
, so that lua does not expand the macros (for example passing on\\LaTeX
instead of\LaTeX
) ?
Installed texlive packages
Packages I think are relevant for the problem:
- texlive-bin 2021.5945-1, build date: 2021-12-27
- texlive-core 2021.61403-1, build date: 2021-12-27
- texlive-formats-extra 2021.57972-1, build date: 2021-04-06
- texlive-latexextra 2021.61405-1, build date: 2021-12-27
lstinputlisting
from Lua). For the "how to avoid escaping twice" part...