Hello, can \directlua be nested, i.e. \directlua0{\directlua0{tex.print('foo()')}} to call Lua function 'foo'? Of course this example is contrived, but I could imagine the situation where Lua code is created by TeX macros which in turn use \directlua). Thanks in advance, Jonathan
Jonathan Sauer wrote:
Hello,
can \directlua be nested, i.e.
\directlua0{\directlua0{tex.print('foo()')}}
to call Lua function 'foo'?
\directlua is never called recursively, even though it sometimes looks like it is. The example above is syntactically wrong, but this works (ignoring \catcode issues): \directlua0{tex.print('\directlua0{foo()}')} however, executation is serialized. The output of tex.print is not seen until the end of the outer \directlua is reached. Luatex's tex engine actually sees something like this: \directlua0{}\directlua0{foo()} which is fine, as it does not recurse. Best wishes, Taco
Hello,
can \directlua be nested, i.e.
\directlua0{\directlua0{tex.print('foo()')}}
to call Lua function 'foo'?
\directlua is never called recursively, even though it sometimes looks like it is. The example above is syntactically wrong, but this works (ignoring \catcode issues):
\directlua0{tex.print('\directlua0{foo()}')}
I'm not sure why: In your example, the second \directlua is expanded while the first \directlua expands its argument, just like in my example. You would have to surround it with \unexpanded or \detokenize (and \luaescapestring because of the backslash) to pass it unmodified to tex.print.
however, executation is serialized. The output of tex.print is not seen until the end of the outer \directlua is reached.
Ah. So \directlua first reads its second parameter and then expands it completely, instead of expanding tokens until it reaches the closing brace delimiting its second parameter, and therefore does not include the output of tex.print? (just like \edef does)
Best wishes, Taco
Jonathan
Jonathan Sauer wrote:
Hello,
can \directlua be nested, i.e.
\directlua0{\directlua0{tex.print('foo()')}}
to call Lua function 'foo'? \directlua is never called recursively, even though it sometimes looks like it is. The example above is syntactically wrong, but this works (ignoring \catcode issues):
\directlua0{tex.print('\directlua0{foo()}')}
I'm not sure why: In your example, the second \directlua is expanded while the first \directlua expands its argument, just like in my example. You would have to surround it with \unexpanded or \detokenize (and \luaescapestring because of the backslash) to pass it unmodified to tex.print.
Yes, that's what I meant. My assumption was that everything is catcode 11 or 12 or some other harmless value. \directlua does indeed read the entire braced argument at once. Best wishes, Taco
Hello
My assumption was that everything is catcode 11 or 12 or some other harmless value. \directlua does indeed read the entire braced argument at once.
Thanks for your reply. When coming home yesterday, I experimented a little bit, and the results do not seem to match your answer. In Plain TeX: \directlua0{% tex.sprint('A')% \directlua0{tex.sprint('B')}% tex.sprint('C')% } This results in "LuaTeX error [string "luas[0]"]:1: attempt to index global 'Btex' (a nil value)". So it seems that the first \directlua expands its braced argument while reading it (possibly using get_x_token), thus also expands the second \directlua which inserts "B" into the token stream, resulting in "Btex.sprint('C')". This on the other hand works: \directlua0{% tex.sprint('A')% \directlua0{tex.sprint('tex')}% .sprint('C')% } In this case, the second \directlua inserts the table name "tex" into the token stream, resulting in "tex.sprint('C')". What do you think?
Best wishes, Taco
Jonathan
Jonathan Sauer wrote:
Hello
My assumption was that everything is catcode 11 or 12 or some other harmless value. \directlua does indeed read the entire braced argument at once.
Thanks for your reply. When coming home yesterday, I experimented a little bit, and the results do not seem to match your answer. In Plain TeX:
\directlua0{% tex.sprint('A')% \directlua0{tex.sprint('B')}% tex.sprint('C')% }
This results in "LuaTeX error [string "luas[0]"]:1: attempt to index global 'Btex' (a nil value)".
Yes, that's what I meant to say (you input is invalid). We have a bit of a communication error, I'm afraid. Do it like this: \let\\\relax \directlua0{% tex.sprint('A')% tex.sprint('\\directlua0{B}')}% tex.sprint('C')% } Best wishes, Taco
Taco Hoekwater wrote:
Jonathan Sauer wrote:
Hello
My assumption was that everything is catcode 11 or 12 or some other harmless value. \directlua does indeed read the entire braced argument at once. Thanks for your reply. When coming home yesterday, I experimented a little bit, and the results do not seem to match your answer. In Plain TeX:
\directlua0{% tex.sprint('A')% \directlua0{tex.sprint('B')}% tex.sprint('C')% }
This results in "LuaTeX error [string "luas[0]"]:1: attempt to index global 'Btex' (a nil value)".
Yes, that's what I meant to say (you input is invalid). We have a bit of a communication error, I'm afraid. Do it like this:
\let\\\relax \directlua0{% tex.sprint('A')% tex.sprint('\\directlua0{B}')}% tex.sprint('C')% }
i suppose that you meant ... \directlua0{% tex.sprint('A')% tex.sprint('\string\\directlua0{tex.sprint("B")}')% tex.sprint('C')% }
tex.sprint('\\directlua0{B}')}%
^ no and B needs to be printed to tex (otherwise an lua error) ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Hello,
Thanks for your reply. When coming home yesterday, I experimented a little bit, and the results do not seem to match your answer. In Plain TeX:
\directlua0{% tex.sprint('A')% \directlua0{tex.sprint('B')}% tex.sprint('C')% }
This results in "LuaTeX error [string "luas[0]"]:1: attempt to index global 'Btex' (a nil value)".
Yes, that's what I meant to say (you input is invalid).
Well, it is invalid, because it generates invalid Lua code (which would not be the case, if a table "Btex" existed and contained a field "sprint").
We have a bit of a communication error, I'm afraid.
Yes, unfortunately my original questions was not clear enough. In the above example, I do not want to output "B" after the outer \directlua (and between "A" and "C"), but I want to insert it *into the Lua code* executed by the outer \directlua (as the example in your answer to Hans does: It inserts "tex.sprint('B')" between "tex.sprint('A')" and "tex.sprint('C')" in the outer \directlua). Or, in my original example \directlua0{\directlua0{tex.print('foo()')}} I wanted the inner \directlua to insert "foo()" into the Lua code executed by the outer \directlua (so that the outer \directlua calls function "foo" with no parameters). And judging from your answer yesterday, | \directlua does indeed read the entire braced argument at once I thought this would not be possible. Now I'm wondering if this possibility of nesting \directlua is accidental (and maybe leaks memory) or not.
Best wishes, Taco
Thanks in advance, Jonathan
participants (3)
-
Hans Hagen
-
Jonathan Sauer
-
Taco Hoekwater