In general, we recommend that Lua chunks are defined separately from expl3
(or indeed standard LaTeX) code, so they can simply be loaded using require()
. Where you do want a short piece of Lua 'inside' expl3
, using ~
is the correct action. However, when you use tex.print
or similar to return tokens to TeX, LuaTeX tokenizes them using the current catcode regime. As such, your space gets ignored. However, there is an optional argument to tex.print()
which takes a category code table number: this can be used to apply any catcode setup one likes.
At present, we are finalising category code table support for expl3
. The 'Lua side' is perhaps not quite right just yet, but it is workable:
\documentclass{article}
\usepackage{expl3}
\usepackage{l3cctab}
\begin{document}
\ExplSyntaxOn
\lua_now:e { tex.print(\int_use:N \c_document_cctab, "Hello~World!") }
\ExplSyntaxOn
\end{document}
(Eventually, l3cctab
will be part of the expl3
core.)
Category code tables are supported by LaTeX nowadays via ltluatex
: you could avoid loading l3cctab
entirely:
\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\makeatletter
\lua_now:e { tex.print(\number\catcodetable@latex, "Hello~World!") }
\makeatother
\ExplSyntaxOn
\end{document}