# Passing LaTeX blocks to Lua

I want to use Lua in order to process some blocks of text in lualatex. It would be fine also if I were to pass hboxes directly, actually.

I attempted the following.

\documentclass{article}
\usepackage{luacode}
\newcommand\mytest[1]{%
\directlua{
x=[[\luatexluaescapestring{#1}]]
tex.print(x)
}}
\begin{document}
\mytest{abc}
\mytest{ab\c{c}\i}
\end{document}


If the argument of \mytest is a string without any commands, then everything works as expected; however, if that contains any macro the result is sometimes (almost always) wrong; see, for instance, the way \i is expanded (while if I use the unicode dotless i, there are no problems). Is there a canonical way to proceed and obtain what I need?

• use x="\luaescapestring{#1}"
– user2478
May 31 '17 at 19:16

Note that in recent latex relases the natural name of the command is \luaescapestring not \luatexluaescapestring you don't want to use both \luaescapestring and [[ ]] string form, also you want to use \unexpanded to stop macros expanding before being passed to lua.

Note the luacode package that you had loaded has a luacode environment to address some of these issues but here I just use \directlua as you had used rather than the luacode package environments.

\documentclass{article}

\newcommand\mytest[1]{%
\directlua{
x='\luaescapestring{\unexpanded{#1}}'
tex.sprint(x)
}}
\begin{document}
\mytest{abc}
\mytest{ab\c{c}\i}
\end{document}


You give no indication of what you wnat to do with the text in Lua, as related to "pass hbox directly you can see the content of any box register from within Lua.

In the following the node list in an hbox register is accessed (but not changed here) and the box \usebox call is printed back out.

\documentclass{article}

\newbox\mybox
\begin{document}

\sbox\mybox{this and that}
\directlua{
local b = tex.box['mybox']
print('BOX' .. node.type(b))
tex.sprint('\string\\usebox\string\\mybox')
}
\end{document}

• That was my mistake. Now the actual file compiles properly. Jun 1 '17 at 12:52
• By the way: the aim of my code was to subtly adjust font size (if and when needed) in order to have the text fitting within a given length. This can be done also in latex but the code in lua appears a lot nicer. Thanks again Jun 1 '17 at 13:35
• @lucag73 I'm surprised lua helps that much for that use, since you have to come back to tex to typeset and find the length but no matter; glad you got something working:-) Jun 1 '17 at 15:36

Since you're loading the luacode package, you might as well make use of that package's \luastringN macro, which converts its argument to a Lua string without performing expansion.

Typing x=\luastringN{<some arg>} should be easier to type, and easier to remember, than x='\luaescapestring{\unexpanded{<some arg>}}', right? :-)

\documentclass{article}
\usepackage{luacode}
\newcommand\mytest[1]{%
\directlua{x = \luastringN{#1}
tex.sprint(x) }}
\begin{document}
\mytest{abc}
\mytest{ab\c{c}\i}
\end{document}


Aside: The luacode package also provides the macros \luastring, which performs full expansion on the argument before converting it into a Lua string, and \luastringO`, which performs one expansion step, on the first token.)