Changes

Jump to navigation Jump to search
1,243 bytes removed ,  13:32, 13 February 2012
m
Remove some stray sandbox text that had somehow ended up at the bottom.
To a first approximation, the interaction between TeX and Lua is straightforward. When TeX (i.e., the LuaTeX engine) starts, it loads the input file in memory and processes it token by token. When TeX encounters <code>\directlua</code>, it stops reading the file in memory, <em>fully expands the argument of <code>\directlua</code></em>, and passes the control to a Lua instance. The Lua instance, which runs with a few preloaded libraries, processes the expanded arguments of <code>\directlua</code>. This Lua instance has a special output stream which can be accessed using <code>tex.print(...)</code>. The function <code>tex.print(...)</code> is just like the Lua function <code>print(...)</code> except that <code>tex.print(...)</code> prints to a <em>TeX stream</em> rather than to the standard output. When the Lua instance finishes processing its input, it passes the contents of the <em>TeX stream</em> back to TeX.<ref>The output of <code>tex.print(...)</code> is buffered and not passed to TeX until the Lua instance has stopped.</ref> TeX then inserts the contents of the <em>TeX stream</em> at the current location of the file that it was reading; expands the contents of the <em>TeX stream</em>; and continues. If TeX encounters another <code>\directlua</code>, the above process is repeated.
As an exercise, imagine what happens when the following input is processed by LuaTeX. The answer is in the footnotes. <ref>In this example, two different kinds of quotations are used to avoid escaping quotes. Escaping quotes inside <code>\directlua</code> is tricky. The above was a contrived example; if you ever need to escape quotes, you can use the <code>\startluacode ... \stopluacode</code> syntax explained later.</ref>
<texcode>
[[Category:LuaTeX]]
[[Category:Programming]]
/texcode>
 
=== Namespaces ===
 
It is a good habit to put your custom-defined functions in their own namespace. The traditional namespace for this is
code>tex.sprint/ref> TeX then inserts the contents of the
/code> doesn't. So the following lines
code>\ctxlua/code>.
 
So, we need a function that can take a string like that, parse it, and turn it into the appropriate TeX code. LuaTeX includes a general parser based on PEG (parsing expression grammar) called [http://www.inf.puc-rio.br/roberto/lpeg/lpeg.html lpeg], and it makes writing little parsers positively joyful. (Once you've got the knack of it, at least.) For example, the above texcode>
% Create an environment that stores everything
% between \startdedentedtyping and \stopdedentedtyping
% in a buffer named 'dedentedtyping'.
\def\startdedentedtyping
{\dostartbuffer
[dedentedtyping]
[startdedentedtyping]
[stopdedentedtyping]}
 
% On closing the dedentedtyping environment, call the LuaTeX
% function dedentedtyping(), and pass it the contents of
% the buffer called 'dedentedtyping'
\def\stopdedentedtyping
{\ctxlua
{userdata.dedentedtyping(buffers.getcontent('dedentedtyping'))}}
/em> at the current location of the file that it was reading; expands the contents of the

Navigation menu