Changes

Jump to navigation Jump to search
970 bytes added ,  09:06, 22 September 2015
m
Corrected typo in URL to lpeg
context("Unless we print them, then we must \\#\\$\\& print the escape characters, too.")
\stopluacode
</texcode>
 
== Putting Lua code in an external file ==
 
You can put your lua code in an external file (with the <code>.lua</code> extension) and include it with the <code>require</code> command:
 
<texcode>
\startluacode
-- include the file my-lua-lib.lua
require("my-lua-lib")
\endluacode
</texcode>
<texcode>
\def\surroundwd#1%
{\ctxlua{userdata.surroundwithdashes([==[#1]==])}}
</texcode>
''NB'': quoting with <code>[==[#1]==]</code>
([http://www.lua.org/manual/5.2/manual.html#3.1 long strings])
works just like <code>"#1"</code> in most cases, but in addition
it is robust against <code>#1</code> containing the quotation mark
<code>"</code> which would terminate the Lua string prematurely.
Inside <code>\protect .. \unprotect</code> the macros <code>\!!bs</code>
and <code>\!!es</code> are at your disposition.
They are equivalent to <code>[===[</code> and <code>]===]</code> and --
being single tokens to TeX -- parsed faster.
(See [http://repo.or.cz/w/context.git/blob/refs/heads/origin:/tex/context/base/luat-ini.mkiv#l174 <code>luat-ini.mkiv</code>].)
== Making \startenv...\stopenv hook into Lua ==
:<code>\molecule{H_3SO_4^+}</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 <code>\molecule</code> macro can be written as follows.
<texcode>
2

edits

Navigation menu