Changes

Jump to navigation Jump to search
1,987 bytes added ,  15:47, 28 January 2012
3-level structure greatly unneccesary. Now we have six main sections with 0-3 subsections each. Also: made Namespaces subsection more complete
= To Calling Lua from TeX and vice versa =
The interweaving of ConTeXt and Lua consists of two elements: first you tell TeX that you're starting some Lua code; then, once inside Lua, you need to use the appropriate functions to put things into the TeX stream.
</texcode>
=== Namespaces ===
It is a good habit to put your custom-defined functions in their own namespace. The traditional namespace for this is <code>userdata</code>:
-- if userdata doesn't exist yet, create it
userdata = userdata or {}
-- define a shorter synonym
u = userdata
-- create my custom function inside the userdata namespace
function userdatau.myfunction()
-- do stuff
end
\stopluacode
</texcode>
The full list of canonical namespaces, taken from [http://minimals.contextgarden.net/current/context/alpha/tex/context/base/luat-ini.lua luat-ini.lua]: <code><pre>userdata = userdata or { } -- for users (e.g. functions etc)thirddata = thirddata or { } -- only for third party modulesmoduledata = moduledata or { } -- only for development teamdocumentdata = documentdata or { } -- for users (e.g. raw data)parametersets = parametersets or { } -- experimental for team</pre></code> If your module, environment, or document is going to be used by other people, you should create your own subnamespaces within these tables. <code><pre>moduledata['mymodule'] = { }mm =moduledata.mymodulefunction mm.mainfunction() -- do stuffend</pre></code> = Putting stuff in your TeX document from Lua ==
=== Simple printing: context(), tex.print(), and tex.sprint() ===
Use <code>context(...)</code> for most things. It is equivalent to <code>tex.print(string.format(...))</code>, so
without any space in between.
=== Context commands ===
Most commands that you would type with a backslash in plain ConTeXt, you can access from Lua with <code>context.<em>command</em></code>. Unadorned strings end up in TeX as arguments in curly braces; Lua tables end up in TeX as paramater blocks in square brackets. The following two pieces of code are equivalent:
One final note: arguments can also be specified in the form of nested functions. Because LuaTeX evaluates the deepest-nested argument first, this may cause the <code>context()</code> calls to be evaluated in the wrong order. For more on this, see the article on [[cld|ConTeXt Lua documents]], and also, again, the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf CLD manual].
 
= Passing arguments and buffers: ConTeXt commands that hook into Lua =
== Making \command{arg1}{arg2} hook into Lua ==
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