You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The issue seems to be with the f-expansion, which seems to expand the command \input as well.
Potential solutions
Replace \input with \exp_not:N \input
The simplest solution would be to change our definition of function print() to replace any occurence of \input with \exp_not:N \input. This solves this particular issue but is easily fooled by e.g. \csname input\endcsname and does not solve the bigger issue, just one of the symptoms.
Controlled expansion
One solution would be to better control the expansion when storing the output of Lua in a tl variable. Namely, we can expand \l_tmpa_tl in a single expansion and \lua_now:n in two expansions, according to interface3.pdf. Therefore, changing our code as follows and adding the correct number of \exp_after:wN to the correct places should do the trick:
\tl_set:Nn
#1
{
\lua_now:n
{ \l_tmpa_tl }
}
However, this solution seems brittle because it relies on the fact that a specific number of expansions is sufficient, which does not seem guaranteed by expl3.
Using token.set_macro()
Instead of collecting the result of \lua_now:n, we might redefine the function print(text) to accumulate the individual lines of text in an auxiliary expl3 variable such as \l_tmpa_tl using e.g. token.set_macro("l_tmpa_tl"):
token.set_macro("l_tmpa_tl", "")
localfunctionprint(input)
input=tostring(input)
localvalue=token.get_macro("l_tmpa_tl")
forlineininput:gmatch("[^\r\n]+") dovalue=value..line.."\n"endtoken.set_macro("l_tmpa_l", value)
end
Then, we can simply execute the code and assign the content of \l_tmpa_tl to the output variable:
\lua_now:V
\l_tmpa_tl
\tl_set:NV
#1
\l_tmpa_tl
This is an intriguing solution but it likely would have trouble with e.g. print([[\catcode`\%=12]]). Currently, this print() statement would affect how percent signs in the following print() statements are tokenized, whereas with the above solution, the percent signs in the following print() statements would still be tokenized as before.
The text was updated successfully, but these errors were encountered:
Description
Using
\luabridge_tl_set:Nn
causes issues in LuaTeX when\input
is produced by the Lua code.Minimal reproducing example
File
example.tex
File
example.lua
File
example_input.tex
Expected behavior
Executing
luatex example
should produce the following text on the terminal, likepdftex --shell-escape example
does:Actual behavior
Executing
luatex example
produces the following text on the terminal>Discussion
Cause of the issue
This behavior seems to be due to the way we define the function
\luabridge_tl_set:Nn
for LuaTeX:lt3luabridge/lt3luabridge.dtx
Lines 496 to 525 in 65ee16e
Specifically, in the Lua code, we redefine
print(text)
to calltex.print()
on the individual lines intext
:lt3luabridge/lt3luabridge.dtx
Lines 506 to 516 in 65ee16e
Then we collect the lines in a tl variable:
lt3luabridge/lt3luabridge.dtx
Lines 519 to 524 in 65ee16e
The issue seems to be with the
f
-expansion, which seems to expand the command\input
as well.Potential solutions
Replace
\input
with\exp_not:N \input
The simplest solution would be to change our definition of function
print()
to replace any occurence of\input
with\exp_not:N \input
. This solves this particular issue but is easily fooled by e.g.\csname input\endcsname
and does not solve the bigger issue, just one of the symptoms.Controlled expansion
One solution would be to better control the expansion when storing the output of Lua in a tl variable. Namely, we can expand
\l_tmpa_tl
in a single expansion and\lua_now:n
in two expansions, according tointerface3.pdf
. Therefore, changing our code as follows and adding the correct number of\exp_after:wN
to the correct places should do the trick:However, this solution seems brittle because it relies on the fact that a specific number of expansions is sufficient, which does not seem guaranteed by expl3.
Using
token.set_macro()
Instead of collecting the result of
\lua_now:n
, we might redefine the functionprint(text)
to accumulate the individual lines oftext
in an auxiliary expl3 variable such as\l_tmpa_tl
using e.g.token.set_macro("l_tmpa_tl")
:Then, we can simply execute the code and assign the content of
\l_tmpa_tl
to the output variable:This is an intriguing solution but it likely would have trouble with e.g.
print([[\catcode`\%=12]])
. Currently, thisprint()
statement would affect how percent signs in the followingprint()
statements are tokenized, whereas with the above solution, the percent signs in the followingprint()
statements would still be tokenized as before.The text was updated successfully, but these errors were encountered: