I would like to define a command using Lua that will start and end a \savebox
depending on if it is starred or not. Let's call this command \switchcolumn
.
Here is the code (the first \switchcolumn
that starts the box is substituted with the literal text, since it is irrelevant to the question):
\documentclass[11pt, paper = B5]{scrbook}
\usepackage{fontspec}
\usepackage{luacode}
\usepackage{xparse}
\setmainfont{Libertinus Serif}
\begin{luacode}
dofile(kpse.find_file("mwe.lua"))
\end{luacode}
\NewDocumentCommand{\switchcolumn}{}{\directlua{SwitchColumn()}}
\begin{document}
\newsavebox{\aaa}
\savebox{\aaa}[0.5\textwidth] {
Hello World!
\switchcolumn
\end{document}
mwe.lua
:
function SwitchColumn()
tex.cprint(2, "}")
end
It doesn't work. The closing curly bracket doesn't end up in the TeX's engine input:
Runaway argument?
{ Hello World! \switchcolumn \par \end {document}
! File ended while scanning use of \@isavebox.
I tried tex.print
, tex.sprint
, it's all the same.
How to set things right?
}
. tex macros are not expanded while scanning for arguments.lrbox
environment rather than\savebox
and then write out\end{lrbox}
(although you would not need Lua for that)