<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.luatex.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Paul</id>
	<title>LuaTeXWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.luatex.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Paul"/>
	<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php/Special:Contributions/Paul"/>
	<updated>2026-04-14T20:46:24Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Documentation_and_help&amp;diff=172</id>
		<title>Documentation and help</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Documentation_and_help&amp;diff=172"/>
		<updated>2013-12-06T10:03:47Z</updated>

		<summary type="html">&lt;p&gt;Paul: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Documentation=&lt;br /&gt;
&lt;br /&gt;
* The definitive source of information on LuaTeX is the [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf LuaTeX Reference]; the link points to the latest version, documenting latest builds, but can be used for older versions too.&lt;br /&gt;
&lt;br /&gt;
* The previous document reduced to a cheatsheet is also available with the [http://www.luatex.org/svn/trunk/manual/functionref.pdf Short Reference].&lt;br /&gt;
&lt;br /&gt;
* Besides general information, the [http://www.luatex.org/ LuaTeX site] also hosts [http://www.luatex.org/documentation.html some presentations] by members of the team.&lt;br /&gt;
&lt;br /&gt;
* [http://tug.org/TUGboat/ TUGboat] (communications of the TeX User Group) has featured regular articles on LuaTeX for some years now.&lt;br /&gt;
&lt;br /&gt;
* More can be found in publications by local user groups and conferences.&lt;br /&gt;
&lt;br /&gt;
=Help=&lt;br /&gt;
&lt;br /&gt;
* The [http://tug.org/mailman/listinfo/luatex LuaTeX mailing list] is frequented by knowledgeable people and questions rarely remain unanswered.&lt;br /&gt;
&lt;br /&gt;
* There is also a [http://www.ntg.nl/mailman/listinfo/dev-luatex list for developers] for very technical things.&lt;br /&gt;
&lt;br /&gt;
* Questions about LuaTeX can also be asked in other TeX-related lists or sites: [http://tug.org/mailman/listinfo/texhax texhax], [http://groups.google.com/group/comp.text.tex comp.text.tex] (and similar groups for different languages), or [http://tex.stackexchange.com/ TeX StackExchange].&lt;br /&gt;
&lt;br /&gt;
=On Lua=&lt;br /&gt;
&lt;br /&gt;
* The main source on the Lua language is the [http://www.lua.org/manual/5.2/manual.html Lua reference manual]; the link here documents version 5.2, currently used by LuaTeX, but Lua 5.1 is what LuaTeX used up to v0.74; see the [http://www.lua.org/manual/5.1/manual.html Lua 5.1 reference manual] if necessary. See also the [http://www.lua.org/manual/5.2/readme.html#changes changes] and [http://www.lua.org/manual/5.2/manual.html#8 incompatibilities].&lt;br /&gt;
&lt;br /&gt;
* [http://www.lua.org/pil/index.html Programming in Lua], by Lua&amp;#039;s chief architect Roberto Ierusalimschy, is available online for version 5.0, quite usable for later versions.&lt;br /&gt;
&lt;br /&gt;
* There is also a [http://www.lua.org/lua-l.html mailing list] and a [http://forum.luahub.com/ forum].&lt;br /&gt;
&lt;br /&gt;
* All this and more can be found on the [http://www.lua.org/ Lua website].&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Show_error_hook&amp;diff=157</id>
		<title>Show error hook</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Show_error_hook&amp;diff=157"/>
		<updated>2012-02-28T09:24:14Z</updated>

		<summary type="html">&lt;p&gt;Paul: The API is not deprecated in LuaTeX itself, it&amp;#039;s luatexbase that deprecates it; it&amp;#039;s better here to document LuaTeX, not luatexbase, because it&amp;#039;s not used by everybody. See the page on callbacks.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The show_error_hook callback=&lt;br /&gt;
&lt;br /&gt;
This [[Callbacks|callback]] doesn&amp;#039;t replace any internal code and isn&amp;#039;t supposed to do anything meaningful. It is called when an error message occurs, precisely right after the first line of the error message (beginning with an exclamation mark) has been printed, and before the rest of the message is given.&lt;br /&gt;
&lt;br /&gt;
No argument is passed to the function registered in the callback, and return values, if any, are ignored. Its best use is probably to report additional information besides the error message. For instance, the input text &amp;lt;tt&amp;gt;a_1&amp;lt;/tt&amp;gt; will produce the following error message (and another one quite similar at the end of the paragraph):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
! Missing $ inserted.&lt;br /&gt;
&amp;lt;inserted text&amp;gt; &lt;br /&gt;
                $&lt;br /&gt;
&amp;lt;to be read again&amp;gt; &lt;br /&gt;
                   _&lt;br /&gt;
l.1 a_&lt;br /&gt;
      1&lt;br /&gt;
I&amp;#039;ve inserted a begin-math/end-math symbol since I think&lt;br /&gt;
you left one out. Proceed, with fingers crossed.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This message can be annoying for two reasons: first, if one has a document made of several files, one doesn&amp;#039;t know which file is concerned here (one can read the log file upward to spot the latest input file, but that isn&amp;#039;t very convenient); second, the line number takes some time to arrive after the message&amp;#039;s first line. The lines 2 to 4 aren&amp;#039;t very informative, and it could be useful if the line number was available at once.&lt;br /&gt;
&lt;br /&gt;
The following code inserts the input file&amp;#039;s name and line number just after the first line of the message; thus the rest of the message can be ignored (as many users do, the first line, along with where the error happened, being often informative enough):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;show_error_hook&amp;quot;,&lt;br /&gt;
  function ()&lt;br /&gt;
    texio.write_nl(&amp;quot;[FILE: &amp;quot; .. status.filename .. &amp;quot;, LINE: &amp;quot; .. status.linenumber .. &amp;quot;]&amp;quot;)&lt;br /&gt;
  end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some information from the &amp;lt;tt&amp;gt;status&amp;lt;/tt&amp;gt; library is put to use here: &amp;lt;tt&amp;gt;status.filename&amp;lt;/tt&amp;gt; returns a string with the name of the current input file, &amp;lt;tt&amp;gt;status.linenumber&amp;lt;/tt&amp;gt; returns a number (automatically converted to a string here) indicating the line TeX is currently reading.&lt;br /&gt;
&lt;br /&gt;
Now the error message looks like this, assuming that &amp;lt;tt&amp;gt;a_1&amp;lt;/tt&amp;gt; appeared in file called &amp;lt;tt&amp;gt;test_input.tex&amp;lt;/tt&amp;gt; in the same directory as the main file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
! Missing $ inserted.&lt;br /&gt;
[FILE: ./test_input.tex, LINE: 1]&lt;br /&gt;
&amp;lt;inserted text&amp;gt; &lt;br /&gt;
                $&lt;br /&gt;
&amp;lt;to be read again&amp;gt; &lt;br /&gt;
                   _&lt;br /&gt;
l.1 a_&lt;br /&gt;
      1&lt;br /&gt;
I&amp;#039;ve inserted a begin-math/end-math symbol since I think&lt;br /&gt;
you left one out. Proceed, with fingers crossed.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If one wants a full path instead of a relative path, one can use [http://keplerproject.github.com/luafilesystem/ the LuaFileSystem library], shipped with LuaTeX (the use of &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt; implies that this code isn&amp;#039;t simply passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;; see [[Writing Lua in TeX]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;show_error_hook&amp;quot;,&lt;br /&gt;
  function ()&lt;br /&gt;
    local f = status.filename&lt;br /&gt;
    f = string.gsub(f, &amp;quot;^%.&amp;quot;, lfs.currentdir())&lt;br /&gt;
    texio.write_nl(&amp;quot;[FILE: &amp;quot; .. f .. &amp;quot;, LINE: &amp;quot; .. status.linenumber .. &amp;quot;]&amp;quot;)&lt;br /&gt;
  end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This replaces &amp;lt;tt&amp;gt;./&amp;lt;/tt&amp;gt; at the beginning of the filename (if any) with the string returned by &amp;lt;tt&amp;gt;lfs.currentdir()&amp;lt;/tt&amp;gt;, whose meaning should be obvious. (See [http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub the Lua reference manual] for information on &amp;lt;tt&amp;gt;string.gsib()&amp;lt;/tt&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=155</id>
		<title>Callbacks</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=155"/>
		<updated>2012-02-01T07:46:20Z</updated>

		<summary type="html">&lt;p&gt;Paul: Modified remark on the distinction between nil and false.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Callbacks allow the user to interact with TeX&amp;#039;s internal operations; those can be enhanced by additional ones, but also replaced altogether. All callbacks are written in Lua.&lt;br /&gt;
&lt;br /&gt;
Since functions registered in callbacks occur in a sequence of actions that makes up TeX&amp;#039;s processing, many of them will receive arguments (and thus should be defined to handle them) and, most importantly, many are supposed to return values. What arguments are passed, if any, and what should be return, vary from callback to callback.&lt;br /&gt;
&lt;br /&gt;
= Registering a callback =&lt;br /&gt;
&lt;br /&gt;
To install a function in a callback one must use the &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; function, whose syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(callback_name, function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;callback_name&amp;lt;/tt&amp;gt; is a string representing a predefined callback name (see below), and &amp;lt;tt&amp;gt;function&amp;lt;/tt&amp;gt; is either a function variable or an anonymous function. The former is just the name of a function previously defined, for instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local my_function = function (an_arg)&lt;br /&gt;
  do_this_or_that(an_arg)&lt;br /&gt;
end&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, my_function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An anonymous function is a function definition that is not assigned to a variable, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function (an_arg) do_this_or_that(an_arg) end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Even in the case of a function variable, what LuaTeX registers is the function itself, and any further assignment to the variable has no effect on the callback.&lt;br /&gt;
&lt;br /&gt;
If registering is successful, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; returns a number, which is the internal representation of the callback; if registering fails (for instance because a callback doesn&amp;#039;t exist, or what is registered isn&amp;#039;t a function), the function returns &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; and a string representing an error message.&lt;br /&gt;
&lt;br /&gt;
Besides functions, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; can take &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;. The first value clears the callback, and returns to the default behavior (i.e. either nothing or TeX&amp;#039;s default processing); the second value prevents anything from happening at this point, even TeX&amp;#039;s default operations, and should be used with great care (the benefits are minor speed gain).&lt;br /&gt;
&lt;br /&gt;
== Several functions in a callback ==&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; erases any function previously registered. I.e. after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_one)&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_two)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
only &amp;lt;tt&amp;gt;function_two&amp;lt;/tt&amp;gt; is registered in &amp;lt;tt&amp;gt;some_callback&amp;lt;/tt&amp;gt;. Hence, if one wants several successive functions to occur in a callback, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; is clearly insufficient. Instead, one must register a metafunction which calls the functions one after the other. This is taken care of in ConTeXt, and there exists &amp;lt;tt&amp;gt;luatexbase-mcb&amp;lt;/tt&amp;gt; for plain TeX and LaTeX, but here&amp;#039;s how to do it oneself. We&amp;#039;ll take the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback has an example; it is called whenever TeX reads an input line, it receives a string (the line) and should return one. A rough approach would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local function_table = { }&lt;br /&gt;
&lt;br /&gt;
local function metafunction (str)&lt;br /&gt;
  for _, fct in ipairs(function_table) do&lt;br /&gt;
    str = fct(str)&lt;br /&gt;
  end&lt;br /&gt;
  return str&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function add_function (fct)&lt;br /&gt;
  table.insert(function_table, fct)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, metafunction)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then one can call &amp;lt;tt&amp;gt;add_function(my_function)&amp;lt;/tt&amp;gt; to add &amp;lt;tt&amp;gt;my_function&amp;lt;/tt&amp;gt; to the list of functions registered in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;metafunction&amp;lt;/tt&amp;gt; will pass the callback&amp;#039;s argument (an input line) to the first function, set that argument to what the function returns, then repeat the process for all functions and finally return the resulting string.&lt;br /&gt;
&lt;br /&gt;
If one also wants to be able to remove functions (and not the entire metafunction), a subtler approach is required, where names are associated with functions and can be used as handles.&lt;br /&gt;
&lt;br /&gt;
= Information about callbacks =&lt;br /&gt;
&lt;br /&gt;
LuaTeX has two functions to get some information about callbacks. The first is &amp;lt;tt&amp;gt;callback.list()&amp;lt;/tt&amp;gt;, called without arguments and returning a table with the callback names as keys and booleans as values: if the value is &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;, it means that something is registered in the callback, or that it is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; (see above). The other function is &amp;lt;tt&amp;gt;callback.find()&amp;lt;/tt&amp;gt;, called with a callback name. If a function is registered, it is returned; if the callback is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, a boolean with that value is returned; finally, if nothing is registered, &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned (note that if &amp;lt;tt&amp;gt;ret&amp;lt;/tt&amp;gt; is the returned value, then &amp;lt;tt&amp;gt;if ret&amp;lt;/tt&amp;gt; is false if &amp;lt;tt&amp;gt;ret&amp;lt;/tt&amp;gt; is either &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;; you must use &amp;lt;tt&amp;gt;if ret == nil &amp;lt;/tt&amp;gt; to distinguish between the two).&lt;br /&gt;
&lt;br /&gt;
= List of callbacks =&lt;br /&gt;
&lt;br /&gt;
For suggestions on how to document callbacks, see [[Talk:Callbacks|the discussion page]].&lt;br /&gt;
&lt;br /&gt;
== File discovery callbacks ==&lt;br /&gt;
&lt;br /&gt;
These are called whenever an input file is needed. Each receives the &amp;lt;tt&amp;gt;asked_name&amp;lt;/tt&amp;gt;, a string representing what file name was called by the user (for instance, &amp;lt;tt&amp;gt;my_file.tex&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;\input my_file.tex&amp;lt;/tt&amp;gt;) and should return the name of a file to be read (or written to). The &amp;lt;tt&amp;gt;id_number&amp;lt;/tt&amp;gt; in the first two callbacks is the stream&amp;#039;s number plus one, because 0 denotes &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; files.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[find_read_file]] ||id_number, asked_name ||actual_name ||File called with &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_write_file]] ||id_number, asked_name ||actual_name ||&amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file or file called with &amp;lt;tt&amp;gt;\write&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_font_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_output_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_format_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_vf_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_map_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_enc_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_sfd_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_pk_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_data_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_opentype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_truetype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_type_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_image_file]] ||asked_name ||actual_name&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File reading callbacks ==&lt;br /&gt;
&lt;br /&gt;
The previous callbacks were called when a file was asked for; the following are triggered when the file is read. In all, &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt; is the string returned by the file discovery callbacks. The first callback reads &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; files; it must return a table, whose first cell is a function that will be called whenever a line from the input file is readed; the second cell is an optional function executed when the input file terminates.&lt;br /&gt;
&lt;br /&gt;
The other callbacks are used for binary files: &amp;lt;tt&amp;gt;success&amp;lt;/tt&amp;gt; is a boolean, &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; is a string and &amp;lt;tt&amp;gt;data_size&amp;lt;/tt&amp;gt; is a number (the length of &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; in bytes).&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[open_read_file]] ||filename ||{reader, close}&lt;br /&gt;
|-&lt;br /&gt;
|[[read_font_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_vf_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_map_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_enc_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_sfd_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_pk_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_data_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_truetype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_type1_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_opentype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Data processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[process_input_buffer]] ||line ||modified_line ||Called for each input line (after &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;open_read_file&amp;lt;/tt&amp;gt; if TeX is not reading the main file).&lt;br /&gt;
|-&lt;br /&gt;
|[[process_output_buffer]] ||line ||modified_line ||Called for each line TeX writes to an external file; this excludes the &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file, the terminal and &amp;lt;tt&amp;gt;\write18&amp;lt;/tt&amp;gt; calls.&lt;br /&gt;
|-&lt;br /&gt;
|[[process_jobname]] ||string ||modified_string ||Processes the jobname when queried with &amp;lt;tt&amp;gt;\jobname&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;tex.jobname&amp;lt;/tt&amp;gt;. The internal job name (used for the output and log files) remains untouched. (This callback appeared in v.0.71.)&lt;br /&gt;
|-&lt;br /&gt;
|[[token_filter]] || ||token ||Called when TeX needs token; no argument is passed to the callback, the next token must be fetched with &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt;; what is returned is immediately processed by TeX.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Node list processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks deal with node manipulations and occur at various stages of TeX&amp;#039;s typesetting processes.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[buildpage_filter]] ||information || ||Called when TeX moves material to the main vertical list. (This callback is an endangered species.)&lt;br /&gt;
|-&lt;br /&gt;
|[[hyphenate]] ||head, tail || || Inserts discretionary nodes in the node list; called in restricted and unrestricted horizontal mode (i.e. &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;&amp;#039;es and paragraph building). A function doing what TeX does here by default is &amp;lt;tt&amp;gt;lang.hyphenate()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[ligaturing]] ||head, tail || || Same as &amp;lt;tt&amp;gt;hyphenate&amp;lt;/tt&amp;gt;, but applying ligatures (and adding some discretionaries too). An associated function is &amp;lt;tt&amp;gt;node.ligaturing()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[kerning]] ||head, tail || || Same as above, with font kerns. The associated function is &amp;lt;tt&amp;gt;node.kerning()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_linebreak_filter]] ||head, context ||boolean or newhead ||Called in unrestricted horizontal mode, after &amp;lt;tt&amp;gt;kerning&amp;lt;/tt&amp;gt;, and before calling the paragraph builder; it receives the list of nodes that is the contents of the paragraph (plus the &amp;lt;tt&amp;gt;\parfillskip&amp;lt;/tt&amp;gt; glue) and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[linebreak_filter]] ||head, is_display ||newhead ||The paragraph builder. It receives what the previous callback has returned and is in charge of making a paragraph out of it; it must return what it has built. An associated function is &amp;lt;tt&amp;gt;tex.linebreak()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[post_linebreak_filter]] ||head, groupcode, ||boolean or newhead ||Receives what has been returned by &amp;lt;tt&amp;gt;linebreak_filter&amp;lt;/tt&amp;gt; and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[hpack_filter]] ||head, context, size, packtype[, dir] ||boolean or newhead ||Called when TeX is going to build an &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[vpack_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Called when TeX is going to build a &amp;lt;tt&amp;gt;\vbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_output_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Same as &amp;lt;tt&amp;gt;vpack_filter&amp;lt;/tt&amp;gt;, but when TeX packs box 255 (or any other value of &amp;lt;tt&amp;gt;\outputbox&amp;lt;/tt&amp;gt;) and goes to the output routine.&lt;br /&gt;
|-&lt;br /&gt;
|[[mlist_to_hlist]] ||head, display_type, need_penalties ||newhead ||Called when TeX turns a math list into a horizontal list (as described in Appendix G of the &amp;lt;em&amp;gt;TeXbook&amp;lt;/em&amp;gt;).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Information reporting callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks are called in various places when TeX has something to say. None of them takes an argument nor returns anything.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_dump]] ||Called before dumping a format.&lt;br /&gt;
|-&lt;br /&gt;
|[[start_run]] ||LuaTeX&amp;#039;s banner (`This is LuaTeX...&amp;#039;); must be changed in the initialization script, otherwise the banner has already been printed.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_run]] ||TeX&amp;#039;s final words (statistics and `Output written to...&amp;#039;).&lt;br /&gt;
|-&lt;br /&gt;
|[[start_page_number]] ||The opening bracket and the page number printed when a page is shipped out.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_page_number]] ||The closing bracket printed after the previous callback.&lt;br /&gt;
|-&lt;br /&gt;
|[[show_error_hook]] || Called at the beginning of each error message.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
||[[finish_pdffile]] || || ||Called when all pages have been written to the PDF file.&lt;br /&gt;
|-&lt;br /&gt;
|[[define_font]] ||name, size, id ||font_table ||Called when the &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; command is executed; &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; is the filename asked for, &amp;lt;tt&amp;gt;size&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;at&amp;lt;/tt&amp;gt; size, &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; is the number that will be assigned as an internal representation to the font thus loaded. The callback should return a table representing a font.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=154</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=154"/>
		<updated>2011-12-29T06:04:55Z</updated>

		<summary type="html">&lt;p&gt;Paul: Reverted edits by Lethuphuong89 (talk) to last revision by 217.235.241.143&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Documentation and help]] points to other online resources (manuals, mailing list, etc.) related to LuaTeX.&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Attributes]] introduces LuaTeX&amp;#039;s thrilling new concept.&lt;br /&gt;
* Pages on callbacks:&lt;br /&gt;
** [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
** There is a page on the [[Post linebreak filter|&amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;]] callback, explaining and illustrating it with a couple of examples. The [[Show the hyphenation points]] article is another example of use.&lt;br /&gt;
** The page on [[process input buffer|the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback]] illustrates how to read documents with non-UTF-8 encoding, and how to write TeX with lightweight markup instead of the usual commands.&lt;br /&gt;
** Another callback is [[show error hook|&amp;lt;tt&amp;gt;show_error_hook&amp;lt;/tt&amp;gt;]], which lets you enliven your error messages!&lt;br /&gt;
** You can fake XeTeX&amp;#039;s interchar tokens with the [[token filter|&amp;lt;tt&amp;gt;token_filter&amp;lt;/tt&amp;gt;]].&lt;br /&gt;
* [[TeX without TeX]] is about using TeX&amp;#039;s functionality (typesetting, pdf writing) only using Lua code (no &amp;lt;tt&amp;gt;\TeX&amp;lt;/tt&amp;gt; macros).&lt;br /&gt;
* [[fontsampler|Create a fontsampler]] using plain LuaTeX and &amp;lt;tt&amp;gt;luaotfload&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Use_a_TrueType_font&amp;diff=150</id>
		<title>Use a TrueType font</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Use_a_TrueType_font&amp;diff=150"/>
		<updated>2011-10-02T08:39:19Z</updated>

		<summary type="html">&lt;p&gt;Paul: Corrected truetype/opentype.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A TrueType (or Openype) font can be used without going through a TFM or an OFM file, like in XeTeX.&lt;br /&gt;
&lt;br /&gt;
The best way to do so is to use the package luaotfload, on the CTAN, and developped here: http://github.com/mpg/luaotfload/&lt;br /&gt;
&lt;br /&gt;
The code shows how we can use the table of a TrueType font to produce an internal TeX font table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\pdfoutput1&lt;br /&gt;
\directlua{&lt;br /&gt;
callback.register(&amp;quot;define_font&amp;quot;,&lt;br /&gt;
  function(name, size)&lt;br /&gt;
    local fonttype, f&lt;br /&gt;
    local fonttype = string.match(string.lower(name), &amp;quot;otf$&amp;quot;) and &amp;quot;opentype&amp;quot;&lt;br /&gt;
                  or string.match(string.lower(name), &amp;quot;ttf$&amp;quot;) and &amp;quot;truetype&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    if fonttype then&lt;br /&gt;
      filename = kpse.find_file(name, &amp;quot;opentype fonts&amp;quot;) or kpse.find_file(name, &amp;quot;truetype fonts&amp;quot;)&lt;br /&gt;
      if size &amp;lt; 0 then&lt;br /&gt;
        size = (- 655.36) * size&lt;br /&gt;
      end&lt;br /&gt;
      ttffont = fontloader.to_table(fontloader.open(filename))&lt;br /&gt;
      if ttffont then&lt;br /&gt;
        f = { }&lt;br /&gt;
        f.name = ttffont.fontname&lt;br /&gt;
        f.fullname = ttffont.names[1].names.fullname&lt;br /&gt;
        f.parameters = { }&lt;br /&gt;
        f.designsize = size&lt;br /&gt;
        f.size = size&lt;br /&gt;
        f.direction = 0&lt;br /&gt;
        f.parameters.slant = 0&lt;br /&gt;
        f.parameters.space = size * 0.25&lt;br /&gt;
        f.parameters.space_stretch = 0.3 * size&lt;br /&gt;
        f.parameters.space_shrink = 0.1 * size&lt;br /&gt;
        f.parameters.x_height = 0.4 * size&lt;br /&gt;
        f.parameters.quad = 1.0 * size&lt;br /&gt;
        f.parameters.extra_space = 0&lt;br /&gt;
        f.characters = { }&lt;br /&gt;
        local mag = size / ttffont.units_per_em&lt;br /&gt;
&lt;br /&gt;
        local names_of_char = { }&lt;br /&gt;
        for char, glyph in pairs(ttffont.map.map) do&lt;br /&gt;
          names_of_char[ttffont.glyphs[glyph].name] = ttffont.map.backmap[glyph]&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        for char, glyph in pairs(ttffont.map.map) do&lt;br /&gt;
          local glyph_table = ttffont.glyphs[glyph]&lt;br /&gt;
&lt;br /&gt;
          f.characters[char] = {&lt;br /&gt;
            index = glyph,&lt;br /&gt;
            width = glyph_table.width * mag,&lt;br /&gt;
            name = glyph_table.name }&lt;br /&gt;
          if glyph_table.boundingbox[4] then&lt;br /&gt;
            f.characters[char].height = glyph_table.boundingbox[4] * mag&lt;br /&gt;
          end&lt;br /&gt;
          if glyph_table.boundingbox[2] then&lt;br /&gt;
            f.characters[char].depth = -glyph_table.boundingbox[2] * mag&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          if glyph_table.kerns then&lt;br /&gt;
            local kerns = { }&lt;br /&gt;
            for _, kern in pairs(glyph_table.kerns) do&lt;br /&gt;
              kerns[names_of_char[kern.char]] = kern.off * mag&lt;br /&gt;
            end&lt;br /&gt;
            f.characters[char].kerns = kerns&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        &lt;br /&gt;
        f.filename = filename&lt;br /&gt;
        f.type = &amp;quot;real&amp;quot;&lt;br /&gt;
        f.format = fonttype&lt;br /&gt;
        f.embedding = &amp;quot;subset&amp;quot;&lt;br /&gt;
        f.cidinfo = {&lt;br /&gt;
          registry = &amp;quot;Adobe&amp;quot;,&lt;br /&gt;
          ordering = &amp;quot;Identity&amp;quot;,&lt;br /&gt;
          supplement = 0,&lt;br /&gt;
          version = 1 }&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      f = font.read_tfm(name, size)&lt;br /&gt;
    end&lt;br /&gt;
  return f&lt;br /&gt;
  end)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The index of the &amp;lt;tt&amp;gt;f.characters&amp;lt;/tt&amp;gt; table is the Unicode value, among the properties of each entry of that table we have &amp;lt;tt&amp;gt;index&amp;lt;/tt&amp;gt; which is the glyph index. Using this property we get the mapping between Unicode characters and glyphs. Kerning is handled via TeX font LKP.&lt;br /&gt;
&lt;br /&gt;
What is missing in the code above is better calculation of standard TeX font parameters and OpenType features.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=149</id>
		<title>Writing Lua in TeX</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=149"/>
		<updated>2011-08-30T06:40:52Z</updated>

		<summary type="html">&lt;p&gt;Paul: /* debug note */ Correction and clean-up.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Embedding Lua code in a TeX document =&lt;br /&gt;
&lt;br /&gt;
Although it is simpler to put Lua code in Lua files, from time to time one may want or need to go Lua in the middle of a document. To this end, LuaTeX has two commands: &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;. They work the same, except &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt; is processed when the page where it appears is shipped out, whereas &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is processed at once; the distinction is immaterial here, and what is said of &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; also applies to &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; can be called in three ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua name {&amp;lt;name&amp;gt;} {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua &amp;lt;number&amp;gt; {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Those three ways are equivalent when it comes to process &amp;lt;tt&amp;gt;&amp;lt;lua code&amp;gt;&amp;lt;/tt&amp;gt;, but in the second case processing will occur in a chunk named &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, and in the third it will occur in a chunk whose name is the entry &amp;lt;tt&amp;gt;&amp;lt;number&amp;gt;&amp;lt;/tt&amp;gt; in the table &amp;lt;tt&amp;gt;lua.name&amp;lt;/tt&amp;gt;. The difference manifests itself only when errors occur, in which case the name of the chunk, if any, is reported.&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, named or not, is processed in a separate chunk. That means that any &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variable is defined for this call only and is lost afterward. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  one = 1&lt;br /&gt;
  local two = 2&lt;br /&gt;
}&lt;br /&gt;
\directlua{&lt;br /&gt;
  texio.write_nl(type(one))&lt;br /&gt;
  texio.write_nl(type(two))&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will report &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; (&amp;lt;tt&amp;gt;texio.write_nl&amp;lt;/tt&amp;gt; writes to the log file). On the other hand, Lua code is completely insensitive to TeX&amp;#039;s grouping mechanism. In other words, calling &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; between &amp;lt;tt&amp;gt;\bgroup&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\egroup&amp;lt;/tt&amp;gt; doesn&amp;#039;t affect the code to be processed.&lt;br /&gt;
&lt;br /&gt;
= TeX catcodes in Lua =&lt;br /&gt;
&lt;br /&gt;
By default, the code passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is treated as normal TeX input and only then sent to the Lua interpreter. This may lead to unwanted results and must be acknowledged.&lt;br /&gt;
&lt;br /&gt;
== Expansion ==&lt;br /&gt;
&lt;br /&gt;
As with any other special, the code in &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; (and the &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, if specifed) is fully expanded. This means that macros can be safely passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; if one wants their values, but that they should also be properly escaped when needed. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; as the number &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;. To store the control sequence &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; instead, another course of action is needed: see the [[#Backslash|section on backslash]] below.&lt;br /&gt;
&lt;br /&gt;
It should be noted that &amp;lt;tt&amp;gt;\par&amp;lt;/tt&amp;gt; tokens are removed from &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; if they are unexpandable (i.e., most likely, if they have their original meaning). Hence, empty lines can be used in Lua code.&lt;br /&gt;
&lt;br /&gt;
== Line ends ==&lt;br /&gt;
&lt;br /&gt;
When TeX reads a file, it normally turns line ends into spaces. That means that what looks like several lines is actually fed to the Lua interpreter as one big line. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
amounts to the following, if it were written in a separate Lua file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That is perfectly legitimate, but strange things might happen. First, TeX macros gobble spaces as usual. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be fed to the interpreter as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1anothervar = 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is not legitimate at all. Second, the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt; will affect everything to the end of the &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call. That is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
--  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 -- anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which works but only defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt;. Third, when reporting error, the Lua interpreter will always mention the line number as 1, since it processes one big line only; that isn&amp;#039;t extremely useful when the code is large.&lt;br /&gt;
&lt;br /&gt;
The solution is to set &amp;lt;tt&amp;gt;\endlinechar=10&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\catcode`\^^M=12&amp;lt;/tt&amp;gt;. In both cases, line ends will be preserved and the code will be processed as it is input.&lt;br /&gt;
&lt;br /&gt;
== Special characters ==&lt;br /&gt;
&lt;br /&gt;
In TeX, some characters have a special behavior. That must be taken into account when writing Lua code: one must change their catcodes beforehand if one wants to handle them as Lua would, as has just been done for line ends. That means that &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, as such, is clearly insufficient to write any extended chunk of code. It is thus better to devise a special macro that sets the catcodes to the appropriate values, reads the Lua code, feeds it to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, and restores the catcodes. The following code does the job:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\luacode{%&lt;br /&gt;
  \bgroup&lt;br /&gt;
  \catcode`\{=12&lt;br /&gt;
  \catcode`\}=12&lt;br /&gt;
  \catcode`\^^M=12&lt;br /&gt;
  \catcode`\#=12&lt;br /&gt;
  \catcode`\~=12&lt;br /&gt;
  \catcode`\%=12&lt;br /&gt;
  \doluacode&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
\bgroup&lt;br /&gt;
\catcode`\^^M=12 %&lt;br /&gt;
\long\gdef\doluacode#1^^M#2\endluacode{\directlua{#2}\egroup}%&lt;br /&gt;
\egroup&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that not all special characters are set to normal (catcode 12) characters; that is explained for each below. Note also that &amp;lt;tt&amp;gt;\doluacode&amp;lt;/tt&amp;gt;, internally called by &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt;, is defined to get rid of anything up to the line end, and then pass anything up to &amp;lt;tt&amp;gt;\endluacode&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;. Discarding what follows &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is important, otherwise a simple code as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = 1&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
would actually create two lines, the first being empty; it is annoying because errors are then reported with the wrong line number (i.e. any error in this one-line code would be reported to happen on line 2).&lt;br /&gt;
&lt;br /&gt;
However, the rest of the line after &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; could also be processed, instead of discarded, to manage special effects (e.g. specifying a chunk&amp;#039;s name, storing the code in a control sequence, or even setting which catcodes should be changed or not).&lt;br /&gt;
&lt;br /&gt;
=== Backslash ===&lt;br /&gt;
&lt;br /&gt;
The backslash in TeX is used to form control sequences. In the definition of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; above, it isn&amp;#039;t changed and thus behaves as usual. It allows commands to be passed and expanded to the Lua code. Anyway a backslash in Lua is also an escape character in strings. Hence, if one wants to store the name of a macro in Lua code, the following won&amp;#039;t work:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because to the Lua interpreter the string is made of &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; followed by &amp;lt;tt&amp;gt;acro&amp;lt;/tt&amp;gt;; since &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; is not defined in Lua, the string is read as &amp;lt;tt&amp;gt;macro&amp;lt;/tt&amp;gt;, but in other circumstances strange things might happen: for instance, &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; is a newline. The proper way to pass a macro verbatim is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which Lua will correctly read as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\\macro&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with the backslash escaped to represent itself. Another solution is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = [[\noexpand\macro]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because the double brackets signals a string in Lua where no escape sequence occurs (and the string can also run on several lines). Note however that in the second case &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; will be defined with a trailing space, i.e. as &amp;lt;tt&amp;gt;&amp;quot;\macro &amp;quot;&amp;lt;/tt&amp;gt;, because of TeX&amp;#039;s habit to append a trailing space to unexpanded (or unexpandable) control sequences.&lt;br /&gt;
&lt;br /&gt;
If one wants the backslash to be treated as any other character (not creating control sequences), then one has to rewrite &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\luacode{%&lt;br /&gt;
  \bgroup&lt;br /&gt;
  \catcode`\\=12&lt;br /&gt;
  \catcode`\{=12&lt;br /&gt;
  \catcode`\}=12&lt;br /&gt;
  \catcode`\^^M=12&lt;br /&gt;
  \catcode`\#=12&lt;br /&gt;
  \catcode`\~=12&lt;br /&gt;
  \catcode`\%=12&lt;br /&gt;
  \doluacode&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
\bgroup&lt;br /&gt;
\catcode`\|=0&lt;br /&gt;
\catcode`\^^M=12 %&lt;br /&gt;
\catcode`\\=12 %&lt;br /&gt;
|long|gdef|doluacode#1^^M#2\endluacode{|directlua{#2}|egroup}%&lt;br /&gt;
|egroup&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Backslashes remain escape characters in Lua, though.&lt;br /&gt;
&lt;br /&gt;
=== Braces ===&lt;br /&gt;
&lt;br /&gt;
One may want to define a string in Lua which contains unbalanced braces, i.e.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the braces&amp;#039; catcodes hadn&amp;#039;t been changed beforehand, that would be impossible. Note, however, that this means that one can&amp;#039;t feed arguments to commands in the usual way. I.e. the following will produce nothing good:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\dosomething{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\dosomething&amp;lt;/tt&amp;gt; will be expanded with the left brace (devoid of its usual delimiter-ness) as its argument, and the rest of the line might produce chaos. Thus, one may also choose not to change the catcodes of braces, depending on how &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is most likely to be used. Note that strings with unbalanced braces can still be defined, even if braces have their usual catcodes, thanks to the following trick:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot; -- }&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the code is passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, braces are balanced because the Lua comment means nothing to TeX; when passed to the Lua interpreter, on the other hand, the right brace is ignored.&lt;br /&gt;
&lt;br /&gt;
=== Hash and comment ===&lt;br /&gt;
&lt;br /&gt;
The hash sign &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; in Lua is the length operator: prefixed to a string or table variable, it returns its length. If its catcode weren&amp;#039;t taken care of, LuaTeX would pass to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; a double hash for each hash, i.e. each &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; would be turned into &amp;lt;tt&amp;gt;##&amp;lt;/tt&amp;gt;. That is normal TeX behavior, but unwanted here.&lt;br /&gt;
&lt;br /&gt;
As for the commen sign &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, it is useful in Lua when manipulating strings. If it weren&amp;#039;t escaped it would discard parts of the code when TeX reads it, and a mutilated version of the input would be passed to the Lua interpreter. In turn, discarding a line by commenting it in &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; should be done with the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Active characters ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt; character is generally active and used as a no-break space in TeX. It it were passed as is to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, it would expand to uninterpretable control sequences, whereas in Lua it is used to form the unequal operator &amp;lt;tt&amp;gt;~=&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Other possible active characters should be taken care of, but which characters are active is unpredictable; punctuation marks might be so to accommodate special spacing, as with LaTeX&amp;#039;s &amp;#039;&amp;#039;babel&amp;#039;&amp;#039; package, but such tricks are unlikely to survive in LuaTeX (cleaner methods exist that add a space before punctuation marks when necessary).&lt;br /&gt;
&lt;br /&gt;
=== Other characters ===&lt;br /&gt;
&lt;br /&gt;
When processing verbatim text in TeX, one generally also changes the catcodes of &amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;&amp;amp;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;^&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; and the space character, because they too are special. When passed to the Lua interpreter, though, their usual catcodes won&amp;#039;t do any harm, that is why they are left unmodified here.&lt;br /&gt;
&lt;br /&gt;
== \luaescapestring ==&lt;br /&gt;
&lt;br /&gt;
Although it can&amp;#039;t do all of what&amp;#039;s been explained, the &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; command might be useful in some cases: it expands its argument (which must be enclosed in &amp;#039;&amp;#039;real&amp;#039;&amp;#039; braces) fully, then modify it so that dangerous characters are escaped: backslashes, hashes, quotes and line ends. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{&amp;quot;\noexpand\foo&amp;quot;}&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\luaescapestring{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be passed to Lua as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\&amp;quot;\\foo \&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so that &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; is defined as &amp;lt;tt&amp;gt;&amp;quot;\foo &amp;quot;&amp;lt;/tt&amp;gt;, with the quotes as parts of it. Note that the trailing space after &amp;lt;tt&amp;gt;\foo&amp;lt;/tt&amp;gt; still happens.&lt;br /&gt;
&lt;br /&gt;
= From Lua to TeX =&lt;br /&gt;
&lt;br /&gt;
Inside Lua code, one can pass strings to be processed by TeX with the functions &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;tex.sprint()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;tex.tprint()&amp;lt;/tt&amp;gt;. All such calls are processed at the end of a &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call, even though they might happen in the middle of the code. This behavior is worth noting because it might be surprising in some cases, although it is generally harmless.&lt;br /&gt;
&lt;br /&gt;
== tex.print() ==&lt;br /&gt;
&lt;br /&gt;
This function receives as its argument(s) either one or more strings or an array of strings. Each string is processed as an input line: an end-of-line character is appended (except to the last string), and TeX is in state &amp;lt;tt&amp;gt;newline&amp;lt;/tt&amp;gt; when processing it (i.e. leading spaces are skipped). Hence the two equivalent calls:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.print(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
tex.print({&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
are both interpreted by TeX as would the following two lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a&lt;br /&gt;
b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus `a b&amp;#039; is produced, since line ends normally produce a space.&lt;br /&gt;
&lt;br /&gt;
The function can also take an optional number as its first argument; it is interpreted as referring to a catcode table (as defined by &amp;lt;tt&amp;gt;\initcatcodetable&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\savecatcodetable&amp;lt;/tt&amp;gt;), and each line is processed by TeX with that catcode regime. For instance (note that with such a minimal catcode table, braces don&amp;#039;t even have their usual values):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\bgroup&lt;br /&gt;
\initcatcodetable1&lt;br /&gt;
\catcode`\_=0&lt;br /&gt;
\savecatcodetable1&lt;br /&gt;
\egroup&lt;br /&gt;
&lt;br /&gt;
\directlua{tex.print(1, &amp;quot;_TeX&amp;quot;)}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The string will be read with &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; as an escape character, and thus interpreted as the command commonly known as &amp;lt;tt&amp;gt;\TeX&amp;lt;/tt&amp;gt;. The catcode regime holds only for the strings passed to &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt; and the rest of the document isn&amp;#039;t affected.&lt;br /&gt;
&lt;br /&gt;
If the optional number is &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt;, or points to an invalid (i.e. undefined) catcode table, then the strings are processed with the current catcodes, as if there was no optional argument. If it is &amp;lt;tt&amp;gt;-2&amp;lt;/tt&amp;gt;, then the strings are read as if the result of &amp;lt;tt&amp;gt;\detokenize&amp;lt;/tt&amp;gt;: all characters have catcode 12 (i.e. `other&amp;#039;, characters that have no function beside representing themselves), except space, which has catcode 10 (as usual).&lt;br /&gt;
&lt;br /&gt;
== tex.sprint() ==&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, this function can receive either one or more strings or an array of strings, with an optional number as its first argument pointing to a catcode table. Unlike &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, however, each string is processed as if TeX were in the middle of a line and not at the beginning of a new one: spaces aren&amp;#039;t skipped, no end-of-line character is added and trailing spaces aren&amp;#039;t ignored. Thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.sprint(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is interpreted by TeX as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ab&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
without any space inbetween.&lt;br /&gt;
&lt;br /&gt;
== tex.tprint() ==&lt;br /&gt;
&lt;br /&gt;
This function takes an unlimited number of tables as its arguments; each table must be an array of strings, with the first entry optionally being a number pointing to a catcode table. Then each table is processed as if passed to &amp;lt;tt&amp;gt;tex.sprint()&amp;lt;/tt&amp;gt;. Thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.tprint({1, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;}, {&amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is equivalent to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.sprint(1, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
tex.sprint(&amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== debug note ===&lt;br /&gt;
&lt;br /&gt;
Note that there are some tables that Lua inside LuaTeX has; one reference is in [http://www.luatex.org/svn/trunk/manual/functionref.pdf functionref]: &amp;#039;&amp;#039;`&amp;lt;tt&amp;gt;luatex&amp;lt;/tt&amp;gt; is a typesetter; &amp;lt;tt&amp;gt;texlua&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;luatex --luaonly&amp;lt;/tt&amp;gt; are lua interpreters. In lua interpreter mode, the lua tables &amp;lt;tt&amp;gt;tex&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;token&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;node&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pdf&amp;lt;/tt&amp;gt; are unavailable.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
For utilizing lua-specific debug techniques, see &lt;br /&gt;
* [http://www.lua.org/pil/23.html 23. The Debug Library - Lua], &lt;br /&gt;
* [http://www.lua.org/pil/23.1.html 23.1 - Introspective Facilities], &lt;br /&gt;
* [http://lua-users.org/wiki/DebugLibraryTutorial lua -users wiki: Debug Library Tutorial]&lt;br /&gt;
&lt;br /&gt;
You can use commands like these in your .tex file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
for k,v in pairs(tex) do print(k, v) end&lt;br /&gt;
print&amp;quot;===&amp;quot;&lt;br /&gt;
for k,v in pairs(lua) do print(k,v) end&lt;br /&gt;
print&amp;quot;===&amp;quot;&lt;br /&gt;
print(debug.traceback(1))&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
... and the output of lua&amp;#039;s &amp;lt;tt&amp;gt;print&amp;lt;/tt&amp;gt; will be sent to stdout; the three &amp;lt;tt&amp;gt;=&amp;lt;/tt&amp;gt; signs will serve simply as `delimiters&amp;#039; in the output, which will look something like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;height:250px;overflow:scroll;&amp;quot;&amp;gt;&lt;br /&gt;
tprint	function: 0x89086e8&lt;br /&gt;
box	table: 0x895a0a0&lt;br /&gt;
getsfcode	function: 0x895ae10&lt;br /&gt;
pdffontsize	function: 0x895b048&lt;br /&gt;
shipout	function: 0x895b2d8&lt;br /&gt;
getmath	function: 0x895b380&lt;br /&gt;
setuccode	function: 0x895ae48&lt;br /&gt;
getskip	function: 0x8908860&lt;br /&gt;
pdfxformname	function: 0x895b170&lt;br /&gt;
setlist	function: 0x895ab38&lt;br /&gt;
setattribute	function: 0x8908898&lt;br /&gt;
count	table: 0x895b600&lt;br /&gt;
getuccode	function: 0x895ae80&lt;br /&gt;
getbox	function: 0x895ab00&lt;br /&gt;
uniformdeviate	function: 0x895b080&lt;br /&gt;
fontname	function: 0x895af58&lt;br /&gt;
primitives	function: 0x895b220&lt;br /&gt;
badness	function: 0x895b310&lt;br /&gt;
setmath	function: 0x895b348&lt;br /&gt;
setskip	function: 0x8908828&lt;br /&gt;
getmathcode	function: 0x895ada0&lt;br /&gt;
getcount	function: 0x8908950&lt;br /&gt;
toks	table: 0x895b6b0&lt;br /&gt;
setsfcode	function: 0x895add8&lt;br /&gt;
run	function: 0x8908638&lt;br /&gt;
sfcode	table: 0x895a130&lt;br /&gt;
pdfpageref	function: 0x895b138&lt;br /&gt;
setcount	function: 0x8908918&lt;br /&gt;
setdimen	function: 0x89087b8&lt;br /&gt;
print	function: 0x89086d0&lt;br /&gt;
setlccode	function: 0x895acf8&lt;br /&gt;
fontidentifier	function: 0x895af90&lt;br /&gt;
getlccode	function: 0x895ad30&lt;br /&gt;
setmathcode	function: 0x895ad68&lt;br /&gt;
round	function: 0x895aeb8&lt;br /&gt;
getdimen	function: 0x89087f0&lt;br /&gt;
write	function: 0x89086b8&lt;br /&gt;
set	function: 0x8908770&lt;br /&gt;
definefont	function: 0x895b1b0&lt;br /&gt;
dimen	table: 0x895b550&lt;br /&gt;
get	function: 0x89087a0&lt;br /&gt;
number	function: 0x895b0c0&lt;br /&gt;
nest	table: 0x895a600&lt;br /&gt;
lists	table: 0x895a550&lt;br /&gt;
setcatcode	function: 0x895ac18&lt;br /&gt;
delcode	table: 0x895a4a0&lt;br /&gt;
setnest	function: 0x895aba8&lt;br /&gt;
mathcode	table: 0x895a3f0&lt;br /&gt;
getdelcode	function: 0x895acc0&lt;br /&gt;
catcode	table: 0x895a340&lt;br /&gt;
pdffontname	function: 0x895afd0&lt;br /&gt;
uccode	table: 0x895a290&lt;br /&gt;
setdelcode	function: 0x895ac88&lt;br /&gt;
lccode	table: 0x895a1e0&lt;br /&gt;
sp	function: 0x895af28&lt;br /&gt;
attribute	table: 0x895b410&lt;br /&gt;
getcatcode	function: 0x895ac50&lt;br /&gt;
extraprimitives	function: 0x895b258&lt;br /&gt;
linebreak	function: 0x895b3b8&lt;br /&gt;
setbox	function: 0x895aac8&lt;br /&gt;
skip	table: 0x895b4c0&lt;br /&gt;
pdffontobjnum	function: 0x895b008&lt;br /&gt;
settoks	function: 0x8908988&lt;br /&gt;
gettoks	function: 0x895aa90&lt;br /&gt;
enableprimitives	function: 0x895b298&lt;br /&gt;
getlist	function: 0x895ab70&lt;br /&gt;
getnest	function: 0x895abe0&lt;br /&gt;
getattribute	function: 0x89088d8&lt;br /&gt;
romannumeral	function: 0x895b0f8&lt;br /&gt;
scale	function: 0x895aef0&lt;br /&gt;
hashtokens	function: 0x895b1e8&lt;br /&gt;
error	function: 0x8908720&lt;br /&gt;
finish	function: 0x8908668&lt;br /&gt;
&lt;br /&gt;
====&lt;br /&gt;
setluaname	function: 0x895efb8&lt;br /&gt;
setbytecode	function: 0x895f028&lt;br /&gt;
name	table: 0x895ef50&lt;br /&gt;
getluaname	function: 0x895ef80&lt;br /&gt;
getbytecode	function: 0x895eff0&lt;br /&gt;
version	Lua 5.1&lt;br /&gt;
bytecode	table: 0x895f080&lt;br /&gt;
====&lt;br /&gt;
1&lt;br /&gt;
stack traceback:&lt;br /&gt;
	&amp;lt;\directlua &amp;gt;:1: in main chunk&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the string returned by &amp;lt;tt&amp;gt;debug.traceback&amp;lt;/tt&amp;gt; contains &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; and thus should be handled with care if passed to TeX, which might try to execute it.&lt;br /&gt;
&lt;br /&gt;
= The expansion of \directlua =&lt;br /&gt;
&lt;br /&gt;
A call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is fully expandable; i.e. it can be used in contexts where full expansion is required, as in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\csname\directlua{tex.print(&amp;quot;TeX&amp;quot;)}\endcsname&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is a somewhat convoluted way of saying &amp;lt;tt&amp;gt;\TeX&amp;lt;/tt&amp;gt;. Besides, since Lua code is processed at once, things that were previously unthinkable can now be done easily. For instance, it is impossible to perform an assignment in an &amp;lt;tt&amp;gt;\edef&amp;lt;/tt&amp;gt; by TeX&amp;#039;s traditional means. I.e. the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\edef\macro{\count1=5}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; as &amp;lt;tt&amp;gt;\count1=5&amp;lt;/tt&amp;gt; but doesn&amp;#039;t perform the assignment (the &amp;lt;tt&amp;gt;\edef&amp;lt;/tt&amp;gt; does nothing more than a simple &amp;lt;tt&amp;gt;\def&amp;lt;/tt&amp;gt;). After the definition, the value of &amp;lt;tt&amp;gt;\count1&amp;lt;/tt&amp;gt; hasn&amp;#039;t changed. The same is not true, though, if such an assigment is made with Lua code. The following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\edef\macro{\directlua{tex.count[1] = 5}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; emptily (since nothing remains after &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; has been processed) &amp;#039;&amp;#039;and&amp;#039;&amp;#039; sets count 1 to 5. Since such a behavior is totally unexpected in normal TeX, one should be wary when using &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; in such contexts.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=112</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=112"/>
		<updated>2011-07-01T05:05:29Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added link to token_filter.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Documentation and help]] points to other online resources (manuals, mailing list, etc.) related to LuaTeX.&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Attributes]] introduces LuaTeX&amp;#039;s thrilling new concept.&lt;br /&gt;
* Pages on callbacks:&lt;br /&gt;
** [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
** There is a page on the [[Post linebreak filter|&amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;]] callback, explaining and illustrating it with a couple of examples. The [[Show the hyphenation points]] article is another example of use.&lt;br /&gt;
** The page on [[process input buffer|the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback]] illustrates how to read documents with non-UTF-8 encoding, and how to write TeX with lightweight markup instead of the usual commands.&lt;br /&gt;
** Another callback is [[show error hook|&amp;lt;tt&amp;gt;show_error_hook&amp;lt;/tt&amp;gt;]], which lets you enliven your error messages!&lt;br /&gt;
** You can fake XeTeX&amp;#039;s interchar tokens with the [[token filter|&amp;lt;tt&amp;gt;token_filter&amp;lt;/tt&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Token_filter&amp;diff=111</id>
		<title>Token filter</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Token_filter&amp;diff=111"/>
		<updated>2011-07-01T04:59:10Z</updated>

		<summary type="html">&lt;p&gt;Paul: Corrections.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Syntax =&lt;br /&gt;
&lt;br /&gt;
This [[Callbacks|callback]] is called whenever TeX needs a new token. No arguments are passed to the callback, and the return value should be either a Lua table representing a to-be-processed token, a table consisting of a list of such tokens, or something else like &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or an empty table. Since no argument is passed, if you want to get the next token in the document, you should use the &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt; function. Your lua function should therefore look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function()&lt;br /&gt;
    return &amp;lt;table&amp;gt; token&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your Lua function does not return a table representing a valid token, it will be immediately called again, until it eventually does return a useful token or tokenlist (or until you reset the callback value to nil). If your function returns a single usable token, then that token will be processed by LuaTeX immediately. If the function returns a token list (a table consisting of a list of consecutive token tables), then that list will be pushed to the input stack at a completely new token list level. If what is passed to TeX is expandable, then the result of expansion is inserted into input and &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt; will grab it.&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Faking \XeTeXinterchartoks ==&lt;br /&gt;
&lt;br /&gt;
Lua code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% luatexinterchartoks.lua&lt;br /&gt;
&lt;br /&gt;
charclasses = charclasses or {}&lt;br /&gt;
&lt;br /&gt;
function setcharclass (a,b)&lt;br /&gt;
   charclasses[a] = b&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local i = 0&lt;br /&gt;
while i &amp;lt; 65536 do&lt;br /&gt;
  charclasses[i]  = 0&lt;br /&gt;
  i = i + 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
interchartoks =  interchartoks or {}&lt;br /&gt;
&lt;br /&gt;
function setinterchartoks (a,b,c)&lt;br /&gt;
   interchartoks[a] = interchartoks[a] or {}&lt;br /&gt;
   interchartoks[a][b] = c&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local nc, oc&lt;br /&gt;
oc = 255&lt;br /&gt;
&lt;br /&gt;
function do_intertoks () &lt;br /&gt;
  local tok = token.get_next() &lt;br /&gt;
  if tex.count[&amp;#039;XeTeXinterchartokenstate&amp;#039;] == 1 then&lt;br /&gt;
      if tok[1] == 11 or  tok[1] == 12 then&lt;br /&gt;
        nc = charclasses[tok[2]] &lt;br /&gt;
        newchar = tok[2]&lt;br /&gt;
      else &lt;br /&gt;
        nc = 255&lt;br /&gt;
        newchar = &amp;#039;&amp;#039;&lt;br /&gt;
      end&lt;br /&gt;
      local insert  = &amp;#039;&amp;#039;&lt;br /&gt;
      if interchartoks[oc] and interchartoks[oc][nc] then&lt;br /&gt;
          insert = interchartoks[oc][nc] &lt;br /&gt;
          local newtok = tok&lt;br /&gt;
          if insert&amp;lt;100 then&lt;br /&gt;
            local dec = math.floor(insert / 10) + 48;&lt;br /&gt;
            local unit = math.floor(insert % 10) + 48;&lt;br /&gt;
            newtok = {&lt;br /&gt;
              -- \XeTeXinterchartokenstate=0 \the\toks&amp;lt;n&amp;gt; \XeTeXinterchartokenstate=1&lt;br /&gt;
              token.create(&amp;#039;XeTeXinterchartokenstate&amp;#039;),&lt;br /&gt;
              token.create(string.byte(&amp;#039;=&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039;0&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              token.create(&amp;#039;the&amp;#039;),&lt;br /&gt;
              token.create(&amp;#039;toks&amp;#039;),&lt;br /&gt;
              token.create(dec,12),&lt;br /&gt;
              token.create(unit,12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              token.create(&amp;#039;XeTeXinterchartokenstate&amp;#039;),&lt;br /&gt;
              token.create(string.byte(&amp;#039;=&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039;1&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              {tok[1], tok[2], tok[3]}}               &lt;br /&gt;
          end&lt;br /&gt;
          tok = newtok&lt;br /&gt;
      end&lt;br /&gt;
      oc = nc&lt;br /&gt;
  end&lt;br /&gt;
  return tok&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register (&amp;#039;token_filter&amp;#039;, do_intertoks)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
LaTeX package file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% luatexinterchartoks.sty&lt;br /&gt;
\newcount\XeTeXinterchartokenstate&lt;br /&gt;
&lt;br /&gt;
\newcount\charclasses&lt;br /&gt;
\def\newXeTeXintercharclass#1%&lt;br /&gt;
  {\global\advance\charclasses1\relax&lt;br /&gt;
   \newcount#1&lt;br /&gt;
   \global#1=\the\charclasses&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
\newcount\cchone&lt;br /&gt;
\newcount\cchtwo&lt;br /&gt;
&lt;br /&gt;
\def\dodoXeTeXcharclass&lt;br /&gt;
    {\directlua{setcharclass(\the\cchone,\the\cchtwo)}}&lt;br /&gt;
&lt;br /&gt;
\def\doXeTeXcharclass%&lt;br /&gt;
   {\afterassignment\dodoXeTeXcharclass\cchtwo }&lt;br /&gt;
&lt;br /&gt;
\def\XeTeXcharclass%&lt;br /&gt;
   {\afterassignment\doXeTeXcharclass\cchone }&lt;br /&gt;
&lt;br /&gt;
\protected\def\XeTeXdointerchartoks%&lt;br /&gt;
   {\directlua{setinterchartoks(\the\cchone,\the\cchtwo,\the\allocationnumber)}}&lt;br /&gt;
&lt;br /&gt;
\protected\def\dodoXeTeXinterchartoks%&lt;br /&gt;
   {\newtoks\mytoks\afterassignment\XeTeXdointerchartoks\global\mytoks }&lt;br /&gt;
&lt;br /&gt;
\protected\def\doXeTeXinterchartoks%&lt;br /&gt;
   {\afterassignment\dodoXeTeXinterchartoks\cchtwo }&lt;br /&gt;
&lt;br /&gt;
\def\XeTeXinterchartoks%&lt;br /&gt;
   {\afterassignment\doXeTeXinterchartoks\cchone }&lt;br /&gt;
&lt;br /&gt;
\luatexdirectlua{require(&amp;#039;luatexinterchartoks.lua&amp;#039;)}&lt;br /&gt;
&lt;br /&gt;
\endinput&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test document:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\usepackage{luatexinterchartoks}&lt;br /&gt;
\usepackage{color}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
\newXeTeXintercharclass \mycharclassa&lt;br /&gt;
\newXeTeXintercharclass \mycharclassA&lt;br /&gt;
\newXeTeXintercharclass \mycharclassB&lt;br /&gt;
\XeTeXcharclass `\a \mycharclassa&lt;br /&gt;
\XeTeXcharclass `\A \mycharclassA&lt;br /&gt;
\XeTeXcharclass `\B \mycharclassB&lt;br /&gt;
% between &amp;quot;a&amp;quot; and &amp;quot;A&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks \mycharclassa \mycharclassA = {[\itshape}&lt;br /&gt;
\XeTeXinterchartoks \mycharclassA \mycharclassa = {\upshape]}&lt;br /&gt;
% between &amp;quot; &amp;quot; and &amp;quot;B&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks 255 \mycharclassB = {\bgroup\color{blue}}&lt;br /&gt;
\XeTeXinterchartoks \mycharclassB 255 = {\egroup}&lt;br /&gt;
% between &amp;quot;B&amp;quot; and &amp;quot;B&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks \mycharclassB \mycharclassB = {.}&lt;br /&gt;
&lt;br /&gt;
\begingroup&lt;br /&gt;
\XeTeXinterchartokenstate = 1&lt;br /&gt;
aAa A a B aBa BB&lt;br /&gt;
\endgroup&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=100</id>
		<title>Callbacks</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=100"/>
		<updated>2011-05-29T09:49:30Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added process_jobname.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Callbacks allow the user to interact with TeX&amp;#039;s internal operations; those can be enhanced by additional ones, but also replaced altogether. All callbacks are written in Lua.&lt;br /&gt;
&lt;br /&gt;
Since functions registered in callbacks occur in a sequence of actions that makes up TeX&amp;#039;s processing, many of them will receive arguments (and thus should be defined to handle them) and, most importantly, many are supposed to return values. What arguments are passed, if any, and what should be return, vary from callback to callback.&lt;br /&gt;
&lt;br /&gt;
= Registering a callback =&lt;br /&gt;
&lt;br /&gt;
To install a function in a callback one must use the &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; function, whose syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(callback_name, function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;callback_name&amp;lt;/tt&amp;gt; is a string representing a predefined callback name (see below), and &amp;lt;tt&amp;gt;function&amp;lt;/tt&amp;gt; is either a function variable or an anonymous function. The former is just the name of a function previously defined, for instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local my_function = function (an_arg)&lt;br /&gt;
  do_this_or_that(an_arg)&lt;br /&gt;
end&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, my_function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An anonymous function is a function definition that is not assigned to a variable, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function (an_arg) do_this_or_that(an_arg) end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Even in the case of a function variable, what LuaTeX registers is the function itself, and any further assignment to the variable has no effect on the callback.&lt;br /&gt;
&lt;br /&gt;
If registering is successful, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; returns a number, which is the internal representation of the callback; if registering fails (for instance because a callback doesn&amp;#039;t exist, or what is registered isn&amp;#039;t a function), the function returns &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; and a string representing an error message.&lt;br /&gt;
&lt;br /&gt;
Besides functions, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; can take &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;. The first value clears the callback, and returns to the default behavior (i.e. either nothing or TeX&amp;#039;s default processing); the second value prevents anything from happening at this point, even TeX&amp;#039;s default operations, and should be used with great care (the benefits are minor speed gain).&lt;br /&gt;
&lt;br /&gt;
== Several functions in a callback ==&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; erases any function previously registered. I.e. after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_one)&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_two)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
only &amp;lt;tt&amp;gt;function_two&amp;lt;/tt&amp;gt; is registered in &amp;lt;tt&amp;gt;some_callback&amp;lt;/tt&amp;gt;. Hence, if one wants several successive functions to occur in a callback, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; is clearly insufficient. Instead, one must register a metafunction which calls the functions one after the other. This is taken care of in ConTeXt, and there exists &amp;lt;tt&amp;gt;luatexbase-mcb&amp;lt;/tt&amp;gt; for plain TeX and LaTeX, but here&amp;#039;s how to do it oneself. We&amp;#039;ll take the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback has an example; it is called whenever TeX reads an input line, it receives a string (the line) and should return one. A rough approach would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local function_table = { }&lt;br /&gt;
&lt;br /&gt;
local function metafunction (str)&lt;br /&gt;
  for _, fct in ipairs(function_table) do&lt;br /&gt;
    str = fct(str)&lt;br /&gt;
  end&lt;br /&gt;
  return str&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function add_function (fct)&lt;br /&gt;
  table.insert(function_table, fct)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, metafunction)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then one can call &amp;lt;tt&amp;gt;add_function(my_function)&amp;lt;/tt&amp;gt; to add &amp;lt;tt&amp;gt;my_function&amp;lt;/tt&amp;gt; to the list of functions registered in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;metafunction&amp;lt;/tt&amp;gt; will pass the callback&amp;#039;s argument (an input line) to the first function, set that argument to what the function returns, then repeat the process for all functions and finally return the resulting string.&lt;br /&gt;
&lt;br /&gt;
If one also wants to be able to remove functions (and not the entire metafunction), a subtler approach is required, where names are associated with functions and can be used as handles.&lt;br /&gt;
&lt;br /&gt;
= Information about callbacks =&lt;br /&gt;
&lt;br /&gt;
LuaTeX has two functions to get some information about callbacks. The first is &amp;lt;tt&amp;gt;callback.list()&amp;lt;/tt&amp;gt;, called without arguments and returning a table with the callback names as keys and booleans as values: if the value is &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;, it means that something is registered in the callback, or that it is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; (see above). The other function is &amp;lt;tt&amp;gt;callback.find()&amp;lt;/tt&amp;gt;, called with a callback name. If a function is registered, it is returned; if the callback is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, a boolean with that value is returned; finally, if nothing is registered, &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned (note that to distinguish between a boolean set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;, you must use &amp;lt;tt&amp;gt;type()&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
= List of callbacks =&lt;br /&gt;
&lt;br /&gt;
For suggestions on how to document callbacks, see [[Talk:Callbacks|the discussion page]].&lt;br /&gt;
&lt;br /&gt;
== File discovery callbacks ==&lt;br /&gt;
&lt;br /&gt;
These are called whenever an input file is needed. Each receives the &amp;lt;tt&amp;gt;asked_name&amp;lt;/tt&amp;gt;, a string representing what file name was called by the user (for instance, &amp;lt;tt&amp;gt;my_file.tex&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;\input my_file.tex&amp;lt;/tt&amp;gt;) and should return the name of a file to be read (or written to). The &amp;lt;tt&amp;gt;id_number&amp;lt;/tt&amp;gt; in the first two callbacks is the stream&amp;#039;s number plus one, because 0 denotes &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; files.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[find_read_file]] ||id_number, asked_name ||actual_name ||File called with &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_write_file]] ||id_number, asked_name ||actual_name ||&amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file or file called with &amp;lt;tt&amp;gt;\write&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_font_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_output_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_format_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_vf_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_map_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_enc_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_sfd_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_pk_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_data_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_opentype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_truetype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_type_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_image_file]] ||asked_name ||actual_name&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File reading callbacks ==&lt;br /&gt;
&lt;br /&gt;
The previous callbacks were called when a file was asked for; the following are triggered when the file is read. In all, &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt; is the string returned by the file discovery callbacks. The first callback reads &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; files; it must return a table, whose first cell is a function that will be called whenever a line from the input file is readed; the second cell is an optional function executed when the input file terminates.&lt;br /&gt;
&lt;br /&gt;
The other callbacks are used for binary files: &amp;lt;tt&amp;gt;success&amp;lt;/tt&amp;gt; is a boolean, &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; is a string and &amp;lt;tt&amp;gt;data_size&amp;lt;/tt&amp;gt; is a number (the length of &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; in bytes).&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[open_read_file]] ||filename ||{reader, close}&lt;br /&gt;
|-&lt;br /&gt;
|[[read_font_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_vf_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_map_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_enc_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_sfd_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_pk_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_data_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_truetype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_type1_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_opentype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Data processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[process_input_buffer]] ||line ||modified_line ||Called for each input line (after &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;open_read_file&amp;lt;/tt&amp;gt; if TeX is not reading the main file).&lt;br /&gt;
|-&lt;br /&gt;
|[[process_output_buffer]] ||line ||modified_line ||Called for each line TeX writes to an external file; this excludes the &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file, the terminal and &amp;lt;tt&amp;gt;\write18&amp;lt;/tt&amp;gt; calls.&lt;br /&gt;
|-&lt;br /&gt;
|[[process_jobname]] ||string ||modified_string ||Processes the jobname when queried with &amp;lt;tt&amp;gt;\jobname&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;tex.jobname&amp;lt;/tt&amp;gt;. The internal job name (used for the output and log files) remains untouched. (This callback appeared in v.0.71.)&lt;br /&gt;
|-&lt;br /&gt;
|[[token_filter]] || ||token ||Called when TeX needs token; no argument is passed to the callback, the next token must be fetched with &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt;; what is returned is immediately processed by TeX.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Node list processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks deal with node manipulations and occur at various stages of TeX&amp;#039;s typesetting processes.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[buildpage_filter]] ||information || ||Called when TeX moves material to the main vertical list. (This callback is an endangered species.)&lt;br /&gt;
|-&lt;br /&gt;
|[[hyphenate]] ||head, tail || || Inserts discretionary nodes in the node list; called in restricted and unrestricted horizontal mode (i.e. &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;&amp;#039;es and paragraph building). A function doing what TeX does here by default is &amp;lt;tt&amp;gt;lang.hyphenate()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[ligaturing]] ||head, tail || || Same as &amp;lt;tt&amp;gt;hyphenate&amp;lt;/tt&amp;gt;, but applying ligatures (and adding some discretionaries too). An associated function is &amp;lt;tt&amp;gt;node.ligaturing()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[kerning]] ||head, tail || || Same as above, with font kerns. The associated function is &amp;lt;tt&amp;gt;node.kerning()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_linebreak_filter]] ||head, context ||boolean or newhead ||Called in unrestricted horizontal mode, after &amp;lt;tt&amp;gt;kerning&amp;lt;/tt&amp;gt;, and before calling the paragraph builder; it receives the list of nodes that is the contents of the paragraph (plus the &amp;lt;tt&amp;gt;\parfillskip&amp;lt;/tt&amp;gt; glue) and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[linebreak_filter]] ||head, is_display ||newhead ||The paragraph builder. It receives what the previous callback has returned and is in charge of making a paragraph out of it; it must return what it has built. An associated function is &amp;lt;tt&amp;gt;tex.linebreak()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[post_linebreak_filter]] ||head, groupcode, ||boolean or newhead ||Receives what has been returned by &amp;lt;tt&amp;gt;linebreak_filter&amp;lt;/tt&amp;gt; and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[hpack_filter]] ||head, context, size, packtype[, dir] ||boolean or newhead ||Called when TeX is going to build an &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[vpack_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Called when TeX is going to build a &amp;lt;tt&amp;gt;\vbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_output_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Same as &amp;lt;tt&amp;gt;vpack_filter&amp;lt;/tt&amp;gt;, but when TeX packs box 255 (or any other value of &amp;lt;tt&amp;gt;\outputbox&amp;lt;/tt&amp;gt;) and goes to the output routine.&lt;br /&gt;
|-&lt;br /&gt;
|[[mlist_to_hlist]] ||head, display_type, need_penalties ||newhead ||Called when TeX turns a math list into a horizontal list (as described in Appendix G of the &amp;lt;em&amp;gt;TeXbook&amp;lt;/em&amp;gt;).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Information reporting callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks are called in various places when TeX has something to say. None of them takes an argument nor returns anything.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_dump]] ||Called before dumping a format.&lt;br /&gt;
|-&lt;br /&gt;
|[[start_run]] ||LuaTeX&amp;#039;s banner (`This is LuaTeX...&amp;#039;); must be changed in the initialization script, otherwise the banner has already been printed.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_run]] ||TeX&amp;#039;s final words (statistics and `Output written to...&amp;#039;).&lt;br /&gt;
|-&lt;br /&gt;
|[[start_page_number]] ||The opening bracket and the page number printed when a page is shipped out.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_page_number]] ||The closing bracket printed after the previous callback.&lt;br /&gt;
|-&lt;br /&gt;
|[[show_error_hook]] || Called at the beginning of each error message.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
||[[finish_pdffile]] || || ||Called when all pages have been written to the PDF file.&lt;br /&gt;
|-&lt;br /&gt;
|[[define_font]] ||name, size, id ||font_table ||Called when the &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; command is executed; &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; is the filename asked for, &amp;lt;tt&amp;gt;size&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;at&amp;lt;/tt&amp;gt; size, &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; is the number that will be assigned as an internal representation to the font thus loaded. The callback should return a table representing a font.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Process_input_buffer&amp;diff=98</id>
		<title>Process input buffer</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Process_input_buffer&amp;diff=98"/>
		<updated>2011-05-11T10:47:51Z</updated>

		<summary type="html">&lt;p&gt;Paul: Reorganised the sections.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Syntax =&lt;br /&gt;
&lt;br /&gt;
This [[Callbacks|callback]] is called whenever TeX needs a new input line from the file it is currently reading. The argument passed to the function registered there is a string representing the original input line; the function should return another string, to be processed by TeX, or &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;; in the latter case, the original string is processed. So the function should be defined according to the following blueprint:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function (&amp;lt;string&amp;gt; original_line)&lt;br /&gt;
  return &amp;lt;string&amp;gt; adjusted_line or nil&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case TeX isn&amp;#039;t reading the main document but a file that is &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; in it, the callback is called after the &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; function possibly defined in the &amp;lt;tt&amp;gt;[[open_read_file]]&amp;lt;/tt&amp;gt; callback.&lt;br /&gt;
&lt;br /&gt;
The following examples aren&amp;#039;t mutually exclusive; however, if one wanted to register them all in the same callback, special care would be needed, as explained [[Callbacks#Several_functions_in_a_callback|in the main page on callbacks]]. Here each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; removes whatever there might have been in the callback beforehand.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Encoding ==&lt;br /&gt;
&lt;br /&gt;
LuaTeX understands UTF-8 and UTF-8 only (ASCII is a part of it, however). A line containing invalid characters will produce an error message: &amp;lt;em&amp;gt;String contains an invalid utf-8 sequence.&amp;lt;/em&amp;gt; Thus documents with different encoding must be converted, and this can be done in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that such a conversion might not produce anything meaningful when the document is compiled, because the fonts used must have the proper glyphs in the right place; for instance, the Computer Modern fonts don&amp;#039;t have characters with diacritics, and Latin Modern should be used instead. An alternative solution (quite archaic with LuaTeX) is to define the special characters as active, and to make them create a diacritic. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\catcode`\é = 13&lt;br /&gt;
\def é{\&amp;#039;e}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Latin-1 ===&lt;br /&gt;
&lt;br /&gt;
Reading a document written in Latin-1 (ISO-8859-1) is relatively straightforward, because the &amp;lt;tt&amp;gt;slnunicode&amp;lt;/tt&amp;gt; library embedded in LuaTeX does the conversion at once.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local utf8_char, byte, gsub = unicode.utf8.char, string.byte, string.gsub&lt;br /&gt;
&lt;br /&gt;
local function convert_char (char)&lt;br /&gt;
  return utf8_char(byte(char))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function convert_line (line)&lt;br /&gt;
  return gsub(line, &amp;quot;.&amp;quot;, convert_char)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, convert_line)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code reads as follows (bottom up): the &amp;lt;tt&amp;gt;convert_line&amp;lt;/tt&amp;gt; function is registered in the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback; it is passed the original input line as its sole argument, and returns another string, namely that line processed with the &amp;lt;tt&amp;gt;string.gsub()&amp;lt;/tt&amp;gt; function. The latter replaces in a string (the first argument) all the occurrences of a pattern (the second argument) with the outcome of its third argument (for further information on &amp;lt;tt&amp;gt;string.gsub()&amp;lt;/tt&amp;gt; see [http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub the Lua reference manual]).&lt;br /&gt;
&lt;br /&gt;
Here the third argument is a function, &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt; to which is passed whatever matches the pattern. This pattern matches any character, the dot being a magic character. In &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt;, the character is first converted to a numerical code (the codepoint in Latin-1, see the [http://www.lua.org/manual/5.1/manual.html#pdf-string.byte description of &amp;lt;tt&amp;gt;string.byte()&amp;lt;/tt&amp;gt;]); the code is passed to &amp;lt;tt&amp;gt;unicode.utf8.char&amp;lt;/tt&amp;gt; which, given a number, returns the associated character in UTF-8, the number being interpreted as a code point in Unicode; since the characters in Latin-1 have the same code points as in Unicode, the conversion is automatic here.&lt;br /&gt;
&lt;br /&gt;
The use of &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variables ensures speed, and above all that those variables aren&amp;#039;t defined outside the current chunk, for instance, the current &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call or the current Lua file; actually, the code could even be embedded between &amp;lt;tt&amp;gt;do&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end&amp;lt;/tt&amp;gt; and leave absolutely no trace whatsoever.&lt;br /&gt;
&lt;br /&gt;
=== Other 8-bit encodings ===&lt;br /&gt;
&lt;br /&gt;
When using other 8-bit encoding, the previous code won&amp;#039;t work, because it defaults to Latin-1 only. Then one must convert each character one by one by setting up a table matching each input character with the Unicode value; that value can be passed to unicode.utf8.char to yield the desired character.&lt;br /&gt;
&lt;br /&gt;
For instance, here&amp;#039;s the code needed to process a document encoded in Latin/Greek (ISO-8859-7):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local utf8_char, byte = unicode.utf8.char, string.byte&lt;br /&gt;
&lt;br /&gt;
local LatinGreek_table = {&lt;br /&gt;
  [0] = 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,&lt;br /&gt;
  -- ... 240+ other values...&lt;br /&gt;
  0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
local function convert_char (char)&lt;br /&gt;
  return utf8_char(LatinGreek_table[byte(char)])&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function convert_line (line)&lt;br /&gt;
  return gsub(line, &amp;quot;.&amp;quot;, convert_char)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, convert_line)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;convert_line()&amp;lt;/tt&amp;gt; function and call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; are the same as above. What has changed is &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt;; the numerical value of the original character is now used as an index in the &amp;lt;tt&amp;gt;LatinGreek_table&amp;lt;/tt&amp;gt;, and the value returned is the corresponding Unicode code point; that number is passed to &amp;lt;tt&amp;gt;unicode.utf8.char&amp;lt;/tt&amp;gt; to produce the character in UTF-8.&lt;br /&gt;
&lt;br /&gt;
The values in the &amp;lt;tt&amp;gt;LatinGreek_table&amp;lt;/tt&amp;gt; are assigned to the right indices because they are declared in a row (here in hexadecimal form by prefixing them with &amp;lt;tt&amp;gt;0x&amp;lt;/tt&amp;gt;). The only index that needs to be specified is 0, because indexing of tables in Lua begins at 1 by default. The table length is 254 plus the value at index 0 (not 255, because there is no such character in Latin/Greek). Each index is a code point in Latin/Greek, and the value is the code point of the same character in Unicode. For instance, lowercase omega in Latin/Greek is 249, at which index one finds 0x03C9, lowercase omega in Unicode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== TeX as a lightweight markup language ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; can be put to an entirely different use, namely to preprocess input strings using some kind of lightweight markup and turn them into proper TeX.&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s suppose one has two control sequences, &amp;lt;tt&amp;gt;\italic&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\bold&amp;lt;/tt&amp;gt;, taking a single argument; let&amp;#039;s suppose furthermore that one wants to write a sentence like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This is in /italic/ and this is in *bold*.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to be processed by TeX as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This is in \italic{italic} and this is in \bold{bold}.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so as to produce: `This is in &amp;lt;em&amp;gt;italic&amp;lt;/em&amp;gt; and this is in &amp;lt;strong&amp;gt;bold&amp;lt;/strong&amp;gt;.&amp;#039; The following code does exactly that (the use of the percent sing and of &amp;lt;tt&amp;gt;\\&amp;lt;/tt&amp;gt; without &amp;lt;tt&amp;gt;\noexpand&amp;lt;/tt&amp;gt; means that this code is either in a Lua file or in the second version of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; as defined in [[Writing Lua in TeX#Backslash|Writing Lua in TeX]]; also, this code illustrates the registering of an anonymous function instead of a function variable as in the previous examples):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local gsub = string.gsub&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;,&lt;br /&gt;
  function (str)&lt;br /&gt;
    str = gsub(str, &amp;quot;/(.-)/&amp;quot;, &amp;quot;\\italic{%1}&amp;quot;)&lt;br /&gt;
    str = gsub(str, &amp;quot;%*(.-)%*&amp;quot;, &amp;quot;\\bold{%1}&amp;quot;)&lt;br /&gt;
    return str&lt;br /&gt;
  end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What happens is that the original string is replaced with successive call to &amp;lt;tt&amp;gt;string.gsub&amp;lt;/tt&amp;gt; (see [http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub the Lua reference manual]), in which the captures in the patterns are replaced with themselves as arguments to the TeX function (the non-capture parts of the patterns are discarded). For instance, &amp;lt;tt&amp;gt;/a word/&amp;lt;/tt&amp;gt; yields &amp;lt;tt&amp;gt;\italic{a word}&amp;lt;/tt&amp;gt;. Note that with &amp;lt;tt&amp;gt;\bold&amp;lt;/tt&amp;gt;, the asterisks in the pattern must be escaped with &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, otherwise they would be interpreted as magic character. The line can then be processed by TeX as usual.&lt;br /&gt;
&lt;br /&gt;
Only pairs of slashes or asterisks in the same line will be interpreted as markup, because lines are processed one by one and nothing is remembered from one line to the next (that can be implemented, but is a bit more complicated and dangerous). Hence, nothing will be in italics in the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This will /not&lt;br /&gt;
be/ in italic.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead, slashes will be read.&lt;br /&gt;
&lt;br /&gt;
One can add an unlimited number of replacements of the input line. For instance, here&amp;#039;s a way to mark sections by rows of &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt;&amp;#039;s:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*#%s*(.*)&amp;quot;, &amp;quot;\\section{%1}&amp;quot;)&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*##%s*(.*)&amp;quot;, &amp;quot;\\subsection{%1}&amp;quot;)&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*###%s*(.*)&amp;quot;, &amp;quot;\\subsubsection{%1}&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is recognized as a section header is any line beginning with one, two or three hashes, ignoring space before and after. So one can write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# A section&lt;br /&gt;
  ## A subsection&lt;br /&gt;
    ### A subsubsection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More complicated things are possible. However, using the [[open read file]] callback (only with an &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; file) is preferable if subtlety is required, because it can read an entire file (or any set of lines) before changing anything. Also, the powerful [http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html LPeg library] (embedded in LuaTeX) can be used instead of the default operations.&lt;br /&gt;
&lt;br /&gt;
Finally, one must note that such manipulations can be dangerous or lead to unwanted results. For instance, a line such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
... the file should be contained in user/directory/myfiles...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed without slashes and with &amp;lt;em&amp;gt;directory&amp;lt;/em&amp;gt; in italics. Thus, such markup should either be used with files specially tailored for it, or contain ways to be overriden or to protect some text.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=97</id>
		<title>Callbacks</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=97"/>
		<updated>2011-05-11T10:28:43Z</updated>

		<summary type="html">&lt;p&gt;Paul: Link to the post_linebreak_filter page.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Callbacks allow the user to interact with TeX&amp;#039;s internal operations; those can be enhanced by additional ones, but also replaced altogether. All callbacks are written in Lua.&lt;br /&gt;
&lt;br /&gt;
Since functions registered in callbacks occur in a sequence of actions that makes up TeX&amp;#039;s processing, many of them will receive arguments (and thus should be defined to handle them) and, most importantly, many are supposed to return values. What arguments are passed, if any, and what should be return, vary from callback to callback.&lt;br /&gt;
&lt;br /&gt;
= Registering a callback =&lt;br /&gt;
&lt;br /&gt;
To install a function in a callback one must use the &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; function, whose syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(callback_name, function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;callback_name&amp;lt;/tt&amp;gt; is a string representing a predefined callback name (see below), and &amp;lt;tt&amp;gt;function&amp;lt;/tt&amp;gt; is either a function variable or an anonymous function. The former is just the name of a function previously defined, for instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local my_function = function (an_arg)&lt;br /&gt;
  do_this_or_that(an_arg)&lt;br /&gt;
end&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, my_function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An anonymous function is a function definition that is not assigned to a variable, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function (an_arg) do_this_or_that(an_arg) end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Even in the case of a function variable, what LuaTeX registers is the function itself, and any further assignment to the variable has no effect on the callback.&lt;br /&gt;
&lt;br /&gt;
If registering is successful, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; returns a number, which is the internal representation of the callback; if registering fails (for instance because a callback doesn&amp;#039;t exist, or what is registered isn&amp;#039;t a function), the function returns &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; and a string representing an error message.&lt;br /&gt;
&lt;br /&gt;
Besides functions, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; can take &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;. The first value clears the callback, and returns to the default behavior (i.e. either nothing or TeX&amp;#039;s default processing); the second value prevents anything from happening at this point, even TeX&amp;#039;s default operations, and should be used with great care (the benefits are minor speed gain).&lt;br /&gt;
&lt;br /&gt;
== Several functions in a callback ==&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; erases any function previously registered. I.e. after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_one)&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_two)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
only &amp;lt;tt&amp;gt;function_two&amp;lt;/tt&amp;gt; is registered in &amp;lt;tt&amp;gt;some_callback&amp;lt;/tt&amp;gt;. Hence, if one wants several successive functions to occur in a callback, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; is clearly insufficient. Instead, one must register a metafunction which calls the functions one after the other. This is taken care of in ConTeXt, and there exists &amp;lt;tt&amp;gt;luatexbase-mcb&amp;lt;/tt&amp;gt; for plain TeX and LaTeX, but here&amp;#039;s how to do it oneself. We&amp;#039;ll take the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback has an example; it is called whenever TeX reads an input line, it receives a string (the line) and should return one. A rough approach would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local function_table = { }&lt;br /&gt;
&lt;br /&gt;
local function metafunction (str)&lt;br /&gt;
  for _, fct in ipairs(function_table) do&lt;br /&gt;
    str = fct(str)&lt;br /&gt;
  end&lt;br /&gt;
  return str&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function add_function (fct)&lt;br /&gt;
  table.insert(function_table, fct)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, metafunction)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then one can call &amp;lt;tt&amp;gt;add_function(my_function)&amp;lt;/tt&amp;gt; to add &amp;lt;tt&amp;gt;my_function&amp;lt;/tt&amp;gt; to the list of functions registered in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;metafunction&amp;lt;/tt&amp;gt; will pass the callback&amp;#039;s argument (an input line) to the first function, set that argument to what the function returns, then repeat the process for all functions and finally return the resulting string.&lt;br /&gt;
&lt;br /&gt;
If one also wants to be able to remove functions (and not the entire metafunction), a subtler approach is required, where names are associated with functions and can be used as handles.&lt;br /&gt;
&lt;br /&gt;
= Information about callbacks =&lt;br /&gt;
&lt;br /&gt;
LuaTeX has two functions to get some information about callbacks. The first is &amp;lt;tt&amp;gt;callback.list()&amp;lt;/tt&amp;gt;, called without arguments and returning a table with the callback names as keys and booleans as values: if the value is &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;, it means that something is registered in the callback, or that it is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; (see above). The other function is &amp;lt;tt&amp;gt;callback.find()&amp;lt;/tt&amp;gt;, called with a callback name. If a function is registered, it is returned; if the callback is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, a boolean with that value is returned; finally, if nothing is registered, &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned (note that to distinguish between a boolean set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;, you must use &amp;lt;tt&amp;gt;type()&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
= List of callbacks =&lt;br /&gt;
&lt;br /&gt;
For suggestions on how to document callbacks, see [[Talk:Callbacks|the discussion page]].&lt;br /&gt;
&lt;br /&gt;
== File discovery callbacks ==&lt;br /&gt;
&lt;br /&gt;
These are called whenever an input file is needed. Each receives the &amp;lt;tt&amp;gt;asked_name&amp;lt;/tt&amp;gt;, a string representing what file name was called by the user (for instance, &amp;lt;tt&amp;gt;my_file.tex&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;\input my_file.tex&amp;lt;/tt&amp;gt;) and should return the name of a file to be read (or written to). The &amp;lt;tt&amp;gt;id_number&amp;lt;/tt&amp;gt; in the first two callbacks is the stream&amp;#039;s number plus one, because 0 denotes &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; files.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[find_read_file]] ||id_number, asked_name ||actual_name ||File called with &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_write_file]] ||id_number, asked_name ||actual_name ||&amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file or file called with &amp;lt;tt&amp;gt;\write&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_font_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_output_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_format_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_vf_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_map_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_enc_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_sfd_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_pk_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_data_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_opentype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_truetype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_type_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_image_file]] ||asked_name ||actual_name&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File reading callbacks ==&lt;br /&gt;
&lt;br /&gt;
The previous callbacks were called when a file was asked for; the following are triggered when the file is read. In all, &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt; is the string returned by the file discovery callbacks. The first callback reads &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; files; it must return a table, whose first cell is a function that will be called whenever a line from the input file is readed; the second cell is an optional function executed when the input file terminates.&lt;br /&gt;
&lt;br /&gt;
The other callbacks are used for binary files: &amp;lt;tt&amp;gt;success&amp;lt;/tt&amp;gt; is a boolean, &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; is a string and &amp;lt;tt&amp;gt;data_size&amp;lt;/tt&amp;gt; is a number (the length of &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; in bytes).&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[open_read_file]] ||filename ||{reader, close}&lt;br /&gt;
|-&lt;br /&gt;
|[[read_font_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_vf_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_map_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_enc_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_sfd_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_pk_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_data_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_truetype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_type1_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_opentype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Data processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[process_input_buffer]] ||line ||modified_line ||Called for each input line (after &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;open_read_file&amp;lt;/tt&amp;gt; if TeX is not reading the main file).&lt;br /&gt;
|-&lt;br /&gt;
|[[process_output_buffer]] ||line ||modified_line ||Called for each line TeX writes to an external file; this excludes the &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file, the terminal and &amp;lt;tt&amp;gt;\write18&amp;lt;/tt&amp;gt; calls.&lt;br /&gt;
|-&lt;br /&gt;
|[[token_filter]] || ||token ||Called when TeX needs token; no argument is passed to the callback, the next token must be fetched with &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt;; what is returned is immediately processed by TeX.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Node list processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks deal with node manipulations and occur at various stages of TeX&amp;#039;s typesetting processes.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[buildpage_filter]] ||information || ||Called when TeX moves material to the main vertical list. (This callback is an endangered species.)&lt;br /&gt;
|-&lt;br /&gt;
|[[hyphenate]] ||head, tail || || Inserts discretionary nodes in the node list; called in restricted and unrestricted horizontal mode (i.e. &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;&amp;#039;es and paragraph building). A function doing what TeX does here by default is &amp;lt;tt&amp;gt;lang.hyphenate()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[ligaturing]] ||head, tail || || Same as &amp;lt;tt&amp;gt;hyphenate&amp;lt;/tt&amp;gt;, but applying ligatures (and adding some discretionaries too). An associated function is &amp;lt;tt&amp;gt;node.ligaturing()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[kerning]] ||head, tail || || Same as above, with font kerns. The associated function is &amp;lt;tt&amp;gt;node.kerning()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_linebreak_filter]] ||head, context ||boolean or newhead ||Called in unrestricted horizontal mode, after &amp;lt;tt&amp;gt;kerning&amp;lt;/tt&amp;gt;, and before calling the paragraph builder; it receives the list of nodes that is the contents of the paragraph (plus the &amp;lt;tt&amp;gt;\parfillskip&amp;lt;/tt&amp;gt; glue) and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[linebreak_filter]] ||head, is_display ||newhead ||The paragraph builder. It receives what the previous callback has returned and is in charge of making a paragraph out of it; it must return what it has built. An associated function is &amp;lt;tt&amp;gt;tex.linebreak()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[post_linebreak_filter]] ||head, groupcode, ||boolean or newhead ||Receives what has been returned by &amp;lt;tt&amp;gt;linebreak_filter&amp;lt;/tt&amp;gt; and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[hpack_filter]] ||head, context, size, packtype[, dir] ||boolean or newhead ||Called when TeX is going to build an &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[vpack_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Called when TeX is going to build a &amp;lt;tt&amp;gt;\vbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_output_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Same as &amp;lt;tt&amp;gt;vpack_filter&amp;lt;/tt&amp;gt;, but when TeX packs box 255 (or any other value of &amp;lt;tt&amp;gt;\outputbox&amp;lt;/tt&amp;gt;) and goes to the output routine.&lt;br /&gt;
|-&lt;br /&gt;
|[[mlist_to_hlist]] ||head, display_type, need_penalties ||newhead ||Called when TeX turns a math list into a horizontal list (as described in Appendix G of the &amp;lt;em&amp;gt;TeXbook&amp;lt;/em&amp;gt;).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Information reporting callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks are called in various places when TeX has something to say. None of them takes an argument nor returns anything.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_dump]] ||Called before dumping a format.&lt;br /&gt;
|-&lt;br /&gt;
|[[start_run]] ||LuaTeX&amp;#039;s banner (`This is LuaTeX...&amp;#039;); must be changed in the initialization script, otherwise the banner has already been printed.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_run]] ||TeX&amp;#039;s final words (statistics and `Output written to...&amp;#039;).&lt;br /&gt;
|-&lt;br /&gt;
|[[start_page_number]] ||The opening bracket and the page number printed when a page is shipped out.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_page_number]] ||The closing bracket printed after the previous callback.&lt;br /&gt;
|-&lt;br /&gt;
|[[show_error_hook]] || Called at the beginning of each error message.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
||[[finish_pdffile]] || || ||Called when all pages have been written to the PDF file.&lt;br /&gt;
|-&lt;br /&gt;
|[[define_font]] ||name, size, id ||font_table ||Called when the &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; command is executed; &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; is the filename asked for, &amp;lt;tt&amp;gt;size&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;at&amp;lt;/tt&amp;gt; size, &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; is the number that will be assigned as an internal representation to the font thus loaded. The callback should return a table representing a font.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Attributes&amp;diff=96</id>
		<title>Attributes</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Attributes&amp;diff=96"/>
		<updated>2011-05-11T10:27:14Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added link to example in the post_linebreak_filter article.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Attributes are a new concept introduced in LuaTeX. In a way, they are a mix between count registers (they are numbers) and fonts (they are attached to the [[nodes]] created under their scope). They allow information to be carried between different stages of processing.&lt;br /&gt;
&lt;br /&gt;
= Syntax =&lt;br /&gt;
&lt;br /&gt;
Syntactically, attributes are no different from (and can be used as) count registers. Accordingly, primitive commands to handle attribute registers mimick those for count registers: like &amp;lt;tt&amp;gt;\count&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;\attribute&amp;lt;/tt&amp;gt; is used to refer to a register, and like &amp;lt;tt&amp;gt;\countdef&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt; defines a control sequence to be a synonym for a register. The syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attribute &amp;lt;16-bit number&amp;gt; &amp;lt;optional equals&amp;gt; &amp;lt;32-bit number&amp;gt;&lt;br /&gt;
\attributedef &amp;lt;csname&amp;gt; &amp;lt;optional equals&amp;gt; &amp;lt;16-bit number&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is wise to have a \newattribute command, like \newcount (from plain TeX, used also in LaTeX and ConTeXt), for the allocation of registers. The command exists in ConTeXt, and is available in the [http://tug.ctan.org/pkg/luatexbase luatexbase] package for plain TeX and LaTeX under the name \newluatexattribute.&lt;br /&gt;
&lt;br /&gt;
Whenever a count register is allowed, an attribute register is too; that is, primitives like \advance, \multiply, \divide, can be used. More generally, anywhere a number is needed, attributes will do. Of course, assignments to attribute registers obey grouping.&lt;br /&gt;
&lt;br /&gt;
Attributes differ from count registers in two respects:&lt;br /&gt;
&lt;br /&gt;
* They have a special value, -&amp;quot;7FFFFFFF in hexadecimal or -2147483647 in decimal; an attribute with that value (which is default) is said to be &amp;lt;em&amp;gt;unset&amp;lt;/em&amp;gt;. With any larger value, the attribute is &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;.&lt;br /&gt;
* When [[nodes]] are created, they carry with them the values of all the attributes in force at creation time (except for some [[#Asynchronous attribute settings|asynchronous attribute settings]]). In other words, when inspecting a [[nodes|node]] at whatever stage of its processing, one can retrieve (and also modify) the values of the attributes when the node was created. In that respect, attributes behave like fonts (glyph [[nodes]] always carry information about their fonts).&lt;br /&gt;
&lt;br /&gt;
== Box attributes ==&lt;br /&gt;
&lt;br /&gt;
When creating boxes, there exists a new keyword &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; besides &amp;lt;tt&amp;gt;to&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;spread&amp;lt;/tt&amp;gt;. The node representing the box will then have the associate attribute, but not its contents. For instance, after:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attribute0=1&lt;br /&gt;
\setbox0=\hbox attr 1 = 1 {contents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The box (as a node) and each node of its contents have value 1 for attribute 0, but only the box node has value 1 for attribute 1; its contents has whatever value is currently assigned to that attribute (very probably the attribute is unset).&lt;br /&gt;
&lt;br /&gt;
The syntax for the &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; keyword is the following (all spaces are optional):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
attr &amp;lt;number&amp;gt; = &amp;lt;number&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; declaration(s) should precede the &amp;lt;tt&amp;gt;to&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;spread&amp;lt;/tt&amp;gt; declaration if any.&lt;br /&gt;
&lt;br /&gt;
== Asynchronous attribute settings ==&lt;br /&gt;
&lt;br /&gt;
Sometimes [[nodes]] are assigned attribute values depending not on the real values at creation time but on the values of the surrounding nodes. The following simplistic code illustrates that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{\attribute0=1 Ve}\par&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
LuaTeX will insert a font kern between the two letters (if the font says so, of course). That node will have attribute 0 set to 1, even though that is no longer the real value. Indeed, the kern node is created when building the paragraph, i.e. when the &amp;lt;tt&amp;gt;\par&amp;lt;/tt&amp;gt; command is encountered; at that time, attribute 0 no longer has value 1, since that value was assigned in a group and the group is closed. So the kern should normally have whatever value is in force outside the group; instead it is given the value of the nodes that prompted its creation (more precisely, the value of the first node, which can differ from the second one). Similarly, a ligature node takes the values of those nodes it is created from, as does a discretionary node.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;\leftskip&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\rightskip&amp;lt;/tt&amp;gt; glues, inserted when non-zero at the beginning and end of each line of a paragraph, take the value of the neighboring nodes (i.e. the first and last nodes of a line, respectively).&lt;br /&gt;
&lt;br /&gt;
Those asynchronous settings are supposed to make more sense than assignments that would follow the real values. Counter-measures can easily be implemented if necessary: for instance, one can inspect all lines in a paragraph and reassign to the &amp;lt;tt&amp;gt;\leftskip&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\rightskip&amp;lt;/tt&amp;gt; glues the current values for the attributes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= On the Lua side =&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
&lt;br /&gt;
Like all registers, attributes are stored in a Lua table in the &amp;lt;tt&amp;gt;tex&amp;lt;/tt&amp;gt; library. Hence besides &amp;lt;tt&amp;gt;tex.count&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;tex.box&amp;lt;/tt&amp;gt;, etc., there exists &amp;lt;tt&amp;gt;tex.attribute&amp;lt;/tt&amp;gt;. The indices to entries are either a register number or the name of a control sequence defined with &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt;. For instance, after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attributedef\myattribute=100&lt;br /&gt;
\myattribute=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
both &amp;lt;tt&amp;gt;tex.attribute.myattribute&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;tex.attribute[100]&amp;lt;/tt&amp;gt; return 1. The entry can also be assigned to, but then the assignment is necessarily local.&lt;br /&gt;
&lt;br /&gt;
There are also two functions to set or retrieve attribute values, with the following syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.getattribute(&amp;lt;register&amp;gt;)&lt;br /&gt;
tex.setattribute([&amp;quot;global&amp;quot;,] &amp;lt;register&amp;gt;, &amp;lt;number&amp;gt;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In both case &amp;lt;tt&amp;gt;&amp;lt;register&amp;gt;&amp;lt;/tt&amp;gt; is the same as an entry in &amp;lt;tt&amp;gt;tex.attribute&amp;lt;/tt&amp;gt;: either a number or a string representing a control sequence defined with &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt;. The first function returns the current value of the attribute, while the second sets it, either locally or globally if the first optional argument is given.&lt;br /&gt;
&lt;br /&gt;
== Attributes in nodes ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;node&amp;lt;/tt&amp;gt; library offers the following function to deal with the values of attributes for a given node:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.has_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;[, &amp;lt;value&amp;gt;])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Without the third argument, if &amp;lt;tt&amp;gt;&amp;lt;node&amp;gt;&amp;lt;/tt&amp;gt; has &amp;lt;tt&amp;gt;attribute&amp;lt;/tt&amp;gt; &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;, i.e. with a value different from -&amp;quot;7FFFFFFF, then the value is returned; otherwise &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned. If the third argument is given, the value of the attribute is returned if and only if it is equal to &amp;lt;value&amp;gt;, and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; otherwise.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.set_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;, &amp;lt;value&amp;gt;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets &amp;lt;attribute&amp;gt; to &amp;lt;value&amp;gt; for &amp;lt;node&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.unset_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;[, &amp;lt;value&amp;gt;])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unsets &amp;lt;attribute&amp;gt; for &amp;lt;node&amp;gt; and returns the old value; if &amp;lt;value&amp;gt; is present, the attribute is unset only if it matches &amp;lt;value&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Attribute lists ==&lt;br /&gt;
&lt;br /&gt;
In [[nodes]], attributes actually are implemented as nodes in a list. That is why the &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; field of all nodes (except attribute nodes themselves) points to a node of type &amp;lt;tt&amp;gt;attribute_list&amp;lt;/tt&amp;gt;. That node has only one field &amp;lt;tt&amp;gt;next&amp;lt;/tt&amp;gt; pointing to the first attribute node in the list, if any. There might not be any such node because only &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt; attributes are present in the list. Attribute nodes have three fields:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt;: the attribute&amp;#039;s number;&lt;br /&gt;
* &amp;lt;tt&amp;gt;value&amp;lt;/tt&amp;gt;: the attribute&amp;#039;s value;&lt;br /&gt;
* &amp;lt;tt&amp;gt;next&amp;lt;/tt&amp;gt;: the next attribute in the list, i.e. an attribute node that is &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As an example, the following code prints &amp;lt;tt&amp;gt;0 = 5&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;10 = 45&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\setbox0=\hbox attr0 = 5 attr10 = 45 {contents}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
  local attr = tex.box[0].attr.next&lt;br /&gt;
  while attr do&lt;br /&gt;
    texio.write_nl(attr.number .. &amp;quot; = &amp;quot; .. attr.value)&lt;br /&gt;
    attr = attr.next&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
The idea behind attributes is to mark material at a given time so as to spot it at a later stage of processing. The example in the page on the [[Post linebreak filter#Margin notes|&amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;]] [[Callbacks|callback]] shows how attributes can be put to use for margin notes: the text to which a note is added is marked with an attribute, and later, when the paragraph has been built, the note is appended to the line containing that text, identified by its having a given value for the attribute.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=95</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=95"/>
		<updated>2011-05-11T10:22:23Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added link to the article on post_linebreak_filter.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Documentation and help]] points to other online resources (manuals, mailing list, etc.) related to LuaTeX.&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Attributes]] introduces LuaTeX&amp;#039;s thrilling new concept.&lt;br /&gt;
* Pages on callbacks:&lt;br /&gt;
** [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
** There is a page on the [[Post linebreak filter|&amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;]] callback, explaining and illustrating it with a couple of examples. The [[Show the hyphenation points]] article is another example of use.&lt;br /&gt;
** The page on [[process input buffer|the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback]] illustrates how to read documents with non-UTF-8 encoding, and how to write TeX with lightweight markup instead of the usual commands.&lt;br /&gt;
** Another callback is [[show error hook|&amp;lt;tt&amp;gt;show_error_hook&amp;lt;/tt&amp;gt;]], which lets you enliven your error messages!&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Post_linebreak_filter&amp;diff=94</id>
		<title>Post linebreak filter</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Post_linebreak_filter&amp;diff=94"/>
		<updated>2011-05-11T10:14:28Z</updated>

		<summary type="html">&lt;p&gt;Paul: Created page with explanation and examples.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Syntax =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt; [[callbacks|callback]] is called after a paragraph has been built, i.e. after the &amp;lt;tt&amp;gt;[[linebreak_filter]]&amp;lt;/tt&amp;gt; callback or (if the latter isn&amp;#039;t specified) TeX&amp;#039;s default paragraph builder. The callback receives two arguments: the first is the head of the vertical list returned by the previous operation, the second is a string specifying in which context the paragraph is being built: an empty string means the main vertical list (a simple paragraph on the page), and other values include &amp;lt;tt&amp;gt;vbox&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;vtop&amp;lt;/tt&amp;gt;...&lt;br /&gt;
&lt;br /&gt;
The return value should be one of the the following three possibilities: &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; signals that everything is ok and the list that was passed to the callback should be processed further; &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; signals that the list should be flushed from memory: the paragraph simply disappears; finally, if a node list is returned it will be processed instead of the original one. Hence the syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function (&amp;lt;node&amp;gt; head, &amp;lt;string&amp;gt; context)&lt;br /&gt;
  return true | false | &amp;lt;node&amp;gt; newhead&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The callback doesn&amp;#039;t replace any internal code and isn&amp;#039;t expected to do anything meaningful. Its main use is to manipulate the paragraph once built; the list received as the first argument is made mostly of horizontal lists (the lines of text) interspersed with glues (to adjust baseline distances); any kind of postprocessing can be applied to those nodes (not to mention other types of nodes, if any: &amp;lt;tt&amp;gt;\vadjust&amp;lt;/tt&amp;gt;ed material, inserts...).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Between raggedright and justified ==&lt;br /&gt;
&lt;br /&gt;
In paragraph 5.9.6 of [http://mirrors.ctan.org/info/texbytopic/TeXbyTopic.pdf &amp;lt;em&amp;gt;TeX by Topic&amp;lt;/em&amp;gt;], Victor Eijkhout gives a code so that `lines that would stretch beyond certain limits are set with their glue at natural width&amp;#039; (producing a paragraph between raggedright and justified, a style popular in advertisement at the time &amp;lt;em&amp;gt;TeX by Topic&amp;lt;/em&amp;gt; was published).&lt;br /&gt;
&lt;br /&gt;
That is exactly the kind of job the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt; filter is designed for (the smaller the &amp;lt;tt&amp;gt;\hsize&amp;lt;/tt&amp;gt;, the more the result will stand out):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local function check_lines (head)&lt;br /&gt;
  for line in node.traverse_id(node.id(&amp;quot;hlist&amp;quot;), head) do&lt;br /&gt;
    if line.glue_order == 0 and line.glue_sign == 1 and line.glue_set &amp;gt; .4 then&lt;br /&gt;
      line.list = node.hpack(line.list)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;post_linebreak_filter&amp;quot;, check_lines)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the lines in a paragraph are inspected, thanks to the &amp;lt;tt&amp;gt;node.traverse_id()&amp;lt;/tt&amp;gt; iterator, which loops over all the nodes with a given id in a list of node. For each line, one checks whether the following holds: first, the line has been justified with finite glue (it makes little sense to reset lines justified with infinite glue), i.e. the &amp;lt;tt&amp;gt;glue_order&amp;lt;/tt&amp;gt; field of the line is 0 (larger values mean different orders of infinite); second, the glues have been stretched, not shrunk, i.e. &amp;lt;tt&amp;gt;glue_sign&amp;lt;/tt&amp;gt; is 1, not 2; finally, the glue ratio, i.e. the amount of glue used, recorded in &amp;lt;tt&amp;gt;glue_set&amp;lt;/tt&amp;gt; is above an arbitrary threshold: lines whose glues aren&amp;#039;t stretched so much are left untouched.&lt;br /&gt;
&lt;br /&gt;
If the conditions are satisfied, we simply reassign as the contents of the horizontal list that very contents processed with &amp;lt;tt&amp;gt;node.hpack()&amp;lt;/tt&amp;gt;; the latter function turns a list of nodes into material suitable for a horizontal list, somehow like &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;. Here, since no extra argument signalling that the horizontal material should be set to a certain width (as with the keyword &amp;lt;tt&amp;gt;to&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;), glues aren&amp;#039;t stretched nor shrunk and the material is set to its natural width, as wanted.&lt;br /&gt;
&lt;br /&gt;
At the end the head of the list is returned; returning &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; would have the same result, since LuaTeX is supposed to processed the same list that was received as an argument to the callback; that the list have been modified is immaterial here.&lt;br /&gt;
&lt;br /&gt;
== Margin notes ==&lt;br /&gt;
&lt;br /&gt;
The callback can also be used to add material to the lines of text. Margin notes are such material: they are related to something in the paragraph, but as long as the paragraph isn&amp;#039;t built, one doesn&amp;#039;t know where they should be placed. With &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;, since the paragraph can be analysed, the limitation vanishes: one can spot the lines to which a note should be appended (their contents will be marked with [[attributes]]), and add that note.&lt;br /&gt;
&lt;br /&gt;
Note that the following code is meant to illustrate the use of the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt; and of [[attributes]], and isn&amp;#039;t optimal for marginal notes themselves. They would be better dealt with in the output routine: there they can be moved up or down if necessary (so that they don&amp;#039;t bleed into the bottom margin, for instance) and placed in the proper margin (left or right, depending on the page). Here all notes will be placed in the right margin.&lt;br /&gt;
&lt;br /&gt;
The code works along the following lines: the &amp;lt;tt&amp;gt;\marginnote&amp;lt;/tt&amp;gt; command takes two argument, the first being the text in the paragraph to which the note relates, the second the note itself. The text in the paragraph is marked with an [[attributes|attribute]], and can be identified later in the callback, each mark being a new value for the attribute. The note itself is built in a local box and assigned to a PDF Form XObject; the latter move is for practical reasons only and irrelevant to the code here (working with boxes all the way down would yield the same result). After the paragraph is built, we inspect each line of text to check whether it contains material marked with the attribute; if so, the value of that attribute points to a given Form XObject, which is appended to the line.&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s the code for the &amp;lt;tt&amp;gt;\marginnote&amp;lt;/tt&amp;gt; command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\marginnote#1#2{%&lt;br /&gt;
  \begingroup&lt;br /&gt;
  \setbox0=\vtop{%&lt;br /&gt;
    \hsize10em \leftskip1em&lt;br /&gt;
    \rightskip0pt plus 1fill&lt;br /&gt;
    \noindent #2}%&lt;br /&gt;
  \count0=\wd0 \count2=\ht0 \count4=\dp0&lt;br /&gt;
  \pdfxform0&lt;br /&gt;
%&lt;br /&gt;
  \directlua{%&lt;br /&gt;
    local xform  = node.new(node.id(&amp;quot;whatsit&amp;quot;), node.subtype(&amp;quot;pdf_refxform&amp;quot;))&lt;br /&gt;
    xform.objnum = \the\pdflastxform;&lt;br /&gt;
    xform.width  = \the\count0;&lt;br /&gt;
    xform.height = \the\count2;&lt;br /&gt;
    xform.depth  = \the\count4;&lt;br /&gt;
    xforms[\the\pdflastxform] = xform&lt;br /&gt;
    }%&lt;br /&gt;
%&lt;br /&gt;
  \attribute1=\pdflastxform&lt;br /&gt;
  #1%&lt;br /&gt;
  \endgroup&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first part typesets the note in a box (a &amp;lt;tt&amp;gt;\vtop&amp;lt;/tt&amp;gt;, so the first line of the note will be aligned with the text it refers to in the paragraph), which is assigned to an XObject thanks to &amp;lt;tt&amp;gt;\pdfxform&amp;lt;/tt&amp;gt;. The box itself has reduced &amp;lt;tt&amp;gt;\hsize&amp;lt;/tt&amp;gt;, is typeset raggedright, and has a non-zero &amp;lt;tt&amp;gt;\leftskip&amp;lt;/tt&amp;gt; meant to leave a gap between its left margin and the right margin of the paragraph. Other changes (fonts, baseline distances) would be suitable here too.&lt;br /&gt;
&lt;br /&gt;
The second part of the code creates a node using that XObject: that is the kind of node &amp;lt;tt&amp;gt;\pdfrefxform\pdflastxform&amp;lt;/tt&amp;gt; would have created, except we have to set the dimension by hand, which is why the dimensions of the box were recorded (the box has been emptied when assigned to &amp;lt;tt&amp;gt;\pdfxform&amp;lt;/tt&amp;gt;). The node is then stored in a table (to be created permanently in the following code).&lt;br /&gt;
&lt;br /&gt;
The third part simply releases the first argument, to be typeset as part of the paragraph, but marked with a special value of attribute 1 (any attribute would do, of course, and actually one should have something like &amp;lt;tt&amp;gt;\newattribute&amp;lt;/tt&amp;gt; to allocate attributes properly, see [[attributes]]).&lt;br /&gt;
&lt;br /&gt;
The following designs the code to handle the lines in &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt; (and creates the &amp;lt;tt&amp;gt;xforms&amp;lt;/tt&amp;gt; table beforehand):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
xforms = {}&lt;br /&gt;
&lt;br /&gt;
local function find_notes (head)&lt;br /&gt;
  for line in node.traverse_id (node.id(&amp;quot;hlist&amp;quot;), head) do&lt;br /&gt;
    for item in node.traverse (line.list) do&lt;br /&gt;
      local attr = node.has_attribute(item, 1)&lt;br /&gt;
      if attr and xforms[attr] then&lt;br /&gt;
        node.insert_after(line.list, node.tail(line.list), xforms[attr])&lt;br /&gt;
        xforms[attr] = nil&lt;br /&gt;
        break&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;post_linebreak_filter&amp;quot;, find_notes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the node list representing the constructed paragraph, it inspects all lines (i.e. nodes of type &amp;lt;tt&amp;gt;hlist&amp;lt;/tt&amp;gt;). It loops over the material of those lines to check whether some material is marked with attribute 1. If there is, and if the &amp;lt;tt&amp;gt;xforms&amp;lt;/tt&amp;gt; table contains an XObject at the index with that value (a condition to be explained presently), the XObject is simply added at the end of the line&amp;#039;s material with &amp;lt;tt&amp;gt;node.insert_after()&amp;lt;/tt&amp;gt;. The function &amp;lt;tt&amp;gt;node.tail()&amp;lt;/tt&amp;gt; returns the last node of a list, hence &amp;lt;tt&amp;gt;xform&amp;lt;/tt&amp;gt; is added at the very end.&lt;br /&gt;
&lt;br /&gt;
Then the entry in the table is deleted. That is necessary, because the material marked to receive a footnote can end up broken over two lines (or more); if the entry wasn&amp;#039;t deleted, the code would try to add the note twice (once for each line). That is the reason why we check whether the entry &amp;lt;tt&amp;gt;xforms[attr]&amp;lt;/tt&amp;gt; exists.&lt;br /&gt;
&lt;br /&gt;
Finally, &amp;lt;tt&amp;gt;break&amp;lt;/tt&amp;gt; ends the inspection of the line, under the assumption (typographic rather than logical) that there is no more than one note per line.&lt;br /&gt;
&lt;br /&gt;
== Hyphenation points ==&lt;br /&gt;
&lt;br /&gt;
The article [[Show the hyphenation points]] uses the callback to insert markers indicating where hyphenation might have occured in the paragraph; doing so after the paragraph has been built avoids hindering its proper construction.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Attributes&amp;diff=93</id>
		<title>Attributes</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Attributes&amp;diff=93"/>
		<updated>2011-05-10T15:29:46Z</updated>

		<summary type="html">&lt;p&gt;Paul: Small correction.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Attributes are a new concept introduced in LuaTeX. In a way, they are a mix between count registers (they are numbers) and fonts (they are attached to the [[nodes]] created under their scope). They allow information to be carried between different stages of processing.&lt;br /&gt;
&lt;br /&gt;
= Syntax =&lt;br /&gt;
&lt;br /&gt;
Syntactically, attributes are no different from (and can be used as) count registers. Accordingly, primitive commands to handle attribute registers mimick those for count registers: like &amp;lt;tt&amp;gt;\count&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;\attribute&amp;lt;/tt&amp;gt; is used to refer to a register, and like &amp;lt;tt&amp;gt;\countdef&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt; defines a control sequence to be a synonym for a register. The syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attribute &amp;lt;16-bit number&amp;gt; &amp;lt;optional equals&amp;gt; &amp;lt;32-bit number&amp;gt;&lt;br /&gt;
\attributedef &amp;lt;csname&amp;gt; &amp;lt;optional equals&amp;gt; &amp;lt;16-bit number&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is wise to have a \newattribute command, like \newcount (from plain TeX, used also in LaTeX and ConTeXt), for the allocation of registers. The command exists in ConTeXt, and is available in the [http://tug.ctan.org/pkg/luatexbase luatexbase] package for plain TeX and LaTeX under the name \newluatexattribute.&lt;br /&gt;
&lt;br /&gt;
Whenever a count register is allowed, an attribute register is too; that is, primitives like \advance, \multiply, \divide, can be used. More generally, anywhere a number is needed, attributes will do. Of course, assignments to attribute registers obey grouping.&lt;br /&gt;
&lt;br /&gt;
Attributes differ from count registers in two respects:&lt;br /&gt;
&lt;br /&gt;
* They have a special value, -&amp;quot;7FFFFFFF in hexadecimal or -2147483647 in decimal; an attribute with that value (which is default) is said to be &amp;lt;em&amp;gt;unset&amp;lt;/em&amp;gt;. With any larger value, the attribute is &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;.&lt;br /&gt;
* When [[nodes]] are created, they carry with them the values of all the attributes in force at creation time (except for some [[#Asynchronous attribute settings|asynchronous attribute settings]]). In other words, when inspecting a [[nodes|node]] at whatever stage of its processing, one can retrieve (and also modify) the values of the attributes when the node was created. In that respect, attributes behave like fonts (glyph [[nodes]] always carry information about their fonts).&lt;br /&gt;
&lt;br /&gt;
== Box attributes ==&lt;br /&gt;
&lt;br /&gt;
When creating boxes, there exists a new keyword &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; besides &amp;lt;tt&amp;gt;to&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;spread&amp;lt;/tt&amp;gt;. The node representing the box will then have the associate attribute, but not its contents. For instance, after:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attribute0=1&lt;br /&gt;
\setbox0=\hbox attr 1 = 1 {contents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The box (as a node) and each node of its contents have value 1 for attribute 0, but only the box node has value 1 for attribute 1; its contents has whatever value is currently assigned to that attribute (very probably the attribute is unset).&lt;br /&gt;
&lt;br /&gt;
The syntax for the &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; keyword is the following (all spaces are optional):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
attr &amp;lt;number&amp;gt; = &amp;lt;number&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; declaration(s) should precede the &amp;lt;tt&amp;gt;to&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;spread&amp;lt;/tt&amp;gt; declaration if any.&lt;br /&gt;
&lt;br /&gt;
== Asynchronous attribute settings ==&lt;br /&gt;
&lt;br /&gt;
Sometimes [[nodes]] are assigned attribute values depending not on the real values at creation time but on the values of the surrounding nodes. The following simplistic code illustrates that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{\attribute0=1 Ve}\par&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
LuaTeX will insert a font kern between the two letters (if the font says so, of course). That node will have attribute 0 set to 1, even though that is no longer the real value. Indeed, the kern node is created when building the paragraph, i.e. when the &amp;lt;tt&amp;gt;\par&amp;lt;/tt&amp;gt; command is encountered; at that time, attribute 0 no longer has value 1, since that value was assigned in a group and the group is closed. So the kern should normally have whatever value is in force outside the group; instead it is given the value of the nodes that prompted its creation (more precisely, the value of the first node, which can differ from the second one). Similarly, a ligature node takes the values of those nodes it is created from, as does a discretionary node.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;\leftskip&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\rightskip&amp;lt;/tt&amp;gt; glues, inserted when non-zero at the beginning and end of each line of a paragraph, take the value of the neighboring nodes (i.e. the first and last nodes of a line, respectively).&lt;br /&gt;
&lt;br /&gt;
Those asynchronous settings are supposed to make more sense than assignments that would follow the real values. Counter-measures can easily be implemented if necessary: for instance, one can inspect all lines in a paragraph and reassign to the &amp;lt;tt&amp;gt;\leftskip&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\rightskip&amp;lt;/tt&amp;gt; glues the current values for the attributes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= On the Lua side =&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
&lt;br /&gt;
Like all registers, attributes are stored in a Lua table in the &amp;lt;tt&amp;gt;tex&amp;lt;/tt&amp;gt; library. Hence besides &amp;lt;tt&amp;gt;tex.count&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;tex.box&amp;lt;/tt&amp;gt;, etc., there exists &amp;lt;tt&amp;gt;tex.attribute&amp;lt;/tt&amp;gt;. The indices to entries are either a register number or the name of a control sequence defined with &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt;. For instance, after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attributedef\myattribute=100&lt;br /&gt;
\myattribute=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
both &amp;lt;tt&amp;gt;tex.attribute.myattribute&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;tex.attribute[100]&amp;lt;/tt&amp;gt; return 1. The entry can also be assigned to, but then the assignment is necessarily local.&lt;br /&gt;
&lt;br /&gt;
There are also two functions to set or retrieve attribute values, with the following syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.getattribute(&amp;lt;register&amp;gt;)&lt;br /&gt;
tex.setattribute([&amp;quot;global&amp;quot;,] &amp;lt;register&amp;gt;, &amp;lt;number&amp;gt;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In both case &amp;lt;tt&amp;gt;&amp;lt;register&amp;gt;&amp;lt;/tt&amp;gt; is the same as an entry in &amp;lt;tt&amp;gt;tex.attribute&amp;lt;/tt&amp;gt;: either a number or a string representing a control sequence defined with &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt;. The first function returns the current value of the attribute, while the second sets it, either locally or globally if the first optional argument is given.&lt;br /&gt;
&lt;br /&gt;
== Attributes in nodes ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;node&amp;lt;/tt&amp;gt; library offers the following function to deal with the values of attributes for a given node:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.has_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;[, &amp;lt;value&amp;gt;])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Without the third argument, if &amp;lt;tt&amp;gt;&amp;lt;node&amp;gt;&amp;lt;/tt&amp;gt; has &amp;lt;tt&amp;gt;attribute&amp;lt;/tt&amp;gt; &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;, i.e. with a value different from -&amp;quot;7FFFFFFF, then the value is returned; otherwise &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned. If the third argument is given, the value of the attribute is returned if and only if it is equal to &amp;lt;value&amp;gt;, and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; otherwise.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.set_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;, &amp;lt;value&amp;gt;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets &amp;lt;attribute&amp;gt; to &amp;lt;value&amp;gt; for &amp;lt;node&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.unset_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;[, &amp;lt;value&amp;gt;])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unsets &amp;lt;attribute&amp;gt; for &amp;lt;node&amp;gt; and returns the old value; if &amp;lt;value&amp;gt; is present, the attribute is unset only if it matches &amp;lt;value&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Attribute lists ==&lt;br /&gt;
&lt;br /&gt;
In [[nodes]], attributes actually are implemented as nodes in a list. That is why the &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; field of all nodes (except attribute nodes themselves) points to a node of type &amp;lt;tt&amp;gt;attribute_list&amp;lt;/tt&amp;gt;. That node has only one field &amp;lt;tt&amp;gt;next&amp;lt;/tt&amp;gt; pointing to the first attribute node in the list, if any. There might not be any such node because only &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt; attributes are present in the list. Attribute nodes have three fields:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt;: the attribute&amp;#039;s number;&lt;br /&gt;
* &amp;lt;tt&amp;gt;value&amp;lt;/tt&amp;gt;: the attribute&amp;#039;s value;&lt;br /&gt;
* &amp;lt;tt&amp;gt;next&amp;lt;/tt&amp;gt;: the next attribute in the list, i.e. an attribute node that is &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As an example, the following code prints &amp;lt;tt&amp;gt;0 = 5&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;10 = 45&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\setbox0=\hbox attr0 = 5 attr10 = 45 {contents}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
  local attr = tex.box[0].attr.next&lt;br /&gt;
  while attr do&lt;br /&gt;
    texio.write_nl(attr.number .. &amp;quot; = &amp;quot; .. attr.value)&lt;br /&gt;
    attr = attr.next&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=92</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=92"/>
		<updated>2011-05-10T15:08:08Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added link to Attributes.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Documentation and help]] points to other online resources (manuals, mailing list, etc.) related to LuaTeX.&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Attributes]] introduces LuaTeX&amp;#039;s thrilling new concept.&lt;br /&gt;
* Pages on callbacks:&lt;br /&gt;
** [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
** [[Show the hyphenation points]] does what it says; it is also an example of use of the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
** The page on [[process input buffer|the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback]] illustrates how to read documents with non-UTF-8 encoding, and how to write TeX with lightweight markup instead of the usual commands.&lt;br /&gt;
** Another callback is [[show error hook|&amp;lt;tt&amp;gt;show_error_hook&amp;lt;/tt&amp;gt;]], which lets you enliven your error messages!&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Attributes&amp;diff=91</id>
		<title>Attributes</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Attributes&amp;diff=91"/>
		<updated>2011-05-10T15:06:10Z</updated>

		<summary type="html">&lt;p&gt;Paul: How attributes work. There lack examples.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Attributes are a new concept introduced in LuaTeX. In a way, they are a mix between count registers (they are numbers) and fonts (they are attached to the [[nodes]] created under their scope). They allow information to be carried between different stages of processing.&lt;br /&gt;
&lt;br /&gt;
= Syntax =&lt;br /&gt;
&lt;br /&gt;
Syntactically, attributes are no different from (and can be used as) count registers. Accordingly, primitive commands to handle attribute registers mimick those for count registers: like &amp;lt;tt&amp;gt;\count&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;\attribute&amp;lt;/tt&amp;gt; is used to refer to a register, and like &amp;lt;tt&amp;gt;\countdef&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt; defines a control sequence to be a synonym for a register. The syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attribute &amp;lt;16-bit number&amp;gt; &amp;lt;optional equals&amp;gt; &amp;lt;32-bit number&amp;gt;&lt;br /&gt;
\attributedef &amp;lt;csname&amp;gt; &amp;lt;optional equals&amp;gt; &amp;lt;16-bit number&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is wise to have a \newattribute command, like \newcount (from plain TeX, used also in LaTeX and ConTeXt), for the allocation of registers. The command exists in LuaTeX, and is available in the [http://tug.ctan.org/pkg/luatexbase luatexbase] package for plain TeX and LaTeX under the name \newluatexattribute.&lt;br /&gt;
&lt;br /&gt;
Whenever a count register is allowed, an attribute register too; that is, primitives like \advance, \multiply, \divide, can be used. More generally, anywhere a number is needed, attributes will do. Of course, assignments to attribute registers obey grouping.&lt;br /&gt;
&lt;br /&gt;
Attributes differ from count registers in two respects:&lt;br /&gt;
&lt;br /&gt;
* They have a special value, -&amp;quot;7FFFFFFF in hexadecimal or -2147483647 in decimal; an attribute with that value (which is default) is said to be &amp;lt;em&amp;gt;unset&amp;lt;/em&amp;gt;. With any larger value, the attribute is &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;.&lt;br /&gt;
* When [[nodes]] are created, they carry with them the values of all the attributes in force at creation time (except for some asynchronous attribute settings). In other words, when inspecting a [[nodes|node]] at whatever stage of its processing, one can retrieve (and also modify) the values of the attributes when the node was created. In that respect, attributes behave like fonts (glyph [[nodes]] always carry information about their fonts).&lt;br /&gt;
&lt;br /&gt;
== Box attributes ==&lt;br /&gt;
&lt;br /&gt;
When creating boxes, there exists a new keyword &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; besides &amp;lt;tt&amp;gt;to&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;spread&amp;lt;/tt&amp;gt;. The node representing the box will then have the associate attribute, but not its contents. For instance, after:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attribute0=1&lt;br /&gt;
\setbox0=\hbox attr 1 = 1 {contents}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The box (as a node) and each node of its contents have value 1 for attribute 0, but only the box node has value 1 for attribute 1; its contents has whatever value is currently assigned to that attribute (very probably the attribute is unset).&lt;br /&gt;
&lt;br /&gt;
The syntax for the &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; keyword is the following (all spaces are optional):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
attr &amp;lt;number&amp;gt; = &amp;lt;number&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; declaration should precede the &amp;lt;tt&amp;gt;to&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;spread&amp;lt;/tt&amp;gt; declaration if any.&lt;br /&gt;
&lt;br /&gt;
== Asynchronous attribute settings ==&lt;br /&gt;
&lt;br /&gt;
Sometimes [[nodes]] are assigned attribute values depending not on the real values at creation time but on the values of the surrounding nodes. The following simplistic code illustrates that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
{\attribute0=1 Ve}\par&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
LuaTeX will insert a font kern between the two letters (if the font says so, of course). That node will have attribute 0 set to 1, even though that is no longer the real value. Indeed, the kern node is created when building the paragraph, i.e. when the &amp;lt;tt&amp;gt;\par&amp;lt;/tt&amp;gt; command is encountered; at that time, attribute 0 no longer has value 1, since that value was assigned in a group and the group is closed. So the kern should normally have whatever value is in force outside the group; instead it is given the value of the nodes that prompted its creation (more precisely, the value of the first node, which can differ from the second one). Similarly, a ligature node takes the values of those nodes it is created from, as does a discretionary node.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;\leftskip&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\rightskip&amp;lt;/tt&amp;gt; glues, inserted when non-zero at the beginning and end of each line of a paragraph, take the value of the neighboring nodes (i.e. the first and last nodes of a line, respectively).&lt;br /&gt;
&lt;br /&gt;
Those asynchronous settings are supposed to make more sense than assignments that would follow the real values. Counter-measures can easily be implemented if necessary: for instance, one can inspect all lines in a paragraph and reassign to the &amp;lt;tt&amp;gt;\leftskip&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\rightskip&amp;lt;/tt&amp;gt; glues the current values for the attributes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= On the Lua side =&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
&lt;br /&gt;
Like all registers, attributes are stored in a Lua table in the &amp;lt;tt&amp;gt;tex&amp;lt;/tt&amp;gt; library. Hence besides &amp;lt;tt&amp;gt;tex.count&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;tex.box&amp;lt;/tt&amp;gt;, etc., there exists &amp;lt;tt&amp;gt;tex.attribute&amp;lt;/tt&amp;gt;. The indices to entries are either a register number or the name of a control sequence defined with &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt;. For instance, after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\attributedef\myattribute=100&lt;br /&gt;
\myattribute=1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
both &amp;lt;tt&amp;gt;tex.attribute.myattribute&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;tex.attribute[100]&amp;lt;/tt&amp;gt; return 1. The entry can also be assigned to, but then the assignment is necessarily local.&lt;br /&gt;
&lt;br /&gt;
There are also two functions to set or retrieve attribute values, with the following syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.getattribute(&amp;lt;register&amp;gt;)&lt;br /&gt;
tex.setattribute([&amp;quot;global&amp;quot;,] &amp;lt;register&amp;gt;, &amp;lt;number&amp;gt;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In both case &amp;lt;tt&amp;gt;&amp;lt;register&amp;gt;&amp;lt;/tt&amp;gt; is the same as an entry in &amp;lt;tt&amp;gt;tex.attribute&amp;lt;/tt&amp;gt;: either a number or a string representing a control sequence defined with &amp;lt;tt&amp;gt;\attributedef&amp;lt;/tt&amp;gt;. The first function returns the current value of the attribute, while the second sets it, either locally or globally if the first optional argument is given.&lt;br /&gt;
&lt;br /&gt;
== Attributes in nodes ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;node&amp;lt;/tt&amp;gt; library offers the following function to deal with the values of attributes for a given node:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.has_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;[, &amp;lt;value&amp;gt;])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Without the third argument, if &amp;lt;tt&amp;gt;&amp;lt;node&amp;gt;&amp;lt;/tt&amp;gt; has &amp;lt;tt&amp;gt;attribute&amp;lt;/tt&amp;gt; &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;, i.e. with a value different from -&amp;quot;7FFFFFFF, then the value is returned; otherwise &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned. If the third argument is given, the value of the attribute is returned if and only if it is equal to &amp;lt;value&amp;gt;, and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; otherwise.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.set_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;, &amp;lt;value&amp;gt;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets &amp;lt;attribute&amp;gt; to &amp;lt;value&amp;gt; for &amp;lt;node&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
node.unset_attribute(&amp;lt;node&amp;gt;, &amp;lt;attribute&amp;gt;[, &amp;lt;value&amp;gt;])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unsets &amp;lt;attribute&amp;gt; for &amp;lt;node&amp;gt; and returns the old value; if &amp;lt;value&amp;gt; is present, the attribute is unset only if it matches &amp;lt;value&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Attribute lists ==&lt;br /&gt;
&lt;br /&gt;
In [[nodes]], attributes actually are implemented as nodes in a list. That is why the &amp;lt;tt&amp;gt;attr&amp;lt;/tt&amp;gt; field of all nodes (except attribute nodes themselves) points to a node of type &amp;lt;tt&amp;gt;attribute_list&amp;lt;/tt&amp;gt;. That node has only one field &amp;lt;tt&amp;gt;next&amp;lt;/tt&amp;gt; pointing to the first attribute node in the list, if any. There might not be any such node because only &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt; attributes are present in the list. Attribute nodes have three fields:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt;: the attribute&amp;#039;s number;&lt;br /&gt;
* &amp;lt;tt&amp;gt;value&amp;lt;/tt&amp;gt;: the attribute&amp;#039;s value;&lt;br /&gt;
* &amp;lt;tt&amp;gt;next&amp;lt;/tt&amp;gt;: the next attribute in the list, i.e. an attribute node that is &amp;lt;em&amp;gt;set&amp;lt;/em&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As an example, the following code prints &amp;lt;tt&amp;gt;0 = 5&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;10 = 45&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\setbox0=\hbox attr0 = 5 attr10 = 45 {contents}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
  local attr = tex.box[0].attr.next&lt;br /&gt;
  while attr do&lt;br /&gt;
    texio.write_nl(attr.number .. &amp;quot; = &amp;quot; .. attr.value)&lt;br /&gt;
    attr = attr.next&lt;br /&gt;
  end&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Documentation_and_help&amp;diff=90</id>
		<title>Documentation and help</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Documentation_and_help&amp;diff=90"/>
		<updated>2011-05-08T11:21:09Z</updated>

		<summary type="html">&lt;p&gt;Paul: Corrected link.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Documentation=&lt;br /&gt;
&lt;br /&gt;
* The definitive source of information on LuaTeX is the [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf LuaTeX Reference]; the link points to the latest version, documenting latest builds, but can be used for older versions too.&lt;br /&gt;
&lt;br /&gt;
* The previous document reduced to a cheatsheet is also available with the [http://www.luatex.org/svn/trunk/manual/functionref.pdf Short Reference].&lt;br /&gt;
&lt;br /&gt;
* Besides general information, the [http://www.luatex.org/ LuaTeX site] also hosts [http://www.luatex.org/documentation.html some presentations] by members of the team.&lt;br /&gt;
&lt;br /&gt;
* [http://tug.org/TUGboat/ TUGboat] (communications of the TeX User Group) has featured regular articles on LuaTeX for some years now.&lt;br /&gt;
&lt;br /&gt;
* More can be found in publications by local user groups and conferences.&lt;br /&gt;
&lt;br /&gt;
=Help=&lt;br /&gt;
&lt;br /&gt;
* The [http://tug.org/mailman/listinfo/luatex LuaTeX mailing list] is frequented by knowledgeable people and questions rarely remain unanswered.&lt;br /&gt;
&lt;br /&gt;
* There is also a [http://www.ntg.nl/mailman/listinfo/dev-luatex list for developpers] for very technical things.&lt;br /&gt;
&lt;br /&gt;
* Questions about LuaTeX can also be asked in other TeX-related lists or sites: [http://tug.org/mailman/listinfo/texhax texhax], [http://groups.google.com/group/comp.text.tex comp.text.tex] (and similar groups for different languages), or [http://tex.stackexchange.com/ TeX StackExchange].&lt;br /&gt;
&lt;br /&gt;
=On Lua=&lt;br /&gt;
&lt;br /&gt;
* The main source on the Lua language is the [http://www.lua.org/manual/5.1/manual.html Lua reference manual]; the link here documents version 5.1, currently used by LuaTeX, but version 5.2 is on its way and very probably used by LuaTeX when stable; see the [http://www.lua.org/work/doc/manual.html alpha version of the manual for Lua 5.2].&lt;br /&gt;
&lt;br /&gt;
* [http://www.lua.org/pil/index.html Programming in Lua], by Lua&amp;#039;s chief architect Roberto Ierusalimschy, is available online for version 5.0, quite usable for later versions.&lt;br /&gt;
&lt;br /&gt;
* There is also a [http://www.lua.org/lua-l.html mailing list] and a [http://forum.luahub.com/ forum].&lt;br /&gt;
&lt;br /&gt;
* All this and more can be found on the [http://www.lua.org/ Lua website].&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=89</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=89"/>
		<updated>2011-05-08T11:05:30Z</updated>

		<summary type="html">&lt;p&gt;Paul: Removed the section on help and added link to the article Documentation and help.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Documentation and help]] points to other online resources (manuals, mailing list, etc.) related to LuaTeX.&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
* [[Show the hyphenation points]] does what it says; it is also an example of use of the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* The page on [[process input buffer|the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback]] illustrates how to read documents with non-UTF-8 encoding, and how to write TeX with lightweight markup instead of the usual commands.&lt;br /&gt;
* Another callback is [[show error hook|&amp;lt;tt&amp;gt;show_error_hook&amp;lt;/tt&amp;gt;]], which lets you enliven your error messages!&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Documentation_and_help&amp;diff=88</id>
		<title>Documentation and help</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Documentation_and_help&amp;diff=88"/>
		<updated>2011-05-08T11:03:46Z</updated>

		<summary type="html">&lt;p&gt;Paul: Links to documentation and help.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Documentation=&lt;br /&gt;
&lt;br /&gt;
* The definitive source of information on LuaTeX is the [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf LuaTeX Reference]; the link points to the latest version, documenting latest builds, but can be used for older versions too.&lt;br /&gt;
&lt;br /&gt;
* The previous document reduced to a cheatsheet is also available with the [http://www.luatex.org/svn/trunk/manual/functionref.pdf Short Reference].&lt;br /&gt;
&lt;br /&gt;
* Besides general information, the [http://www.luatex.org/ LuaTeX site] also hosts [http://www.luatex.org/documentation.html some presentations] by members of the team.&lt;br /&gt;
&lt;br /&gt;
* [http://tug.org/TUGboat/ TUGboat] (communications of the TeX User Group) has featured regular articles on LuaTeX for some years now.&lt;br /&gt;
&lt;br /&gt;
* More can be found in publications by local user groups and conferences.&lt;br /&gt;
&lt;br /&gt;
=Help=&lt;br /&gt;
&lt;br /&gt;
* The [http://tug.org/mailman/listinfo/luatex LuaTeX mailing list] is frequented by knowledgeable people and questions rarely remain unanswered.&lt;br /&gt;
&lt;br /&gt;
* There is also a [http://www.ntg.nl/mailman/listinfo/dev-luatex list for developpers] for very technical things.&lt;br /&gt;
&lt;br /&gt;
* Questions about LuaTeX can also be asked in other TeX-related lists or sites: [http://tug.org/mailman/listinfo/texhax texhax], [http://groups.google.com/group/comp.text.tex/topics comp.text.tex] (and similar groups for different languages), or [http://tex.stackexchange.com/ TeX StackExchange].&lt;br /&gt;
&lt;br /&gt;
=On Lua=&lt;br /&gt;
&lt;br /&gt;
* The main source on the Lua language is the [http://www.lua.org/manual/5.1/manual.html Lua reference manual]; the link here documents version 5.1, currently used by LuaTeX, but version 5.2 is on its way and very probably used by LuaTeX when stable; see the [http://www.lua.org/work/doc/manual.html alpha version of the manual for Lua 5.2].&lt;br /&gt;
&lt;br /&gt;
* [http://www.lua.org/pil/index.html Programming in Lua], by Lua&amp;#039;s chief architect Roberto Ierusalimschy, is available online for version 5.0, quite usable for later versions.&lt;br /&gt;
&lt;br /&gt;
* There is also a [http://www.lua.org/lua-l.html mailing list] and a [http://forum.luahub.com/ forum].&lt;br /&gt;
&lt;br /&gt;
* All this and more can be found on the [http://www.lua.org/ Lua website].&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=87</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=87"/>
		<updated>2011-05-08T10:27:17Z</updated>

		<summary type="html">&lt;p&gt;Paul: Formatting.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Getting help ==&lt;br /&gt;
&lt;br /&gt;
* There are two mailing lists related to LuaTeX, [http://tug.org/mailman/listinfo/luatex one for general discussions and questions about LuaTeX (&amp;quot;luatex users&amp;quot;)] and [http://www.ntg.nl/mailman/listinfo/dev-luatex one more related to the development of LuaTeX (&amp;quot;dev-luatex&amp;quot;)]&lt;br /&gt;
* There is also [http://www.tug.org/mailman/listinfo/lualatex-dev a LuaLaTeX (LaTeX with LuaTeX) mailing list].&lt;br /&gt;
* [http://tex.stackexchange.com/ tex.stackexchange.com] is a q&amp;amp;a site for questions related to TeX, and you can ask (and answer) LuaTeX related questions there.&lt;br /&gt;
* The [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf online reference manual (pdf)] describes every aspect of LuaTeX, but is rather technical and concise. There also exists [http://www.luatex.org/svn/trunk/manual/functionref.pdf a short reference (pdf)].&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
* [[Show the hyphenation points]] does what it says; it is also an example of use of the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* The page on [[process input buffer|the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback]] illustrates how to read documents with non-UTF-8 encoding, and how to write TeX with lightweight markup instead of the usual commands.&lt;br /&gt;
* Another callback is [[show error hook|&amp;lt;tt&amp;gt;show_error_hook&amp;lt;/tt&amp;gt;]], which lets you enliven your error messages!&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=86</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=86"/>
		<updated>2011-05-08T10:26:25Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added link to show_error_hook.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Getting help ==&lt;br /&gt;
&lt;br /&gt;
* There are two mailing lists related to LuaTeX, [http://tug.org/mailman/listinfo/luatex one for general discussions and questions about LuaTeX (&amp;quot;luatex users&amp;quot;)] and [http://www.ntg.nl/mailman/listinfo/dev-luatex one more related to the development of LuaTeX (&amp;quot;dev-luatex&amp;quot;)]&lt;br /&gt;
* There is also [http://www.tug.org/mailman/listinfo/lualatex-dev a LuaLaTeX (LaTeX with LuaTeX) mailing list].&lt;br /&gt;
* [http://tex.stackexchange.com/ tex.stackexchange.com] is a q&amp;amp;a site for questions related to TeX, and you can ask (and answer) LuaTeX related questions there.&lt;br /&gt;
* The [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf online reference manual (pdf)] describes every aspect of LuaTeX, but is rather technical and concise. There also exists [http://www.luatex.org/svn/trunk/manual/functionref.pdf a short reference (pdf)].&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
* [[Show the hyphenation points]] does what it says; it is also an example of use of the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* The page on [[process input buffer|the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback]] illustrates how to read documents with non-UTF-8 encoding, and how to write TeX with lightweight markup instead of the usual commands.&lt;br /&gt;
* Another callback is [[show error hook]], which lets you enliven your error messages!&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Show_error_hook&amp;diff=85</id>
		<title>Show error hook</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Show_error_hook&amp;diff=85"/>
		<updated>2011-05-08T10:24:34Z</updated>

		<summary type="html">&lt;p&gt;Paul: Description and example of use of the show_error_hook callback.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The show_error_hook callback=&lt;br /&gt;
&lt;br /&gt;
This [[Callbacks|callback]] doesn&amp;#039;t replace any internal code and isn&amp;#039;t supposed to do anything meaningful. It is called when an error message occurs, precisely right after the first line of the error message (beginning with an exclamation mark) has been printed, and before the rest of the message is given.&lt;br /&gt;
&lt;br /&gt;
No argument is passed to the function registered in the callback, and return values, if any, are ignored. Its best use is probably to report additional information besides the error message. For instance, the input text &amp;lt;tt&amp;gt;a_1&amp;lt;/tt&amp;gt; will produce the following error message (and another one quite similar at the end of the paragraph):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
! Missing $ inserted.&lt;br /&gt;
&amp;lt;inserted text&amp;gt; &lt;br /&gt;
                $&lt;br /&gt;
&amp;lt;to be read again&amp;gt; &lt;br /&gt;
                   _&lt;br /&gt;
l.1 a_&lt;br /&gt;
      1&lt;br /&gt;
I&amp;#039;ve inserted a begin-math/end-math symbol since I think&lt;br /&gt;
you left one out. Proceed, with fingers crossed.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This message can be annoying for two reasons: first, if one has a document made of several files, one doesn&amp;#039;t know which file is concerned here (one can read the log file upward to spot the latest input file, but that isn&amp;#039;t very convenient); second, the line number takes some time to arrive after the message&amp;#039;s first line. The lines 2 to 4 aren&amp;#039;t very informative, and it could be useful if the line number was available at once.&lt;br /&gt;
&lt;br /&gt;
The following code inserts the input file&amp;#039;s name and line number just after the first line of the message; thus the rest of the message can be ignored (as many users do, the first line, along with where the error happened, being often informative enough):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;show_error_hook&amp;quot;,&lt;br /&gt;
  function ()&lt;br /&gt;
    texio.write_nl(&amp;quot;[FILE: &amp;quot; .. status.filename .. &amp;quot;, LINE: &amp;quot; .. status.linenumber .. &amp;quot;]&amp;quot;)&lt;br /&gt;
  end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some information from the &amp;lt;tt&amp;gt;status&amp;lt;/tt&amp;gt; library is put to use here: &amp;lt;tt&amp;gt;status.filename&amp;lt;/tt&amp;gt; returns a string with the name of the current input file, &amp;lt;tt&amp;gt;status.linenumber&amp;lt;/tt&amp;gt; returns a number (automatically converted to a string here) indicating the line TeX is currently reading.&lt;br /&gt;
&lt;br /&gt;
Now the error message looks like this, assuming that &amp;lt;tt&amp;gt;a_1&amp;lt;/tt&amp;gt; appeared in file called &amp;lt;tt&amp;gt;test_input.tex&amp;lt;/tt&amp;gt; in the same directory as the main file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
! Missing $ inserted.&lt;br /&gt;
[FILE: ./test_input.tex, LINE: 1]&lt;br /&gt;
&amp;lt;inserted text&amp;gt; &lt;br /&gt;
                $&lt;br /&gt;
&amp;lt;to be read again&amp;gt; &lt;br /&gt;
                   _&lt;br /&gt;
l.1 a_&lt;br /&gt;
      1&lt;br /&gt;
I&amp;#039;ve inserted a begin-math/end-math symbol since I think&lt;br /&gt;
you left one out. Proceed, with fingers crossed.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If one wants a full path instead of a relative path, one can use [http://keplerproject.github.com/luafilesystem/ the LuaFileSystem library], shipped with LuaTeX (the use of &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt; implies that this code isn&amp;#039;t simply passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;; see [[Writing Lua in TeX]]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;show_error_hook&amp;quot;,&lt;br /&gt;
  function ()&lt;br /&gt;
    local f = status.filename&lt;br /&gt;
    f = string.gsub(f, &amp;quot;^%.&amp;quot;, lfs.currentdir())&lt;br /&gt;
    texio.write_nl(&amp;quot;[FILE: &amp;quot; .. f .. &amp;quot;, LINE: &amp;quot; .. status.linenumber .. &amp;quot;]&amp;quot;)&lt;br /&gt;
  end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This replaces &amp;lt;tt&amp;gt;./&amp;lt;/tt&amp;gt; at the beginning of the filename (if any) with the string returned by &amp;lt;tt&amp;gt;lfs.currentdir()&amp;lt;/tt&amp;gt;, whose meaning should be obvious. (See [http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub the Lua reference manual] for information on &amp;lt;tt&amp;gt;string.gsib()&amp;lt;/tt&amp;gt;.)&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Process_input_buffer&amp;diff=84</id>
		<title>Process input buffer</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Process_input_buffer&amp;diff=84"/>
		<updated>2011-05-08T09:23:17Z</updated>

		<summary type="html">&lt;p&gt;Paul: Points to main page on callbacks.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The process_input_buffer callback=&lt;br /&gt;
&lt;br /&gt;
This [[Callbacks|callback]] is called whenever TeX needs a new input line from the file it is currently reading. The argument passed to the function registered there is a string representing the original input line; the function should return another string, to be processed by TeX, or &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;; in the latter case, the original string is processed. So the function should be defined according to the following blueprint:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function (&amp;lt;string&amp;gt; original_line)&lt;br /&gt;
  return &amp;lt;string&amp;gt; adjusted_line or nil&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case TeX isn&amp;#039;t reading the main document but a file that is &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; in it, the callback is called after the &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; function possibly defined in the &amp;lt;tt&amp;gt;[[open_read_file]]&amp;lt;/tt&amp;gt; callback.&lt;br /&gt;
&lt;br /&gt;
The following examples aren&amp;#039;t mutually exclusive; however, if one wanted to register them all in the same callback, special care would be needed, as explained [[Callbacks#Several_functions_in_a_callback|in the main page on callbacks]]. Here each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; removes whatever there might have been in the callback beforehand.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Encoding=&lt;br /&gt;
&lt;br /&gt;
LuaTeX understands UTF-8 and UTF-8 only (ASCII is a part of it, however). A line containing invalid characters will produce an error message: &amp;lt;em&amp;gt;String contains an invalid utf-8 sequence.&amp;lt;/em&amp;gt; Thus documents with different encoding must be converted, and this can be done in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that such a conversion might not produce anything meaningful when the document is compiled, because the fonts used must have the proper glyphs in the right place; for instance, the Computer Modern fonts don&amp;#039;t have characters with diacritics, and Latin Modern should be used instead. An alternative solution (quite archaic with LuaTeX) is to define the special characters as active, and to make them create a diacritic. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\catcode`\é = 13&lt;br /&gt;
\def é{\&amp;#039;e}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Latin-1==&lt;br /&gt;
&lt;br /&gt;
Reading a document written in Latin-1 (ISO-8859-1) is relatively straightforward, because the &amp;lt;tt&amp;gt;slnunicode&amp;lt;/tt&amp;gt; library embedded in LuaTeX does the conversion at once.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local utf8_char, byte, gsub = unicode.utf8.char, string.byte, string.gsub&lt;br /&gt;
&lt;br /&gt;
local function convert_char (char)&lt;br /&gt;
  return utf8_char(byte(char))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function convert_line (line)&lt;br /&gt;
  return gsub(line, &amp;quot;.&amp;quot;, convert_char)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, convert_line)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code reads as follows (bottom up): the &amp;lt;tt&amp;gt;convert_line&amp;lt;/tt&amp;gt; function is registered in the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback; it is passed the original input line as its sole argument, and returns another string, namely that line processed with the &amp;lt;tt&amp;gt;string.gsub()&amp;lt;/tt&amp;gt; function. The latter replaces in a string (the first argument) all the occurrences of a pattern (the second argument) with the outcome of its third argument (for further information on &amp;lt;tt&amp;gt;string.gsub()&amp;lt;/tt&amp;gt; see [http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub the Lua reference manual]).&lt;br /&gt;
&lt;br /&gt;
Here the third argument is a function, &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt; to which is passed whatever matches the pattern. This pattern matches any character, the dot being a magic character. In &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt;, the character is first converted to a numerical code (the codepoint in Latin-1, see the [http://www.lua.org/manual/5.1/manual.html#pdf-string.byte description of &amp;lt;tt&amp;gt;string.byte()&amp;lt;/tt&amp;gt;]); the code is passed to &amp;lt;tt&amp;gt;unicode.utf8.char&amp;lt;/tt&amp;gt; which, given a number, returns the associated character in UTF-8, the number being interpreted as a code point in Unicode; since the characters in Latin-1 have the same code points as in Unicode, the conversion is automatic here.&lt;br /&gt;
&lt;br /&gt;
The use of &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variables ensures speed, and above all that those variables aren&amp;#039;t defined outside the current chunk, for instance, the current &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call or the current Lua file; actually, the code could even be embedded between &amp;lt;tt&amp;gt;do&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end&amp;lt;/tt&amp;gt; and leave absolutely no trace whatsoever.&lt;br /&gt;
&lt;br /&gt;
==Other 8-bit encodings==&lt;br /&gt;
&lt;br /&gt;
When using other 8-bit encoding, the previous code won&amp;#039;t work, because it defaults to Latin-1 only. Then one must convert each character one by one by setting up a table matching each input character with the Unicode value; that value can be passed to unicode.utf8.char to yield the desired character.&lt;br /&gt;
&lt;br /&gt;
For instance, here&amp;#039;s the code needed to process a document encoded in Latin/Greek (ISO-8859-7):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local utf8_char, byte = unicode.utf8.char, string.byte&lt;br /&gt;
&lt;br /&gt;
local LatinGreek_table = {&lt;br /&gt;
  [0] = 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,&lt;br /&gt;
  -- ... 240+ other values...&lt;br /&gt;
  0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
local function convert_char (char)&lt;br /&gt;
  return utf8_char(LatinGreek_table[byte(char)])&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function convert_line (line)&lt;br /&gt;
  return gsub(line, &amp;quot;.&amp;quot;, convert_char)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, convert_line)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;convert_line()&amp;lt;/tt&amp;gt; function and call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; are the same as above. What has changed is &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt;; the numerical value of the original character is now used as an index in the &amp;lt;tt&amp;gt;LatinGreek_table&amp;lt;/tt&amp;gt;, and the value returned is the corresponding Unicode code point; that number is passed to &amp;lt;tt&amp;gt;unicode.utf8.char&amp;lt;/tt&amp;gt; to produce the character in UTF-8.&lt;br /&gt;
&lt;br /&gt;
The values in the &amp;lt;tt&amp;gt;LatinGreek_table&amp;lt;/tt&amp;gt; are assigned to the right indices because they are declared in a row (here in hexadecimal form by prefixing them with &amp;lt;tt&amp;gt;0x&amp;lt;/tt&amp;gt;). The only index that needs to be specified is 0, because indexing of tables in Lua begins at 1 by default. The table length is 254 plus the value at index 0 (not 255, because there is no such character in Latin/Greek). Each index is a code point in Latin/Greek, and the value is the code point of the same character in Unicode. For instance, lowercase omega in Latin/Greek is 249, at which index one finds 0x03C9, lowercase omega in Unicode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=TeX as a lightweight markup language=&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; can be put to an entirely different use, namely to preprocess input strings using some kind of lightweight markup and turn them into proper TeX.&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s suppose one has two control sequences, &amp;lt;tt&amp;gt;\italic&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\bold&amp;lt;/tt&amp;gt;, taking a single argument; let&amp;#039;s suppose furthermore that one wants to write a sentence like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This is in /italic/ and this is in *bold*.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to be processed by TeX as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This is in \italic{italic} and this is in \bold{bold}.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so as to produce: `This is in &amp;lt;em&amp;gt;italic&amp;lt;/em&amp;gt; and this is in &amp;lt;strong&amp;gt;bold&amp;lt;/strong&amp;gt;.&amp;#039; The following code does exactly that (the use of the percent sing and of &amp;lt;tt&amp;gt;\\&amp;lt;/tt&amp;gt; without &amp;lt;tt&amp;gt;\noexpand&amp;lt;/tt&amp;gt; means that this code is either in a Lua file or in the second version of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; as defined in [[Writing Lua in TeX#Backslash|Writing Lua in TeX]]; also, this code illustrates the registering of an anonymous function instead of a function variable as in the previous examples):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local gsub = string.gsub&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;,&lt;br /&gt;
  function (str)&lt;br /&gt;
    str = gsub(str, &amp;quot;/(.-)/&amp;quot;, &amp;quot;\\italic{%1}&amp;quot;)&lt;br /&gt;
    str = gsub(str, &amp;quot;%*(.-)%*&amp;quot;, &amp;quot;\\bold{%1}&amp;quot;)&lt;br /&gt;
    return str&lt;br /&gt;
  end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What happens is that the original string is replaced with successive call to &amp;lt;tt&amp;gt;string.gsub&amp;lt;/tt&amp;gt; (see [http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub the Lua reference manual]), in which the captures in the patterns are replaced with themselves as arguments to the TeX function (the non-capture parts of the patterns are discarded). For instance, &amp;lt;tt&amp;gt;/a word/&amp;lt;/tt&amp;gt; yields &amp;lt;tt&amp;gt;\italic{a word}&amp;lt;/tt&amp;gt;. Note that with &amp;lt;tt&amp;gt;\bold&amp;lt;/tt&amp;gt;, the asterisks in the pattern must be escaped with &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, otherwise they would be interpreted as magic character. The line can then be processed by TeX as usual.&lt;br /&gt;
&lt;br /&gt;
Only pairs of slashes or asterisks in the same line will be interpreted as markup, because lines are processed one by one and nothing is remembered from one line to the next (that can be implemented, but is a bit more complicated and dangerous). Hence, nothing will be in italics in the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This will /not&lt;br /&gt;
be/ in italic.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead, slashes will be read.&lt;br /&gt;
&lt;br /&gt;
One can add an unlimited number of replacements of the input line. For instance, here&amp;#039;s a way to mark sections by rows of &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt;&amp;#039;s:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*#%s*(.*)&amp;quot;, &amp;quot;\\section{%1}&amp;quot;)&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*##%s*(.*)&amp;quot;, &amp;quot;\\subsection{%1}&amp;quot;)&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*###%s*(.*)&amp;quot;, &amp;quot;\\subsubsection{%1}&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is recognized as a section header is any line beginning with one, two or three hashes, ignoring space before and after. So one can write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# A section&lt;br /&gt;
  ## A subsection&lt;br /&gt;
    ### A subsubsection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More complicated things are possible. However, using the [[open read file]] callback (only with an &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; file) is preferable if subtlety is required, because it can read an entire file (or any set of lines) before changing anything. Also, the powerful [http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html LPeg library] (embedded in LuaTeX) can be used instead of the default operations.&lt;br /&gt;
&lt;br /&gt;
Finally, one must note that such manipulations can be dangerous or lead to unwanted results. For instance, a line such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
... the file should be contained in user/directory/myfiles...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed without slashes and with &amp;lt;em&amp;gt;directory&amp;lt;/em&amp;gt; in italics. Thus, such markup should either be used with files specially tailored for it, or contain ways to be overriden or to protect some text.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=83</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=83"/>
		<updated>2011-05-07T17:33:52Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added link to process_input_buffer.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Getting help ==&lt;br /&gt;
&lt;br /&gt;
* There are two mailing lists related to LuaTeX, [http://tug.org/mailman/listinfo/luatex one for general discussions and questions about LuaTeX (&amp;quot;luatex users&amp;quot;)] and [http://www.ntg.nl/mailman/listinfo/dev-luatex one more related to the development of LuaTeX (&amp;quot;dev-luatex&amp;quot;)]&lt;br /&gt;
* There is also [http://www.tug.org/mailman/listinfo/lualatex-dev a LuaLaTeX (LaTeX with LuaTeX) mailing list].&lt;br /&gt;
* [http://tex.stackexchange.com/ tex.stackexchange.com] is a q&amp;amp;a site for questions related to TeX, and you can ask (and answer) LuaTeX related questions there.&lt;br /&gt;
* The [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf online reference manual (pdf)] describes every aspect of LuaTeX, but is rather technical and concise. There also exists [http://www.luatex.org/svn/trunk/manual/functionref.pdf a short reference (pdf)].&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
* [[Show the hyphenation points]] does what it says; it is also an example of use of the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* The page on [[process input buffer|the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback]] illustrates how to read documents with non-UTF-8 encoding, and how to write TeX with lightweight markup instead of the usual commands.&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Process_input_buffer&amp;diff=82</id>
		<title>Process input buffer</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Process_input_buffer&amp;diff=82"/>
		<updated>2011-05-07T17:30:42Z</updated>

		<summary type="html">&lt;p&gt;Paul: Examples of use of the process_input_buffer callback.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=The process_input_buffer callback=&lt;br /&gt;
&lt;br /&gt;
This callback is called whenever TeX needs a new input line from the file it is currently reading. The argument passed to the function registered there is a string representing the original input line; the function should return another string, to be processed by TeX, or &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;; in the latter case, the original string is processed. So the function should be defined according to the following blueprint:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function (&amp;lt;string&amp;gt; original_line)&lt;br /&gt;
  return &amp;lt;string&amp;gt; adjusted_line or nil&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In case TeX isn&amp;#039;t reading the main document but a file that is &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; in it, the callback is called after the &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; function possibly defined in the &amp;lt;tt&amp;gt;[[open_read_file]]&amp;lt;/tt&amp;gt; callback.&lt;br /&gt;
&lt;br /&gt;
The following examples aren&amp;#039;t mutually exclusive; however, if one wanted to register them all in the same callback, special care would be needed, as explained [[Callbacks#Several_functions_in_a_callback|in the main page on callbacks]]. Here each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; removes whatever there might have been in the callback beforehand.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Encoding=&lt;br /&gt;
&lt;br /&gt;
LuaTeX understands UTF-8 and UTF-8 only (ASCII is a part of it, however). A line containing invalid characters will produce an error message: &amp;lt;em&amp;gt;String contains an invalid utf-8 sequence.&amp;lt;/em&amp;gt; Thus documents with different encoding must be converted, and this can be done in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that such a conversion might not produce anything meaningful when the document is compiled, because the fonts used must have the proper glyphs in the right place; for instance, the Computer Modern fonts don&amp;#039;t have characters with diacritics, and Latin Modern should be used instead. An alternative solution (quite archaic with LuaTeX) is to define the special characters as active, and to make them create a diacritic. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\catcode`\é = 13&lt;br /&gt;
\def é{\&amp;#039;e}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Latin-1==&lt;br /&gt;
&lt;br /&gt;
Reading a document written in Latin-1 (ISO-8859-1) is relatively straightforward, because the &amp;lt;tt&amp;gt;slnunicode&amp;lt;/tt&amp;gt; library embedded in LuaTeX does the conversion at once.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local utf8_char, byte, gsub = unicode.utf8.char, string.byte, string.gsub&lt;br /&gt;
&lt;br /&gt;
local function convert_char (char)&lt;br /&gt;
  return utf8_char(byte(char))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function convert_line (line)&lt;br /&gt;
  return gsub(line, &amp;quot;.&amp;quot;, convert_char)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, convert_line)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code reads as follows (bottom up): the &amp;lt;tt&amp;gt;convert_line&amp;lt;/tt&amp;gt; function is registered in the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback; it is passed the original input line as its sole argument, and returns another string, namely that line processed with the &amp;lt;tt&amp;gt;string.gsub()&amp;lt;/tt&amp;gt; function. The latter replaces in a string (the first argument) all the occurrences of a pattern (the second argument) with the outcome of its third argument (for further information on &amp;lt;tt&amp;gt;string.gsub()&amp;lt;/tt&amp;gt; see [http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub the Lua reference manual]).&lt;br /&gt;
&lt;br /&gt;
Here the third argument is a function, &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt; to which is passed whatever matches the pattern. This pattern matches any character, the dot being a magic character. In &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt;, the character is first converted to a numerical code (the codepoint in Latin-1, see the [http://www.lua.org/manual/5.1/manual.html#pdf-string.byte description of &amp;lt;tt&amp;gt;string.byte()&amp;lt;/tt&amp;gt;]); the code is passed to &amp;lt;tt&amp;gt;unicode.utf8.char&amp;lt;/tt&amp;gt; which, given a number, returns the associated character in UTF-8, the number being interpreted as a code point in Unicode; since the characters in Latin-1 have the same code points as in Unicode, the conversion is automatic here.&lt;br /&gt;
&lt;br /&gt;
The use of &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variables ensures speed, and above all that those variables aren&amp;#039;t defined outside the current chunk, for instance, the current &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call or the current Lua file; actually, the code could even be embedded between &amp;lt;tt&amp;gt;do&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;end&amp;lt;/tt&amp;gt; and leave absolutely no trace whatsoever.&lt;br /&gt;
&lt;br /&gt;
==Other 8-bit encodings==&lt;br /&gt;
&lt;br /&gt;
When using other 8-bit encoding, the previous code won&amp;#039;t work, because it defaults to Latin-1 only. Then one must convert each character one by one by setting up a table matching each input character with the Unicode value; that value can be passed to unicode.utf8.char to yield the desired character.&lt;br /&gt;
&lt;br /&gt;
For instance, here&amp;#039;s the code needed to process a document encoded in Latin/Greek (ISO-8859-7):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local utf8_char, byte = unicode.utf8.char, string.byte&lt;br /&gt;
&lt;br /&gt;
local LatinGreek_table = {&lt;br /&gt;
  [0] = 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,&lt;br /&gt;
  -- ... 240+ other values...&lt;br /&gt;
  0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
local function convert_char (char)&lt;br /&gt;
  return utf8_char(LatinGreek_table[byte(char)])&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function convert_line (line)&lt;br /&gt;
  return gsub(line, &amp;quot;.&amp;quot;, convert_char)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, convert_line)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;convert_line()&amp;lt;/tt&amp;gt; function and call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; are the same as above. What has changed is &amp;lt;tt&amp;gt;convert_char()&amp;lt;/tt&amp;gt;; the numerical value of the original character is now used as an index in the &amp;lt;tt&amp;gt;LatinGreek_table&amp;lt;/tt&amp;gt;, and the value returned is the corresponding Unicode code point; that number is passed to &amp;lt;tt&amp;gt;unicode.utf8.char&amp;lt;/tt&amp;gt; to produce the character in UTF-8.&lt;br /&gt;
&lt;br /&gt;
The values in the &amp;lt;tt&amp;gt;LatinGreek_table&amp;lt;/tt&amp;gt; are assigned to the right indices because they are declared in a row (here in hexadecimal form by prefixing them with &amp;lt;tt&amp;gt;0x&amp;lt;/tt&amp;gt;). The only index that needs to be specified is 0, because indexing of tables in Lua begins at 1 by default. The table length is 254 plus the value at index 0 (not 255, because there is no such character in Latin/Greek). Each index is a code point in Latin/Greek, and the value is the code point of the same character in Unicode. For instance, lowercase omega in Latin/Greek is 249, at which index one finds 0x03C9, lowercase omega in Unicode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=TeX as a lightweight markup language=&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; can be put to an entirely different use, namely to preprocess input strings using some kind of lightweight markup and turn them into proper TeX.&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s suppose one has two control sequences, &amp;lt;tt&amp;gt;\italic&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\bold&amp;lt;/tt&amp;gt;, taking a single argument; let&amp;#039;s suppose furthermore that one wants to write a sentence like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This is in /italic/ and this is in *bold*.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to be processed by TeX as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This is in \italic{italic} and this is in \bold{bold}.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so as to produce: `This is in &amp;lt;em&amp;gt;italic&amp;lt;/em&amp;gt; and this is in &amp;lt;strong&amp;gt;bold&amp;lt;/strong&amp;gt;.&amp;#039; The following code does exactly that (the use of the percent sing and of &amp;lt;tt&amp;gt;\\&amp;lt;/tt&amp;gt; without &amp;lt;tt&amp;gt;\noexpand&amp;lt;/tt&amp;gt; means that this code is either in a Lua file or in the second version of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; as defined in [[Writing Lua in TeX#Backslash|Writing Lua in TeX]]; also, this code illustrates the registering of an anonymous function instead of a function variable as in the previous examples):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local gsub = string.gsub&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;,&lt;br /&gt;
  function (str)&lt;br /&gt;
    str = gsub(str, &amp;quot;/(.-)/&amp;quot;, &amp;quot;\\italic{%1}&amp;quot;)&lt;br /&gt;
    str = gsub(str, &amp;quot;%*(.-)%*&amp;quot;, &amp;quot;\\bold{%1}&amp;quot;)&lt;br /&gt;
    return str&lt;br /&gt;
  end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What happens is that the original string is replaced with successive call to &amp;lt;tt&amp;gt;string.gsub&amp;lt;/tt&amp;gt; (see [http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub the Lua reference manual]), in which the captures in the patterns are replaced with themselves as arguments to the TeX function (the non-capture parts of the patterns are discarded). For instance, &amp;lt;tt&amp;gt;/a word/&amp;lt;/tt&amp;gt; yields &amp;lt;tt&amp;gt;\italic{a word}&amp;lt;/tt&amp;gt;. Note that with &amp;lt;tt&amp;gt;\bold&amp;lt;/tt&amp;gt;, the asterisks in the pattern must be escaped with &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, otherwise they would be interpreted as magic character. The line can then be processed by TeX as usual.&lt;br /&gt;
&lt;br /&gt;
Only pairs of slashes or asterisks in the same line will be interpreted as markup, because lines are processed one by one and nothing is remembered from one line to the next (that can be implemented, but is a bit more complicated and dangerous). Hence, nothing will be in italics in the following example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
This will /not&lt;br /&gt;
be/ in italic.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead, slashes will be read.&lt;br /&gt;
&lt;br /&gt;
One can add an unlimited number of replacements of the input line. For instance, here&amp;#039;s a way to mark sections by rows of &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt;&amp;#039;s:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*#%s*(.*)&amp;quot;, &amp;quot;\\section{%1}&amp;quot;)&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*##%s*(.*)&amp;quot;, &amp;quot;\\subsection{%1}&amp;quot;)&lt;br /&gt;
  str = gsub(str, &amp;quot;^%s*###%s*(.*)&amp;quot;, &amp;quot;\\subsubsection{%1}&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is recognized as a section header is any line beginning with one, two or three hashes, ignoring space before and after. So one can write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# A section&lt;br /&gt;
  ## A subsection&lt;br /&gt;
    ### A subsubsection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More complicated things are possible. However, using the [[open read file]] callback (only with an &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; file) is preferable if subtlety is required, because it can read an entire file (or any set of lines) before changing anything. Also, the powerful [http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html LPeg library] (embedded in LuaTeX) can be used instead of the default operations.&lt;br /&gt;
&lt;br /&gt;
Finally, one must note that such manipulations can be dangerous or lead to unwanted results. For instance, a line such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
... the file should be contained in user/directory/myfiles...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed without slashes and with &amp;lt;em&amp;gt;directory&amp;lt;/em&amp;gt; in italics. Thus, such markup should either be used with files specially tailored for it, or contain ways to be overriden or to protect some text.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=81</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=81"/>
		<updated>2011-05-07T14:21:33Z</updated>

		<summary type="html">&lt;p&gt;Paul: Corrected link.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Getting help ==&lt;br /&gt;
&lt;br /&gt;
* There are two mailing lists related to LuaTeX, [http://tug.org/mailman/listinfo/luatex one for general discussions and questions about LuaTeX (&amp;quot;luatex users&amp;quot;)] and [http://www.ntg.nl/mailman/listinfo/dev-luatex one more related to the development of LuaTeX (&amp;quot;dev-luatex&amp;quot;)]&lt;br /&gt;
* There is also [http://www.tug.org/mailman/listinfo/lualatex-dev a LuaLaTeX (LaTeX with LuaTeX) mailing list].&lt;br /&gt;
* [http://tex.stackexchange.com/ tex.stackexchange.com] is a q&amp;amp;a site for questions related to TeX, and you can ask (and answer) LuaTeX related questions there.&lt;br /&gt;
* The [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf online reference manual (pdf)] describes every aspect of LuaTeX, but is rather technical and concise. There also exists [http://www.luatex.org/svn/trunk/manual/functionref.pdf a short reference (pdf)].&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
* [[Show the hyphenation points]] does what it says; it is also an example of use of the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=80</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=80"/>
		<updated>2011-05-07T14:19:09Z</updated>

		<summary type="html">&lt;p&gt;Paul: Mentionned \functionref; also introduces the articles written especially for this wiki.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Getting help ==&lt;br /&gt;
&lt;br /&gt;
* There are two mailing lists related to LuaTeX, [http://tug.org/mailman/listinfo/luatex one for general discussions and questions about LuaTeX (&amp;quot;luatex users&amp;quot;)] and [http://www.ntg.nl/mailman/listinfo/dev-luatex one more related to the development of LuaTeX (&amp;quot;dev-luatex&amp;quot;)]&lt;br /&gt;
* There is also [http://www.tug.org/mailman/listinfo/lualatex-dev a LuaLaTeX (LaTeX with LuaTeX) mailing list].&lt;br /&gt;
* [http://tex.stackexchange.com/ tex.stackexchange.com] is a q&amp;amp;a site for questions related to TeX, and you can ask (and answer) LuaTeX related questions there.&lt;br /&gt;
* The [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf online reference manual (pdf)] describes every aspect of LuaTeX, but is rather technical and concise. There also exists [[http://www.luatex.org/svn/trunk/manual/functionref.pdf|a short reference]].&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Writing Lua in TeX]] explains how to write Lua code in a TeX document (and back).&lt;br /&gt;
* [[Callbacks]] introduces callbacks and how to use them.&lt;br /&gt;
* [[Show the hyphenation points]] does what it says; it is also an example of use of the &amp;lt;tt&amp;gt;post_linebreak_filter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== From the old bluwiki.com ==&lt;br /&gt;
&lt;br /&gt;
(Mostly by and thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=79</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Main_Page&amp;diff=79"/>
		<updated>2011-05-07T14:12:01Z</updated>

		<summary type="html">&lt;p&gt;Paul: Changed a sentence.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Welcome to the LuaTeX wiki ==&lt;br /&gt;
&lt;br /&gt;
This is a wiki for [http://www.luatex.org LuaTeX], a typesetting engine derived from &lt;br /&gt;
[http://en.wikipedia.org/wiki/TeX TeX] that includes [http://www.lua.org Lua] as an embedded scripting language.&lt;br /&gt;
&lt;br /&gt;
See [[Special:Allpages|all the articles]] or the [[Special:Recentchanges|recent changes]].&lt;br /&gt;
&lt;br /&gt;
* [http://www.mediawiki.org/wiki/Help:Formatting How to edit wiki pages]&lt;br /&gt;
* [[Bug tracking|Bug tracking]]: Mantis [http://tracker.luatex.org bug tracker]&lt;br /&gt;
&lt;br /&gt;
== Getting help ==&lt;br /&gt;
&lt;br /&gt;
* There are two mailing lists related to LuaTeX, [http://tug.org/mailman/listinfo/luatex one for general discussions and questions about LuaTeX (&amp;quot;luatex users&amp;quot;)] and [http://www.ntg.nl/mailman/listinfo/dev-luatex one more related to the development of LuaTeX (&amp;quot;dev-luatex&amp;quot;)]&lt;br /&gt;
* There is also [http://www.tug.org/mailman/listinfo/lualatex-dev a LuaLaTeX (LaTeX with LuaTeX) mailing list].&lt;br /&gt;
* [http://tex.stackexchange.com/ tex.stackexchange.com] is a q&amp;amp;a site for questions related to TeX, and you can ask (and answer) LuaTeX related questions there.&lt;br /&gt;
* The [http://www.luatex.org/svn/trunk/manual/luatexref-t.pdf online reference manual (pdf)] describes every aspect of LuaTeX, but is rather technical and concise.&lt;br /&gt;
&lt;br /&gt;
== Some articles ==&lt;br /&gt;
&lt;br /&gt;
(Mostly from the old wiki at bluwiki.com -- thanks to [http://omega.enstb.org/yannis/ Yannis Haralambous])&lt;br /&gt;
&lt;br /&gt;
* An example of code [[traversing TeX nodes]] before an horizontal list goes through the line breaking engine;&lt;br /&gt;
* An example of code [[traversing tokens]] just before execution or expansion;&lt;br /&gt;
* you want to [[explore the table obtained from a TrueType font]], loaded by &amp;lt;tt&amp;gt;font.read_ttf&amp;lt;/tt&amp;gt;;&lt;br /&gt;
* you want to [[explore the internal font table]] of a pre-loaded font or of a font you have loaded by &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; and then used for at least one glyph;&lt;br /&gt;
* how to [[use a TrueType font]] without going through a TFM or a OFM file;&lt;br /&gt;
* how to do &amp;lt;i&amp;gt;kinsoku&amp;lt;/i&amp;gt; ([[Japanese and more generally CJK typesetting]]);&lt;br /&gt;
* you want a newline in your log file or on the terminal? add &amp;lt;tt&amp;gt;\string\n&amp;lt;/tt&amp;gt; to your string;&lt;br /&gt;
* you want to [[sort a token list]];&lt;br /&gt;
* you want to [[split a comma-separated list]];&lt;br /&gt;
* you want to [[encrypt your document using ROT13]];&lt;br /&gt;
* you want to [[typeset non-TeX files by converting them using Lua code]];&lt;br /&gt;
* example code to [[mirror characters with Bidi_Mirrored property]];&lt;br /&gt;
* using mplib to write [[metapost with LuaTeX]]&lt;br /&gt;
* [[show the hyphenation points]]&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=78</id>
		<title>Callbacks</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=78"/>
		<updated>2011-05-07T14:08:34Z</updated>

		<summary type="html">&lt;p&gt;Paul: Corrected description of mlist_to_hlist.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Callbacks allow the user to interact with TeX&amp;#039;s internal operations; those can be enhanced by additional ones, but also replaced altogether. All callbacks are written in Lua.&lt;br /&gt;
&lt;br /&gt;
Since functions registered in callbacks occur in a sequence of actions that makes up TeX&amp;#039;s processing, many of them will receive arguments (and thus should be defined to handle them) and, most importantly, many are supposed to return values. What arguments are passed, if any, and what should be return, vary from callback to callback.&lt;br /&gt;
&lt;br /&gt;
= Registering a callback =&lt;br /&gt;
&lt;br /&gt;
To install a function in a callback one must use the &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; function, whose syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(callback_name, function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;callback_name&amp;lt;/tt&amp;gt; is a string representing a predefined callback name (see below), and &amp;lt;tt&amp;gt;function&amp;lt;/tt&amp;gt; is either a function variable or an anonymous function. The former is just the name of a function previously defined, for instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local my_function = function (an_arg)&lt;br /&gt;
  do_this_or_that(an_arg)&lt;br /&gt;
end&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, my_function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An anonymous function is a function definition that is not assigned to a variable, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function (an_arg) do_this_or_that(an_arg) end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Even in the case of a function variable, what LuaTeX registers is the function itself, and any further assignment to the variable has no effect on the callback.&lt;br /&gt;
&lt;br /&gt;
If registering is successful, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; returns a number, which is the internal representation of the callback; if registering fails (for instance because a callback doesn&amp;#039;t exist, or what is registered isn&amp;#039;t a function), the function returns &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; and a string representing an error message.&lt;br /&gt;
&lt;br /&gt;
Besides functions, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; can take &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;. The first value clears the callback, and returns to the default behavior (i.e. either nothing or TeX&amp;#039;s default processing); the second value prevents anything from happening at this point, even TeX&amp;#039;s default operations, and should be used with great care (the benefits are minor speed gain).&lt;br /&gt;
&lt;br /&gt;
== Several functions in a callback ==&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; erases any function previously registered. I.e. after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_one)&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_two)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
only &amp;lt;tt&amp;gt;function_two&amp;lt;/tt&amp;gt; is registered in &amp;lt;tt&amp;gt;some_callback&amp;lt;/tt&amp;gt;. Hence, if one wants several successive functions to occur in a callback, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; is clearly insufficient. Instead, one must register a metafunction which calls the functions one after the other. This is taken care of in ConTeXt, and there exists &amp;lt;tt&amp;gt;luatexbase-mcb&amp;lt;/tt&amp;gt; for plain TeX and LaTeX, but here&amp;#039;s how to do it oneself. We&amp;#039;ll take the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback has an example; it is called whenever TeX reads an input line, it receives a string (the line) and should return one. A rough approach would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local function_table = { }&lt;br /&gt;
&lt;br /&gt;
local function metafunction (str)&lt;br /&gt;
  for _, fct in ipairs(function_table) do&lt;br /&gt;
    str = fct(str)&lt;br /&gt;
  end&lt;br /&gt;
  return str&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function add_function (fct)&lt;br /&gt;
  table.insert(function_table, fct)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, metafunction)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then one can call &amp;lt;tt&amp;gt;add_function(my_function)&amp;lt;/tt&amp;gt; to add &amp;lt;tt&amp;gt;my_function&amp;lt;/tt&amp;gt; to the list of functions registered in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;metafunction&amp;lt;/tt&amp;gt; will pass the callback&amp;#039;s argument (an input line) to the first function, set that argument to what the function returns, then repeat the process for all functions and finally return the resulting string.&lt;br /&gt;
&lt;br /&gt;
If one also wants to be able to remove functions (and not the entire metafunction), a subtler approach is required, where names are associated with functions and can be used as handles.&lt;br /&gt;
&lt;br /&gt;
= Information about callbacks =&lt;br /&gt;
&lt;br /&gt;
LuaTeX has two functions to get some information about callbacks. The first is &amp;lt;tt&amp;gt;callback.list()&amp;lt;/tt&amp;gt;, called without arguments and returning a table with the callback names as keys and booleans as values: if the value is &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;, it means that something is registered in the callback, or that it is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; (see above). The other function is &amp;lt;tt&amp;gt;callback.find()&amp;lt;/tt&amp;gt;, called with a callback name. If a function is registered, it is returned; if the callback is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, a boolean with that value is returned; finally, if nothing is registered, &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned (note that to distinguish between a boolean set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;, you must use &amp;lt;tt&amp;gt;type()&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
= List of callbacks =&lt;br /&gt;
&lt;br /&gt;
For suggestions on how to document callbacks, see [[Talk:Callbacks|the discussion page]].&lt;br /&gt;
&lt;br /&gt;
== File discovery callbacks ==&lt;br /&gt;
&lt;br /&gt;
These are called whenever an input file is needed. Each receives the &amp;lt;tt&amp;gt;asked_name&amp;lt;/tt&amp;gt;, a string representing what file name was called by the user (for instance, &amp;lt;tt&amp;gt;my_file.tex&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;\input my_file.tex&amp;lt;/tt&amp;gt;) and should return the name of a file to be read (or written to). The &amp;lt;tt&amp;gt;id_number&amp;lt;/tt&amp;gt; in the first two callbacks is the stream&amp;#039;s number plus one, because 0 denotes &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; files.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[find_read_file]] ||id_number, asked_name ||actual_name ||File called with &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_write_file]] ||id_number, asked_name ||actual_name ||&amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file or file called with &amp;lt;tt&amp;gt;\write&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_font_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_output_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_format_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_vf_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_map_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_enc_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_sfd_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_pk_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_data_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_opentype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_truetype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_type_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_image_file]] ||asked_name ||actual_name&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File reading callbacks ==&lt;br /&gt;
&lt;br /&gt;
The previous callbacks were called when a file was asked for; the following are triggered when the file is read. In all, &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt; is the string returned by the file discovery callbacks. The first callback reads &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; files; it must return a table, whose first cell is a function that will be called whenever a line from the input file is readed; the second cell is an optional function executed when the input file terminates.&lt;br /&gt;
&lt;br /&gt;
The other callbacks are used for binary files: &amp;lt;tt&amp;gt;success&amp;lt;/tt&amp;gt; is a boolean, &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; is a string and &amp;lt;tt&amp;gt;data_size&amp;lt;/tt&amp;gt; is a number (the length of &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; in bytes).&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[open_read_file]] ||filename ||{reader, close}&lt;br /&gt;
|-&lt;br /&gt;
|[[read_font_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_vf_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_map_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_enc_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_sfd_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_pk_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_data_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_truetype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_type1_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_opentype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Data processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[process_input_buffer]] ||line ||modified_line ||Called for each input line (after &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;open_read_file&amp;lt;/tt&amp;gt; if TeX is not reading the main file).&lt;br /&gt;
|-&lt;br /&gt;
|[[process_output_buffer]] ||line ||modified_line ||Called for each line TeX writes to an external file; this excludes the &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file, the terminal and &amp;lt;tt&amp;gt;\write18&amp;lt;/tt&amp;gt; calls.&lt;br /&gt;
|-&lt;br /&gt;
|[[token_filter]] || ||token ||Called when TeX needs token; no argument is passed to the callback, the next token must be fetched with &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt;; what is returned is immediately processed by TeX.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Node list processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks deal with node manipulations and occur at various stages of TeX&amp;#039;s typesetting processes.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[buildpage_filter]] ||information || ||Called when TeX moves material to the main vertical list. (This callback is an endangered species.)&lt;br /&gt;
|-&lt;br /&gt;
|[[hyphenate]] ||head, tail || || Inserts discretionary nodes in the node list; called in restricted and unrestricted horizontal mode (i.e. &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;&amp;#039;es and paragraph building). A function doing what TeX does here by default is &amp;lt;tt&amp;gt;lang.hyphenate()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[ligaturing]] ||head, tail || || Same as &amp;lt;tt&amp;gt;hyphenate&amp;lt;/tt&amp;gt;, but applying ligatures (and adding some discretionaries too). An associated function is &amp;lt;tt&amp;gt;node.ligaturing()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[kerning]] ||head, tail || || Same as above, with font kerns. The associated function is &amp;lt;tt&amp;gt;node.kerning()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_linebreak_filter]] ||head, context ||boolean or newhead ||Called in unrestricted horizontal mode, after &amp;lt;tt&amp;gt;kerning&amp;lt;/tt&amp;gt;, and before calling the paragraph builder; it receives the list of nodes that is the contents of the paragraph (plus the &amp;lt;tt&amp;gt;\parfillskip&amp;lt;/tt&amp;gt; glue) and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[linebreak_filter]] ||head, is_display ||newhead ||The paragraph builder. It receives what the previous callback has returned and is in charge of making a paragraph out of it; it must return what it has built. An associated function is &amp;lt;tt&amp;gt;tex.linebreak()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[Show the hyphenation points|post_linebreak_filter]] ||head, groupcode, ||boolean or newhead ||Receives what has been returned by &amp;lt;tt&amp;gt;linebreak_filter&amp;lt;/tt&amp;gt; and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[hpack_filter]] ||head, context, size, packtype[, dir] ||boolean or newhead ||Called when TeX is going to build an &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[vpack_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Called when TeX is going to build a &amp;lt;tt&amp;gt;\vbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_output_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Same as &amp;lt;tt&amp;gt;vpack_filter&amp;lt;/tt&amp;gt;, but when TeX packs box 255 (or any other value of &amp;lt;tt&amp;gt;\outputbox&amp;lt;/tt&amp;gt;) and goes to the output routine.&lt;br /&gt;
|-&lt;br /&gt;
|[[mlist_to_hlist]] ||head, display_type, need_penalties ||newhead ||Called when TeX turns a math list into a horizontal list (as described in Appendix G of the &amp;lt;em&amp;gt;TeXbook&amp;lt;/em&amp;gt;).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Information reporting callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks are called in various places when TeX has something to say. None of them takes an argument nor returns anything.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_dump]] ||Called before dumping a format.&lt;br /&gt;
|-&lt;br /&gt;
|[[start_run]] ||LuaTeX&amp;#039;s banner (`This is LuaTeX...&amp;#039;); must be changed in the initialization script, otherwise the banner has already been printed.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_run]] ||TeX&amp;#039;s final words (statistics and `Output written to...&amp;#039;).&lt;br /&gt;
|-&lt;br /&gt;
|[[start_page_number]] ||The opening bracket and the page number printed when a page is shipped out.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_page_number]] ||The closing bracket printed after the previous callback.&lt;br /&gt;
|-&lt;br /&gt;
|[[show_error_hook]] || Called at the beginning of each error message.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
||[[finish_pdffile]] || || ||Called when all pages have been written to the PDF file.&lt;br /&gt;
|-&lt;br /&gt;
|[[define_font]] ||name, size, id ||font_table ||Called when the &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; command is executed; &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; is the filename asked for, &amp;lt;tt&amp;gt;size&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;at&amp;lt;/tt&amp;gt; size, &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; is the number that will be assigned as an internal representation to the font thus loaded. The callback should return a table representing a font.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=77</id>
		<title>Writing Lua in TeX</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=77"/>
		<updated>2011-05-06T10:37:12Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added another version of \luacode with catcode-12 backslashes.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Embedding Lua code in a TeX document =&lt;br /&gt;
&lt;br /&gt;
Although it is simpler to put Lua code in Lua files, from time to time one may want or need to go Lua in the middle of a document. To this end, LuaTeX has two commands: &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;. They work the same, except &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt; is processed when the page where it appears is shipped out, whereas &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is processed at once; the distinction is immaterial here, and what is said of &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; also applies to &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; can be called in three ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua name {&amp;lt;name&amp;gt;} {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua &amp;lt;number&amp;gt; {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Those three ways are equivalent when it comes to process &amp;lt;tt&amp;gt;&amp;lt;lua code&amp;gt;&amp;lt;/tt&amp;gt;, but in the second case processing will occur in a chunk named &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, and in the third it will occur in a chunk whose name is the entry &amp;lt;tt&amp;gt;&amp;lt;number&amp;gt;&amp;lt;/tt&amp;gt; in the table &amp;lt;tt&amp;gt;lua.name&amp;lt;/tt&amp;gt;. The difference manifests itself only when errors occur, in which case the name of the chunk, if any, is reported.&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, named or not, is processed in a separate chunk. That means that any &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variable is defined for this call only and is lost afterward. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  one = 1&lt;br /&gt;
  local two = 2&lt;br /&gt;
}&lt;br /&gt;
\directlua{&lt;br /&gt;
  texio.write_nl(type(one))&lt;br /&gt;
  texio.write_nl(type(two))&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will report &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; (&amp;lt;tt&amp;gt;texio.write_nl&amp;lt;/tt&amp;gt; writes to the log file). On the other hand, Lua code is completely insensitive to TeX&amp;#039;s grouping mechanism. In other words, calling &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; between &amp;lt;tt&amp;gt;\bgroup&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\egroup&amp;lt;/tt&amp;gt; doesn&amp;#039;t affect the code to be processed.&lt;br /&gt;
&lt;br /&gt;
= TeX catcodes in Lua =&lt;br /&gt;
&lt;br /&gt;
By default, the code passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is treated as normal TeX input and only then sent to the Lua interpreter. This may lead to unwanted results and must be acknowledged.&lt;br /&gt;
&lt;br /&gt;
== Expansion ==&lt;br /&gt;
&lt;br /&gt;
As with any other special, the code in &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; (and the &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, if specifed) is fully expanded. This means that macros can be safely passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; if one wants their values, but that they should also be properly escaped when needed. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; as the number &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;. To store the control sequence &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; instead, another course of action is needed: see the [[#Backslash|section on backslash]] below.&lt;br /&gt;
&lt;br /&gt;
It should be noted that &amp;lt;tt&amp;gt;\par&amp;lt;/tt&amp;gt; tokens are removed from &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; if they are unexpandable (i.e., most likely, if they have their original meaning). Hence, empty lines can be used in Lua code.&lt;br /&gt;
&lt;br /&gt;
== Line ends ==&lt;br /&gt;
&lt;br /&gt;
When TeX reads a file, it normally turns line ends into spaces. That means that what looks like several lines is actually fed to the Lua interpreter as one big line. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
amounts to the following, if it were written in a separate Lua file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That is perfectly legitimate, but strange things might happen. First, TeX macros gobble spaces as usual. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be fed to the interpreter as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1anothervar = 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is not legitimate at all. Second, the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt; will affect everything to the end of the &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call. That is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
--  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 -- anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which works but only defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt;. Third, when reporting error, the Lua interpreter will always mention the line number as 1, since it processes one big line only; that isn&amp;#039;t extremely useful when the code is large.&lt;br /&gt;
&lt;br /&gt;
The solution is to set &amp;lt;tt&amp;gt;\endlinechar=10&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\catcode`\^^M=12&amp;lt;/tt&amp;gt;. In both cases, line ends will be preserved and the code will be processed as it is input.&lt;br /&gt;
&lt;br /&gt;
== Special characters ==&lt;br /&gt;
&lt;br /&gt;
In TeX, some characters have a special behavior. That must be taken into account when writing Lua code: one must change their catcodes beforehand if one wants to handle them as Lua would, as has just been done for line ends. That means that &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, as such, is clearly insufficient to write any extended chunk of code. It is thus better to devise a special macro that sets the catcodes to the appropriate values, reads the Lua code, feeds it to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, and restores the catcodes. The following code does the job:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\luacode{%&lt;br /&gt;
  \bgroup&lt;br /&gt;
  \catcode`\{=12&lt;br /&gt;
  \catcode`\}=12&lt;br /&gt;
  \catcode`\^^M=12&lt;br /&gt;
  \catcode`\#=12&lt;br /&gt;
  \catcode`\~=12&lt;br /&gt;
  \catcode`\%=12&lt;br /&gt;
  \doluacode&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
\bgroup&lt;br /&gt;
\catcode`\^^M=12 %&lt;br /&gt;
\long\gdef\doluacode#1^^M#2\endluacode{\directlua{#2}\egroup}%&lt;br /&gt;
\egroup&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that not all special characters are set to normal (catcode 12) characters; that is explained for each below. Note also that &amp;lt;tt&amp;gt;\doluacode&amp;lt;/tt&amp;gt;, internally called by &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt;, is defined to get rid of anything up to the line end, and then pass anything up to &amp;lt;tt&amp;gt;\endluacode&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;. Discarding what follows &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is important, otherwise a simple code as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = 1&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
would actually create two lines, the first being empty; it is annoying because errors are then reported with the wrong line number (i.e. any error in this one-line code would be reported to happen on line 2).&lt;br /&gt;
&lt;br /&gt;
However, the rest of the line after &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; could also be processed, instead of discarded, to manage special effects (e.g. specifying a chunk&amp;#039;s name, storing the code in a control sequence, or even setting which catcodes should be changed or not).&lt;br /&gt;
&lt;br /&gt;
=== Backslash ===&lt;br /&gt;
&lt;br /&gt;
The backslash in TeX is used to form control sequences. In the definition of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; above, it isn&amp;#039;t changed and thus behaves as usual. It allows commands to be passed and expanded to the Lua code. Anyway a backslash in Lua is also an escape character in strings. Hence, if one wants to store the name of a macro in Lua code, the following won&amp;#039;t work:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because to the Lua interpreter the string is made of &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; followed by &amp;lt;tt&amp;gt;acro&amp;lt;/tt&amp;gt;; since &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; is not defined in Lua, the string is read as &amp;lt;tt&amp;gt;macro&amp;lt;/tt&amp;gt;, but in other circumstances strange things might happen: for instance, &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; is a newline. The proper way to pass a macro verbatim is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which Lua will correctly read as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\\macro&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with the backslash escaped to represent itself. Another solution is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = [[\noexpand\macro]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because the double brackets signals a string in Lua where no escape sequence occurs (and the string can also run on several lines). Note however that in the second case &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; will be defined with a trailing space, i.e. as &amp;lt;tt&amp;gt;&amp;quot;\macro &amp;quot;&amp;lt;/tt&amp;gt;, because of TeX&amp;#039;s habit to append a trailing space to unexpanded (or unexpandable) control sequences.&lt;br /&gt;
&lt;br /&gt;
If one wants the backslash to be treated as any other character (not creating control sequences), then one has to rewrite &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\luacode{%&lt;br /&gt;
  \bgroup&lt;br /&gt;
  \catcode`\\=12&lt;br /&gt;
  \catcode`\{=12&lt;br /&gt;
  \catcode`\}=12&lt;br /&gt;
  \catcode`\^^M=12&lt;br /&gt;
  \catcode`\#=12&lt;br /&gt;
  \catcode`\~=12&lt;br /&gt;
  \catcode`\%=12&lt;br /&gt;
  \doluacode&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
\bgroup&lt;br /&gt;
\catcode`\|=0&lt;br /&gt;
\catcode`\^^M=12 %&lt;br /&gt;
\catcode`\\=12 %&lt;br /&gt;
|long|gdef|doluacode#1^^M#2\endluacode{|directlua{#2}|egroup}%&lt;br /&gt;
|egroup&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Backslashes remain escape characters in Lua, though.&lt;br /&gt;
&lt;br /&gt;
=== Braces ===&lt;br /&gt;
&lt;br /&gt;
One may want to define a string in Lua which contains unbalanced braces, i.e.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the braces&amp;#039; catcodes hadn&amp;#039;t been changed beforehand, that would be impossible. Note, however, that this means that one can&amp;#039;t feed arguments to commands in the usual way. I.e. the following will produce nothing good:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\dosomething{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\dosomething&amp;lt;/tt&amp;gt; will be expanded with the left brace (devoid of its usual delimiter-ness) as its argument, and the rest of the line might produce chaos. Thus, one may also choose not to change the catcodes of braces, depending on how &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is most likely to be used. Note that strings with unbalanced braces can still be defined, even if braces have their usual catcodes, thanks to the following trick:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot; -- }&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the code is passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, braces are balanced because the Lua comment means nothing to TeX; when passed to the Lua interpreter, on the other hand, the right brace is ignored.&lt;br /&gt;
&lt;br /&gt;
=== Hash and comment ===&lt;br /&gt;
&lt;br /&gt;
The hash sign &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; in Lua is the length operator: prefixed to a string or table variable, it returns its length. If its catcode weren&amp;#039;t taken care of, LuaTeX would pass to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; a double hash for each hash, i.e. each &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; would be turned into &amp;lt;tt&amp;gt;##&amp;lt;/tt&amp;gt;. That is normal TeX behavior, but unwanted here.&lt;br /&gt;
&lt;br /&gt;
As for the commen sign &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, it is useful in Lua when manipulating strings. If it weren&amp;#039;t escaped it would discard parts of the code when TeX reads it, and a mutilated version of the input would be passed to the Lua interpreter. In turn, discarding a line by commenting it in &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; should be done with the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Active characters ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt; character is generally active and used as a no-break space in TeX. It it were passed as is to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, it would expand to uninterpretable control sequences, whereas in Lua it is used to form the unequal operator &amp;lt;tt&amp;gt;~=&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Other possible active characters should be taken care of, but which characters are active is unpredictable; punctuation marks might be so to accommodate special spacing, as with LaTeX&amp;#039;s &amp;#039;&amp;#039;babel&amp;#039;&amp;#039; package, but such tricks are unlikely to survive in LuaTeX (cleaner methods exist that add a space before punctuation marks when necessary).&lt;br /&gt;
&lt;br /&gt;
=== Other characters ===&lt;br /&gt;
&lt;br /&gt;
When processing verbatim text in TeX, one generally also changes the catcodes of &amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;&amp;amp;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;^&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; and the space character, because they too are special. When passed to the Lua interpreter, though, their usual catcodes won&amp;#039;t do any harm, that is why they are left unmodified here.&lt;br /&gt;
&lt;br /&gt;
== \luaescapestring ==&lt;br /&gt;
&lt;br /&gt;
Although it can&amp;#039;t do all of what&amp;#039;s been explained, the &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; command might be useful in some cases: it expands its argument (which must be enclosed in &amp;#039;&amp;#039;real&amp;#039;&amp;#039; braces) fully, then modify it so that dangerous characters are escaped: backslashes, hashes, quotes and line ends. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{&amp;quot;\noexpand\foo&amp;quot;}&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\luaescapestring{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be passed to Lua as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\&amp;quot;\\foo \&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so that &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; is defined as &amp;lt;tt&amp;gt;&amp;quot;\foo &amp;quot;&amp;lt;/tt&amp;gt;, with the quotes as parts of it. Note that the trailing space after &amp;lt;tt&amp;gt;\foo&amp;lt;/tt&amp;gt; still happens.&lt;br /&gt;
&lt;br /&gt;
= From Lua to TeX =&lt;br /&gt;
&lt;br /&gt;
Inside Lua code, one can pass strings to be processed by TeX with the functions &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;tex.sprint()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;tex.tprint()&amp;lt;/tt&amp;gt;. All such calls are processed at the end of a &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call, even though they might happen in the middle of the code. This behavior is worth noting because it might be surprising in some cases, although it is generally harmless.&lt;br /&gt;
&lt;br /&gt;
== tex.print() ==&lt;br /&gt;
&lt;br /&gt;
This function receives as its argument(s) either one or more strings or an array of strings. Each string is processed as an input line: an end-of-line character is appended (except to the last string), and TeX is in state &amp;lt;tt&amp;gt;newline&amp;lt;/tt&amp;gt; when processing it (i.e. leading spaces are skipped). Hence the two equivalent calls:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.print(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
tex.print({&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
are both interpreted by TeX as would the following two lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a&lt;br /&gt;
b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus `a b&amp;#039; is produced, since line ends normally produce a space.&lt;br /&gt;
&lt;br /&gt;
The function can also take an optional number as its first argument; it is interpreted as referring to a catcode table (as defined by &amp;lt;tt&amp;gt;\initcatcodetable&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\savecatcodetable&amp;lt;/tt&amp;gt;), and each line is processed by TeX with that catcode regime. For instance (note that with such a minimal catcode table, braces don&amp;#039;t even have their usual values):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\bgroup&lt;br /&gt;
\initcatcodetable1&lt;br /&gt;
\catcode`\_=0&lt;br /&gt;
\savecatcodetable1&lt;br /&gt;
\egroup&lt;br /&gt;
&lt;br /&gt;
\directlua{tex.print(1, &amp;quot;_TeX&amp;quot;)}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The string will be read with &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; as an escape character, and thus interpreted as the command commonly known as &amp;lt;tt&amp;gt;\TeX&amp;lt;/tt&amp;gt;. The catcode regime holds only for the strings passed to &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt; and the rest of the document isn&amp;#039;t affected.&lt;br /&gt;
&lt;br /&gt;
If the optional number is &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt;, or points to an invalid (i.e. undefined) catcode table, then the strings are processed with the current catcodes, as if there was no optional argument. If it is &amp;lt;tt&amp;gt;-2&amp;lt;/tt&amp;gt;, then the strings are read as if the result of &amp;lt;tt&amp;gt;\detokenize&amp;lt;/tt&amp;gt;: all characters have catcode 12 (i.e. `other&amp;#039;, characters that have no function beside representing themselves), except space, which has catcode 10 (as usual).&lt;br /&gt;
&lt;br /&gt;
== tex.sprint() ==&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, this function can receive either one or more strings or an array of strings, with an optional number as its first argument pointing to a catcode table. Unlike &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, however, each string is processed as if TeX were in the middle of a line and not at the beginning of a new one: spaces aren&amp;#039;t skipped, no end-of-line character is added and trailing spaces aren&amp;#039;t ignored. Thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.sprint(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is interpreted by TeX as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ab&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
without any space inbetween.&lt;br /&gt;
&lt;br /&gt;
== tex.tprint() ==&lt;br /&gt;
&lt;br /&gt;
This function takes an unlimited number of tables as its arguments; each table must be an array of strings, with the first entry optionally being a number pointing to a catcode table. Then each table is processed as if passed to &amp;lt;tt&amp;gt;tex.sprint()&amp;lt;/tt&amp;gt;. Thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.tprint({1, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;}, {&amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is equivalent to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.sprint(1, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
tex.sprint(&amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= The expansion of \directlua =&lt;br /&gt;
&lt;br /&gt;
A call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is fully expandable; i.e. it can be used in contexts where full expansion is required, as in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\csname\directlua{tex.print(&amp;quot;TeX&amp;quot;)}\endcsname&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is a somewhat convoluted way of saying &amp;lt;tt&amp;gt;\TeX&amp;lt;/tt&amp;gt;. Besides, since Lua code is processed at once, things that were previously unthinkable can now be done easily. For instance, it is impossible to perform an assignment in an &amp;lt;tt&amp;gt;\edef&amp;lt;/tt&amp;gt; by TeX&amp;#039;s traditional means. I.e. the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\edef\macro{\count1=5}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; as &amp;lt;tt&amp;gt;\count1=5&amp;lt;/tt&amp;gt; but doesn&amp;#039;t perform the assignment (the &amp;lt;tt&amp;gt;\edef&amp;lt;/tt&amp;gt; does nothing more than a simple &amp;lt;tt&amp;gt;\def&amp;lt;/tt&amp;gt;). After the definition, the value of &amp;lt;tt&amp;gt;\count1&amp;lt;/tt&amp;gt; hasn&amp;#039;t changed. The same is not true, though, if such an assigment is made with Lua code. The following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\edef\macro{\directlua{tex.count[1] = 5}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; emptily (since nothing remains after &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; has been processed) &amp;#039;&amp;#039;and&amp;#039;&amp;#039; sets count 1 to 5. Since such a behavior is totally unexpected in normal TeX, one should be wary when using &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; in such contexts.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=64</id>
		<title>Callbacks</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=64"/>
		<updated>2011-01-17T17:16:28Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added reference to discussion page.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Callbacks allow the user to interact with TeX&amp;#039;s internal operations; those can be enhanced by additional ones, but also replaced altogether. All callbacks are written in Lua.&lt;br /&gt;
&lt;br /&gt;
Since functions registered in callbacks occur in a sequence of actions that makes up TeX&amp;#039;s processing, many of them will receive arguments (and thus should be defined to handle them) and, most importantly, many are supposed to return values. What arguments are passed, if any, and what should be return, vary from callback to callback.&lt;br /&gt;
&lt;br /&gt;
= Registering a callback =&lt;br /&gt;
&lt;br /&gt;
To install a function in a callback one must use the &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; function, whose syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(callback_name, function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;callback_name&amp;lt;/tt&amp;gt; is a string representing a predefined callback name (see below), and &amp;lt;tt&amp;gt;function&amp;lt;/tt&amp;gt; is either a function variable or an anonymous function. The former is just the name of a function previously defined, for instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local my_function = function (an_arg)&lt;br /&gt;
  do_this_or_that(an_arg)&lt;br /&gt;
end&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, my_function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An anonymous function is a function definition that is not assigned to a variable, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function (an_arg) do_this_or_that(an_arg) end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Even in the case of a function variable, what LuaTeX registers is the function itself, and any further assignment to the variable has no effect on the callback.&lt;br /&gt;
&lt;br /&gt;
If registering is successful, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; returns a number, which is the internal representation of the callback; if registering fails (for instance because a callback doesn&amp;#039;t exist, or what is registered isn&amp;#039;t a function), the function returns &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; and a string representing an error message.&lt;br /&gt;
&lt;br /&gt;
Besides functions, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; can take &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;. The first value clears the callback, and returns to the default behavior (i.e. either nothing or TeX&amp;#039;s default processing); the second value prevents anything from happening at this point, even TeX&amp;#039;s default operations, and should be used with great care (the benefits are minor speed gain).&lt;br /&gt;
&lt;br /&gt;
== Several functions in a callback ==&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; erases any function previously registered. I.e. after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_one)&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_two)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
only &amp;lt;tt&amp;gt;function_two&amp;lt;/tt&amp;gt; is registered in &amp;lt;tt&amp;gt;some_callback&amp;lt;/tt&amp;gt;. Hence, if one wants several successive functions to occur in a callback, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; is clearly insufficient. Instead, one must register a metafunction which calls the functions one after the other. This is taken care of in ConTeXt, and there exists &amp;lt;tt&amp;gt;luatexbase-mcb&amp;lt;/tt&amp;gt; for plain TeX and LaTeX, but here&amp;#039;s how to do it oneself. We&amp;#039;ll take the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback has an example; it is called whenever TeX reads an input line, it receives a string (the line) and should return one. A rough approach would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local function_table = { }&lt;br /&gt;
&lt;br /&gt;
local function metafunction (str)&lt;br /&gt;
  for _, fct in ipairs(function_table) do&lt;br /&gt;
    str = fct(str)&lt;br /&gt;
  end&lt;br /&gt;
  return str&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function add_function (fct)&lt;br /&gt;
  table.insert(function_table, fct)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, metafunction)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then one can call &amp;lt;tt&amp;gt;add_function(my_function)&amp;lt;/tt&amp;gt; to add &amp;lt;tt&amp;gt;my_function&amp;lt;/tt&amp;gt; to the list of functions registered in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;metafunction&amp;lt;/tt&amp;gt; will pass the callback&amp;#039;s argument (an input line) to the first function, set that argument to what the function returns, then repeat the process for all functions and finally return the resulting string.&lt;br /&gt;
&lt;br /&gt;
If one also wants to be able to remove functions (and not the entire metafunction), a subtler approach is required, where names are associated with functions and can be used as handles.&lt;br /&gt;
&lt;br /&gt;
= Information about callbacks =&lt;br /&gt;
&lt;br /&gt;
LuaTeX has two functions to get some information about callbacks. The first is &amp;lt;tt&amp;gt;callback.list()&amp;lt;/tt&amp;gt;, called without arguments and returning a table with the callback names as keys and booleans as values: if the value is &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;, it means that something is registered in the callback, or that it is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; (see above). The other function is &amp;lt;tt&amp;gt;callback.find()&amp;lt;/tt&amp;gt;, called with a callback name. If a function is registered, it is returned; if the callback is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, a boolean with that value is returned; finally, if nothing is registered, &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned (note that to distinguish between a boolean set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;, you must use &amp;lt;tt&amp;gt;type()&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
= List of callbacks =&lt;br /&gt;
&lt;br /&gt;
For suggestions on how to document callbacks, see [[Talk:Callbacks|the discussion page]].&lt;br /&gt;
&lt;br /&gt;
== File discovery callbacks ==&lt;br /&gt;
&lt;br /&gt;
These are called whenever an input file is needed. Each receives the &amp;lt;tt&amp;gt;asked_name&amp;lt;/tt&amp;gt;, a string representing what file name was called by the user (for instance, &amp;lt;tt&amp;gt;my_file.tex&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;\input my_file.tex&amp;lt;/tt&amp;gt;) and should return the name of a file to be read (or written to). The &amp;lt;tt&amp;gt;id_number&amp;lt;/tt&amp;gt; in the first two callbacks is the stream&amp;#039;s number plus one, because 0 denotes &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; files.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[find_read_file]] ||id_number, asked_name ||actual_name ||File called with &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_write_file]] ||id_number, asked_name ||actual_name ||&amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file or file called with &amp;lt;tt&amp;gt;\write&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_font_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_output_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_format_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_vf_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_map_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_enc_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_sfd_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_pk_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_data_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_opentype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_truetype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_type_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_image_file]] ||asked_name ||actual_name&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File reading callbacks ==&lt;br /&gt;
&lt;br /&gt;
The previous callbacks were called when a file was asked for; the following are triggered when the file is read. In all, &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt; is the string returned by the file discovery callbacks. The first callback reads &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; files; it must return a table, whose first cell is a function that will be called whenever a line from the input file is readed; the second cell is an optional function executed when the input file terminates.&lt;br /&gt;
&lt;br /&gt;
The other callbacks are used for binary files: &amp;lt;tt&amp;gt;success&amp;lt;/tt&amp;gt; is a boolean, &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; is a string and &amp;lt;tt&amp;gt;data_size&amp;lt;/tt&amp;gt; is a number (the length of &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; in bytes).&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[open_read_file]] ||filename ||{reader, close}&lt;br /&gt;
|-&lt;br /&gt;
|[[read_font_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_vf_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_map_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_enc_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_sfd_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_pk_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_data_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_truetype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_type1_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_opentype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Data processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[process_input_buffer]] ||line ||modified_line ||Called for each input line (after &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;open_read_file&amp;lt;/tt&amp;gt; if TeX is not reading the main file).&lt;br /&gt;
|-&lt;br /&gt;
|[[process_output_buffer]] ||line ||modified_line ||Called for each line TeX writes to an external file; this excludes the &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file, the terminal and &amp;lt;tt&amp;gt;\write18&amp;lt;/tt&amp;gt; calls.&lt;br /&gt;
|-&lt;br /&gt;
|[[token_filter]] || ||token ||Called when TeX needs token; no argument is passed to the callback, the next token must be fetched with &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt;; what is returned is immediately processed by TeX.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Node list processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks deal with node manipulations and occur at various stages of TeX&amp;#039;s typesetting processes.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[buildpage_filter]] ||information || ||Called when TeX moves material to the main vertical list. (This callback is an endangered species.)&lt;br /&gt;
|-&lt;br /&gt;
|[[hyphenate]] ||head, tail || || Inserts discretionary nodes in the node list; called in restricted and unrestricted horizontal mode (i.e. &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;&amp;#039;es and paragraph building). A function doing what TeX does here by default is &amp;lt;tt&amp;gt;lang.hyphenate()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[ligaturing]] ||head, tail || || Same as &amp;lt;tt&amp;gt;hyphenate&amp;lt;/tt&amp;gt;, but applying ligatures (and adding some discretionaries too). An associated function is &amp;lt;tt&amp;gt;node.ligaturing()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[kerning]] ||head, tail || || Same as above, with font kerns. The associated function is &amp;lt;tt&amp;gt;node.kerning()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_linebreak_filter]] ||head, context ||boolean or newhead ||Called in unrestricted horizontal mode, after &amp;lt;tt&amp;gt;kerning&amp;lt;/tt&amp;gt;, and before calling the paragraph builder; it receives the list of nodes that is the contents of the paragraph (plus the &amp;lt;tt&amp;gt;\parfillskip&amp;lt;/tt&amp;gt; glue) and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[linebreak_filter]] ||head, is_display ||newhead ||The paragraph builder. It receives what the previous callback has returned and is in charge of making a paragraph out of it; it must return what it has built. An associated function is &amp;lt;tt&amp;gt;tex.linebreak()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[Show the hyphenation points|post_linebreak_filter]] ||head, groupcode, ||boolean or newhead ||Receives what has been returned by &amp;lt;tt&amp;gt;linebreak_filter&amp;lt;/tt&amp;gt; and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[hpack_filter]] ||head, context, size, packtype[, dir] ||boolean or newhead ||Called when TeX is going to build an &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[vpack_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Called when TeX is going to build a &amp;lt;tt&amp;gt;\vbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_output_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Same as &amp;lt;tt&amp;gt;vpack_filter&amp;lt;/tt&amp;gt;, but when TeX packs box 255 (or any other value of &amp;lt;tt&amp;gt;\outputbox&amp;lt;/tt&amp;gt;) and goes to the output routine.&lt;br /&gt;
|-&lt;br /&gt;
|[[mlist_to_hlist]] ||head, display_type, need_penalties ||newhead ||Called when TeX turns a math list into a node list.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Information reporting callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks are called in various places when TeX has something to say. None of them takes an argument nor returns anything.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_dump]] ||Called before dumping a format.&lt;br /&gt;
|-&lt;br /&gt;
|[[start_run]] ||LuaTeX&amp;#039;s banner (`This is LuaTeX...&amp;#039;); must be changed in the initialization script, otherwise the banner has already been printed.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_run]] ||TeX&amp;#039;s final words (statistics and `Output written to...&amp;#039;).&lt;br /&gt;
|-&lt;br /&gt;
|[[start_page_number]] ||The opening bracket and the page number printed when a page is shipped out.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_page_number]] ||The closing bracket printed after the previous callback.&lt;br /&gt;
|-&lt;br /&gt;
|[[show_error_hook]] || Called at the beginning of each error message.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
||[[finish_pdffile]] || || ||Called when all pages have been written to the PDF file.&lt;br /&gt;
|-&lt;br /&gt;
|[[define_font]] ||name, size, id ||font_table ||Called when the &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; command is executed; &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; is the filename asked for, &amp;lt;tt&amp;gt;size&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;at&amp;lt;/tt&amp;gt; size, &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; is the number that will be assigned as an internal representation to the font thus loaded. The callback should return a table representing a font.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Talk:Callbacks&amp;diff=63</id>
		<title>Talk:Callbacks</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Talk:Callbacks&amp;diff=63"/>
		<updated>2011-01-17T17:09:56Z</updated>

		<summary type="html">&lt;p&gt;Paul: Proposed guidelines for articles on callbacks.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suggested guidelines to document callbacks:&lt;br /&gt;
&lt;br /&gt;
* Have an article for each callback, with a minimal example.&lt;br /&gt;
&lt;br /&gt;
* Let a page&amp;#039;s title be the name of the callback it describes, so it is automatically linked to this page, meant as a general introduction. If the page title differs, change the link here from &amp;lt;pre&amp;gt;[[callback_name]]&amp;lt;/pre&amp;gt; to &amp;lt;pre&amp;gt;[[page_title|callback_name]]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Link all pages on callbacks to this general introduction, which should gather general information about callbacks (e.g. no need to reexplain &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; on each page).&lt;br /&gt;
&lt;br /&gt;
* Make code low-level, which means: no format-specific tricks, and nothing but &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; (we can redirect to the general introduction for discussion about the limitations of &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt;). Basically, this means avoiding things like &amp;lt;tt&amp;gt;\documentclass{article}&amp;lt;/tt&amp;gt; or even plain TeX&amp;#039;s &amp;lt;tt&amp;gt;\bye&amp;lt;/tt&amp;gt;. The code should be pure Lua and pure TeX (probably we can allow allocation macros like \newif and \newbox, though, since they are common to all formats), so the reader can simply paste it and process it as s/he prefers.&lt;br /&gt;
&lt;br /&gt;
* Commented code is better than pure code, although pure code is better than no code.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=62</id>
		<title>Callbacks</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Callbacks&amp;diff=62"/>
		<updated>2011-01-17T12:13:37Z</updated>

		<summary type="html">&lt;p&gt;Paul: Introduction to callbacks and list of callbacks.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Callbacks allow the user to interact with TeX&amp;#039;s internal operations; those can be enhanced by additional ones, but also replaced altogether. All callbacks are written in Lua.&lt;br /&gt;
&lt;br /&gt;
Since functions registered in callbacks occur in a sequence of actions that makes up TeX&amp;#039;s processing, many of them will receive arguments (and thus should be defined to handle them) and, most importantly, many are supposed to return values. What arguments are passed, if any, and what should be return, vary from callback to callback.&lt;br /&gt;
&lt;br /&gt;
= Registering a callback =&lt;br /&gt;
&lt;br /&gt;
To install a function in a callback one must use the &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; function, whose syntax is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(callback_name, function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;callback_name&amp;lt;/tt&amp;gt; is a string representing a predefined callback name (see below), and &amp;lt;tt&amp;gt;function&amp;lt;/tt&amp;gt; is either a function variable or an anonymous function. The former is just the name of a function previously defined, for instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local my_function = function (an_arg)&lt;br /&gt;
  do_this_or_that(an_arg)&lt;br /&gt;
end&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, my_function)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An anonymous function is a function definition that is not assigned to a variable, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function (an_arg) do_this_or_that(an_arg) end)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Even in the case of a function variable, what LuaTeX registers is the function itself, and any further assignment to the variable has no effect on the callback.&lt;br /&gt;
&lt;br /&gt;
If registering is successful, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; returns a number, which is the internal representation of the callback; if registering fails (for instance because a callback doesn&amp;#039;t exist, or what is registered isn&amp;#039;t a function), the function returns &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; and a string representing an error message.&lt;br /&gt;
&lt;br /&gt;
Besides functions, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; can take &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;. The first value clears the callback, and returns to the default behavior (i.e. either nothing or TeX&amp;#039;s default processing); the second value prevents anything from happening at this point, even TeX&amp;#039;s default operations, and should be used with great care (the benefits are minor speed gain).&lt;br /&gt;
&lt;br /&gt;
== Several functions in a callback ==&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; erases any function previously registered. I.e. after&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_one)&lt;br /&gt;
callback.register(&amp;quot;some_callback&amp;quot;, function_two)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
only &amp;lt;tt&amp;gt;function_two&amp;lt;/tt&amp;gt; is registered in &amp;lt;tt&amp;gt;some_callback&amp;lt;/tt&amp;gt;. Hence, if one wants several successive functions to occur in a callback, &amp;lt;tt&amp;gt;callback.register()&amp;lt;/tt&amp;gt; is clearly insufficient. Instead, one must register a metafunction which calls the functions one after the other. This is taken care of in ConTeXt, and there exists &amp;lt;tt&amp;gt;luatexbase-mcb&amp;lt;/tt&amp;gt; for plain TeX and LaTeX, but here&amp;#039;s how to do it oneself. We&amp;#039;ll take the &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt; callback has an example; it is called whenever TeX reads an input line, it receives a string (the line) and should return one. A rough approach would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local function_table = { }&lt;br /&gt;
&lt;br /&gt;
local function metafunction (str)&lt;br /&gt;
  for _, fct in ipairs(function_table) do&lt;br /&gt;
    str = fct(str)&lt;br /&gt;
  end&lt;br /&gt;
  return str&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function add_function (fct)&lt;br /&gt;
  table.insert(function_table, fct)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register(&amp;quot;process_input_buffer&amp;quot;, metafunction)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then one can call &amp;lt;tt&amp;gt;add_function(my_function)&amp;lt;/tt&amp;gt; to add &amp;lt;tt&amp;gt;my_function&amp;lt;/tt&amp;gt; to the list of functions registered in &amp;lt;tt&amp;gt;process_input_buffer&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;metafunction&amp;lt;/tt&amp;gt; will pass the callback&amp;#039;s argument (an input line) to the first function, set that argument to what the function returns, then repeat the process for all functions and finally return the resulting string.&lt;br /&gt;
&lt;br /&gt;
If one also wants to be able to remove functions (and not the entire metafunction), a subtler approach is required, where names are associated with functions and can be used as handles.&lt;br /&gt;
&lt;br /&gt;
= Information about callbacks =&lt;br /&gt;
&lt;br /&gt;
LuaTeX has two functions to get some information about callbacks. The first is &amp;lt;tt&amp;gt;callback.list()&amp;lt;/tt&amp;gt;, called without arguments and returning a table with the callback names as keys and booleans as values: if the value is &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt;, it means that something is registered in the callback, or that it is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; (see above). The other function is &amp;lt;tt&amp;gt;callback.find()&amp;lt;/tt&amp;gt;, called with a callback name. If a function is registered, it is returned; if the callback is set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;, a boolean with that value is returned; finally, if nothing is registered, &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; is returned (note that to distinguish between a boolean set to &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt;, you must use &amp;lt;tt&amp;gt;type()&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
= List of callbacks =&lt;br /&gt;
&lt;br /&gt;
== File discovery callbacks ==&lt;br /&gt;
&lt;br /&gt;
These are called whenever an input file is needed. Each receives the &amp;lt;tt&amp;gt;asked_name&amp;lt;/tt&amp;gt;, a string representing what file name was called by the user (for instance, &amp;lt;tt&amp;gt;my_file.tex&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;\input my_file.tex&amp;lt;/tt&amp;gt;) and should return the name of a file to be read (or written to). The &amp;lt;tt&amp;gt;id_number&amp;lt;/tt&amp;gt; in the first two callbacks is the stream&amp;#039;s number plus one, because 0 denotes &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; files.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[find_read_file]] ||id_number, asked_name ||actual_name ||File called with &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_write_file]] ||id_number, asked_name ||actual_name ||&amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file or file called with &amp;lt;tt&amp;gt;\write&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|[[find_font_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_output_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_format_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_vf_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_map_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_enc_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_sfd_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_pk_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_data_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_opentype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_truetype_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_type_file]] ||asked_name ||actual_name&lt;br /&gt;
|-&lt;br /&gt;
|[[find_image_file]] ||asked_name ||actual_name&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File reading callbacks ==&lt;br /&gt;
&lt;br /&gt;
The previous callbacks were called when a file was asked for; the following are triggered when the file is read. In all, &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt; is the string returned by the file discovery callbacks. The first callback reads &amp;lt;tt&amp;gt;\input&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\read&amp;lt;/tt&amp;gt; files; it must return a table, whose first cell is a function that will be called whenever a line from the input file is readed; the second cell is an optional function executed when the input file terminates.&lt;br /&gt;
&lt;br /&gt;
The other callbacks are used for binary files: &amp;lt;tt&amp;gt;success&amp;lt;/tt&amp;gt; is a boolean, &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; is a string and &amp;lt;tt&amp;gt;data_size&amp;lt;/tt&amp;gt; is a number (the length of &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; in bytes).&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[open_read_file]] ||filename ||{reader, close}&lt;br /&gt;
|-&lt;br /&gt;
|[[read_font_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_vf_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_map_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_enc_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_sfd_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_pk_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_data_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_truetype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_type1_file]] ||filename ||success, data, data_size&lt;br /&gt;
|-&lt;br /&gt;
|[[read_opentype_file]] ||filename ||success, data, data_size&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Data processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[process_input_buffer]] ||line ||modified_line ||Called for each input line (after &amp;lt;tt&amp;gt;reader&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;open_read_file&amp;lt;/tt&amp;gt; if TeX is not reading the main file).&lt;br /&gt;
|-&lt;br /&gt;
|[[process_output_buffer]] ||line ||modified_line ||Called for each line TeX writes to an external file; this excludes the &amp;lt;tt&amp;gt;log&amp;lt;/tt&amp;gt; file, the terminal and &amp;lt;tt&amp;gt;\write18&amp;lt;/tt&amp;gt; calls.&lt;br /&gt;
|-&lt;br /&gt;
|[[token_filter]] || ||token ||Called when TeX needs token; no argument is passed to the callback, the next token must be fetched with &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt;; what is returned is immediately processed by TeX.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Node list processing callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks deal with node manipulations and occur at various stages of TeX&amp;#039;s typesetting processes.&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[buildpage_filter]] ||information || ||Called when TeX moves material to the main vertical list. (This callback is an endangered species.)&lt;br /&gt;
|-&lt;br /&gt;
|[[hyphenate]] ||head, tail || || Inserts discretionary nodes in the node list; called in restricted and unrestricted horizontal mode (i.e. &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;&amp;#039;es and paragraph building). A function doing what TeX does here by default is &amp;lt;tt&amp;gt;lang.hyphenate()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[ligaturing]] ||head, tail || || Same as &amp;lt;tt&amp;gt;hyphenate&amp;lt;/tt&amp;gt;, but applying ligatures (and adding some discretionaries too). An associated function is &amp;lt;tt&amp;gt;node.ligaturing()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[kerning]] ||head, tail || || Same as above, with font kerns. The associated function is &amp;lt;tt&amp;gt;node.kerning()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_linebreak_filter]] ||head, context ||boolean or newhead ||Called in unrestricted horizontal mode, after &amp;lt;tt&amp;gt;kerning&amp;lt;/tt&amp;gt;, and before calling the paragraph builder; it receives the list of nodes that is the contents of the paragraph (plus the &amp;lt;tt&amp;gt;\parfillskip&amp;lt;/tt&amp;gt; glue) and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[linebreak_filter]] ||head, is_display ||newhead ||The paragraph builder. It receives what the previous callback has returned and is in charge of making a paragraph out of it; it must return what it has built. An associated function is &amp;lt;tt&amp;gt;tex.linebreak()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[Show the hyphenation points|post_linebreak_filter]] ||head, groupcode, ||boolean or newhead ||Receives what has been returned by &amp;lt;tt&amp;gt;linebreak_filter&amp;lt;/tt&amp;gt; and must return something similar.&lt;br /&gt;
|-&lt;br /&gt;
|[[hpack_filter]] ||head, context, size, packtype[, dir] ||boolean or newhead ||Called when TeX is going to build an &amp;lt;tt&amp;gt;\hbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[vpack_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Called when TeX is going to build a &amp;lt;tt&amp;gt;\vbox&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_output_filter]] ||head, context, size, packtype, maxdepth[, dir] ||boolean or newhead ||Same as &amp;lt;tt&amp;gt;vpack_filter&amp;lt;/tt&amp;gt;, but when TeX packs box 255 (or any other value of &amp;lt;tt&amp;gt;\outputbox&amp;lt;/tt&amp;gt;) and goes to the output routine.&lt;br /&gt;
|-&lt;br /&gt;
|[[mlist_to_hlist]] ||head, display_type, need_penalties ||newhead ||Called when TeX turns a math list into a node list.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Information reporting callbacks ==&lt;br /&gt;
&lt;br /&gt;
These callbacks are called in various places when TeX has something to say. None of them takes an argument nor returns anything.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Short description&lt;br /&gt;
|-&lt;br /&gt;
|[[pre_dump]] ||Called before dumping a format.&lt;br /&gt;
|-&lt;br /&gt;
|[[start_run]] ||LuaTeX&amp;#039;s banner (`This is LuaTeX...&amp;#039;); must be changed in the initialization script, otherwise the banner has already been printed.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_run]] ||TeX&amp;#039;s final words (statistics and `Output written to...&amp;#039;).&lt;br /&gt;
|-&lt;br /&gt;
|[[start_page_number]] ||The opening bracket and the page number printed when a page is shipped out.&lt;br /&gt;
|-&lt;br /&gt;
|[[stop_page_number]] ||The closing bracket printed after the previous callback.&lt;br /&gt;
|-&lt;br /&gt;
|[[show_error_hook]] || Called at the beginning of each error message.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Miscellaneous ==&lt;br /&gt;
{| cellpadding=&amp;quot;5&amp;quot;&lt;br /&gt;
!Name !! Argument(s) !! Return value(s) !! Short description&lt;br /&gt;
|-&lt;br /&gt;
||[[finish_pdffile]] || || ||Called when all pages have been written to the PDF file.&lt;br /&gt;
|-&lt;br /&gt;
|[[define_font]] ||name, size, id ||font_table ||Called when the &amp;lt;tt&amp;gt;\font&amp;lt;/tt&amp;gt; command is executed; &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; is the filename asked for, &amp;lt;tt&amp;gt;size&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;at&amp;lt;/tt&amp;gt; size, &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; is the number that will be assigned as an internal representation to the font thus loaded. The callback should return a table representing a font.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=48</id>
		<title>Writing Lua in TeX</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=48"/>
		<updated>2010-12-18T21:59:50Z</updated>

		<summary type="html">&lt;p&gt;Paul: Added description of tex.print() and friends, and a note on the expansion of \directlua.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Embedding Lua code in a TeX document =&lt;br /&gt;
&lt;br /&gt;
Although it is simpler to put Lua code in Lua files, from time to time one may want or need to go Lua in the middle of a document. To this end, LuaTeX has two commands: &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;. They work the same, except &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt; is processed when the page where it appears is shipped out, whereas &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is processed at once; the distinction is immaterial here, and what is said of &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; also applies to &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; can be called in three ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua name {&amp;lt;name&amp;gt;} {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua &amp;lt;number&amp;gt; {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Those three ways are equivalent when it comes to process &amp;lt;tt&amp;gt;&amp;lt;lua code&amp;gt;&amp;lt;/tt&amp;gt;, but in the second case processing will occur in a chunk named &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, and in the third it will occur in a chunk whose name is the entry &amp;lt;tt&amp;gt;&amp;lt;number&amp;gt;&amp;lt;/tt&amp;gt; in the table &amp;lt;tt&amp;gt;lua.name&amp;lt;/tt&amp;gt;. The difference manifests itself only when errors occur, in which case the name of the chunk, if any, is reported.&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, named or not, is processed in a separate chunk. That means that any &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variable is defined for this call only and is lost afterward. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  one = 1&lt;br /&gt;
  local two = 2&lt;br /&gt;
}&lt;br /&gt;
\directlua{&lt;br /&gt;
  texio.write_nl(type(one))&lt;br /&gt;
  texio.write_nl(type(two))&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will report &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; (&amp;lt;tt&amp;gt;texio.write_nl&amp;lt;/tt&amp;gt; writes to the log file). On the other hand, Lua code is completely insensitive to TeX&amp;#039;s grouping mechanism. In other words, calling &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; between &amp;lt;tt&amp;gt;\bgroup&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\egroup&amp;lt;/tt&amp;gt; doesn&amp;#039;t affect the code to be processed.&lt;br /&gt;
&lt;br /&gt;
= TeX catcodes in Lua =&lt;br /&gt;
&lt;br /&gt;
By default, the code passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is treated as normal TeX input and only then sent to the Lua interpreter. This may lead to unwanted results and must be acknowledged.&lt;br /&gt;
&lt;br /&gt;
== Expansion ==&lt;br /&gt;
&lt;br /&gt;
As with any other special, the code in &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; (and the &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, if specifed) is fully expanded. This means that macros can be safely passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; if one wants their values, but that they should also be properly escaped when needed. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; as the number &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;. To store the control sequence &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; instead, another course of action is needed: see the [[#Backslash|section on backslash]] below.&lt;br /&gt;
&lt;br /&gt;
== Line ends ==&lt;br /&gt;
&lt;br /&gt;
When TeX reads a file, it normally turns line ends into spaces. That means that what looks like several lines is actually fed to the Lua interpreter as one big line. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
amounts to the following, if it were written in a separate Lua file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That is perfectly legitimate, but strange things might happen. First, TeX macros gobble spaces as usual. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be fed to the interpreter as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1anothervar = 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is not legitimate at all. Second, the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt; will affect everything to the end of the &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call. That is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
--  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 -- anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which works but only defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt;. Third, when reporting error, the Lua interpreter will always mention the line number as 1, since it processes one big line only; that isn&amp;#039;t extremely useful when the code is large.&lt;br /&gt;
&lt;br /&gt;
The solution is to set &amp;lt;tt&amp;gt;\endlinechar=10&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\catcode`\^^M=12&amp;lt;/tt&amp;gt;. In both cases, line ends will be preserved and the code will be processed as it is input.&lt;br /&gt;
&lt;br /&gt;
== Special characters ==&lt;br /&gt;
&lt;br /&gt;
In TeX, some characters have a special behavior. That must be taken into account when writing Lua code: one must change their catcodes beforehand if one wants to handle them as Lua would, as has just been done for line ends. That means that &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, as such, is clearly insufficient to write any extended chunk of code. It is thus better to devise a special macro that sets the catcodes to the appropriate values, reads the Lua code, feeds it to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, and restores the catcodes. The following code does the job:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\luacode{%&lt;br /&gt;
  \bgroup&lt;br /&gt;
  \catcode`\{=12&lt;br /&gt;
  \catcode`\}=12&lt;br /&gt;
  \catcode`\^^M=12&lt;br /&gt;
  \catcode`\#=12&lt;br /&gt;
  \catcode`\~=12&lt;br /&gt;
  \catcode`\%=12&lt;br /&gt;
  \doluacode&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
\bgroup&lt;br /&gt;
\catcode`\^^M=12 %&lt;br /&gt;
\long\gdef\doluacode#1^^M#2\endluacode{\directlua{#2}\egroup}%&lt;br /&gt;
\egroup&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that not all special characters are set to normal (catcode 12) characters; that is explained for each below. Note also that &amp;lt;tt&amp;gt;\doluacode&amp;lt;/tt&amp;gt;, internally called by &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt;, is defined to get rid of anything up to the line end, and then pass anything up to &amp;lt;tt&amp;gt;\endluacode&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;. Discarding what follows &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is important, otherwise a simple code as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = 1&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
would actually create two lines, the first being empty; it is annoying because errors are then reported with the wrong line number (i.e. any error in this one-line code would be reported to happen on line 2).&lt;br /&gt;
&lt;br /&gt;
However, the rest of the line after &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; could also be processed, instead of discarded, to manage special effects (e.g. specifying a chunk&amp;#039;s name, storing the code in a control sequence, or even setting which catcodes should be changed or not).&lt;br /&gt;
&lt;br /&gt;
=== Backslash ===&lt;br /&gt;
&lt;br /&gt;
The backslash in TeX is used to form control sequences. In the definition of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; above, it isn&amp;#039;t changed and thus behaves as usual. It allows commands to be passed and expanded to the Lua code. Anyway a backslash in Lua is also an escape character in strings. Hence, if one wants to store the name of a macro in Lua code, the following won&amp;#039;t work:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because to the Lua interpreter the string is made of &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; followed by &amp;lt;tt&amp;gt;acro&amp;lt;/tt&amp;gt;; since &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; is not defined in Lua, the string is read as &amp;lt;tt&amp;gt;macro&amp;lt;/tt&amp;gt;, but in other circumstances strange things might happen: for instance, &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; is a newline. The proper way to pass a macro verbatim is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which Lua will correctly read as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\\macro&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with the backslash escaped to represent itself. Another solution is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = [[\noexpand\macro]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because the double brackets signals a string in Lua where no escape sequence occurs (and the string can also run on several lines). Note however that in the second case &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; will be defined with a trailing space, i.e. as &amp;lt;tt&amp;gt;&amp;quot;\macro &amp;quot;&amp;lt;/tt&amp;gt;, because of TeX&amp;#039;s habit to append a trailing space to unexpanded (or unexpandable) control sequences.&lt;br /&gt;
&lt;br /&gt;
=== Braces ===&lt;br /&gt;
&lt;br /&gt;
One may want to define a string in Lua which contains unbalanced braces, i.e.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the braces&amp;#039; catcodes hadn&amp;#039;t been changed beforehand, that would be impossible. Note, however, that this means that one can&amp;#039;t feed arguments to commands in the usual way. I.e. the following will produce nothing good:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\dosomething{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\dosomething&amp;lt;/tt&amp;gt; will be expanded with the left brace (devoid of its usual delimiter-ness) as its argument, and the rest of the line might produce chaos. Thus, one may also choose not to change the catcodes of braces, depending on how &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is most likely to be used. Note that strings with unbalanced braces can still be defined, even if braces have their usual catcodes, thanks to the following trick:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot; -- }&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the code is passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, braces are balanced because the Lua comment means nothing to TeX; when passed to the Lua interpreter, on the other hand, the right brace is ignored.&lt;br /&gt;
&lt;br /&gt;
=== Hash and comment ===&lt;br /&gt;
&lt;br /&gt;
The hash sign &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; in Lua is the length operator: prefixed to a string or table variable, it returns its length. If its catcode weren&amp;#039;t taken care of, LuaTeX would pass to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; a double hash for each hash, i.e. each &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; would be turned into &amp;lt;tt&amp;gt;##&amp;lt;/tt&amp;gt;. That is normal TeX behavior, but unwanted here.&lt;br /&gt;
&lt;br /&gt;
As for the commen sign &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, it is useful in Lua when manipulating strings. If it weren&amp;#039;t escaped it would discard parts of the code when TeX reads it, and a mutilated version of the input would be passed to the Lua interpreter. In turn, discarding a line by commenting it in &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; should be done with the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Active characters ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt; character is generally active and used as a no-break space in TeX. It it were passed as is to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, it would expand to uninterpretable control sequences, whereas in Lua it is used to form the unequal operator &amp;lt;tt&amp;gt;~=&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Other possible active characters should be taken care of, but which characters are active is unpredictable; punctuation marks might be so to accommodate special spacing, as with LaTeX&amp;#039;s &amp;#039;&amp;#039;babel&amp;#039;&amp;#039; package, but such tricks are unlikely to survive in LuaTeX (cleaner methods exist that add a space before punctuation marks when necessary).&lt;br /&gt;
&lt;br /&gt;
=== Other characters ===&lt;br /&gt;
&lt;br /&gt;
When processing verbatim text in TeX, one generally also changes the catcodes of &amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;&amp;amp;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;^&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; and the space character, because they too are special. When passed to the Lua interpreter, though, their usual catcodes won&amp;#039;t do any harm, that is why they are left unmodified here.&lt;br /&gt;
&lt;br /&gt;
== \luaescapestring ==&lt;br /&gt;
&lt;br /&gt;
Although it can&amp;#039;t do all of what&amp;#039;s been explained, the &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; command might be useful in some cases: it expands its argument (which must be enclosed in &amp;#039;&amp;#039;real&amp;#039;&amp;#039; braces) fully, then modify it so that dangerous characters are escaped: backslashes, hashes, quotes and line ends. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{&amp;quot;\noexpand\foo&amp;quot;}&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\luaescapestring{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be passed to Lua as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\&amp;quot;\\foo \&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so that &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; is defined as &amp;lt;tt&amp;gt;&amp;quot;\foo &amp;quot;&amp;lt;/tt&amp;gt;, with the quotes as parts of it. Note that the trailing space after &amp;lt;tt&amp;gt;\foo&amp;lt;/tt&amp;gt; still happens.&lt;br /&gt;
&lt;br /&gt;
= From Lua to TeX =&lt;br /&gt;
&lt;br /&gt;
Inside Lua code, one can pass strings to be processed by TeX with the functions &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;tex.sprint()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;tex.tprint()&amp;lt;/tt&amp;gt;. All such calls are processed at the end of a &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call, even though they might happen in the middle of the code. This behavior is worth noting because it might be surprising in some cases, although it is generally harmless.&lt;br /&gt;
&lt;br /&gt;
== tex.print() ==&lt;br /&gt;
&lt;br /&gt;
This function receives as its argument(s) either one or more strings or an array of strings. Each string is processed as an input line: an end-of-line character is appended (except to the last string), and TeX is in state &amp;lt;tt&amp;gt;newline&amp;lt;/tt&amp;gt; when processing it (i.e. leading spaces are skipped). Hence the two equivalent calls:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.print(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
tex.print({&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
are both interpreted by TeX as would the following two lines:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a&lt;br /&gt;
b&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus `a b&amp;#039; is produced, since line ends normally produce a space.&lt;br /&gt;
&lt;br /&gt;
The function can also take an optional number as its first argument; it is interpreted as referring to a catcode table (as defined by &amp;lt;tt&amp;gt;\initcatcodetable&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\savecatcodetable&amp;lt;/tt&amp;gt;), and each line is processed by TeX with that catcode regime. For instance (note that with such a minimal catcode table, braces don&amp;#039;t even have their usual values):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\bgroup&lt;br /&gt;
\initcatcodetable1&lt;br /&gt;
\catcode`\_=0&lt;br /&gt;
\savecatcodetable1&lt;br /&gt;
\egroup&lt;br /&gt;
&lt;br /&gt;
\directlua{tex.print(1, &amp;quot;_TeX&amp;quot;)}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The string will be read with &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; as an escape character, and thus interpreted as the command commonly known as &amp;lt;tt&amp;gt;\TeX&amp;lt;/tt&amp;gt;. The catcode regime holds only for the strings passed to &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt; and the rest of the document isn&amp;#039;t affected.&lt;br /&gt;
&lt;br /&gt;
If the optional number is &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt;, or points to an invalid (i.e. undefined) catcode table, then the strings are processed with the current catcodes, as if there was no optional argument. If it is &amp;lt;tt&amp;gt;-2&amp;lt;/tt&amp;gt;, then the strings are read as if the result of &amp;lt;tt&amp;gt;\detokenize&amp;lt;/tt&amp;gt;: all characters have catcode 12 (i.e. `other&amp;#039;, characters that have no function beside representing themselves), except space, which has catcode 10 (as usual).&lt;br /&gt;
&lt;br /&gt;
== tex.sprint() ==&lt;br /&gt;
&lt;br /&gt;
Like &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, this function can receive either one or more strings or an array of strings, with an optional number as its first argument pointing to a catcode table. Unlike &amp;lt;tt&amp;gt;tex.print()&amp;lt;/tt&amp;gt;, however, each string is processed as if TeX were in the middle of a line and not at the beginning of a new one: spaces aren&amp;#039;t skipped, no end-of-line character is added and trailing spaces aren&amp;#039;t ignored. Thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.sprint(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is interpreted by TeX as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ab&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
without any space inbetween.&lt;br /&gt;
&lt;br /&gt;
== tex.tprint() ==&lt;br /&gt;
&lt;br /&gt;
This function takes an unlimited number of tables as its arguments; each table must be an array of strings, with the first entry optionally being a number pointing to a catcode table. Then each table is processed as if passed to &amp;lt;tt&amp;gt;tex.sprint()&amp;lt;/tt&amp;gt;. Thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.tprint({1, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;}, {&amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is equivalent to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
tex.sprint(1, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;)&lt;br /&gt;
tex.sprint(&amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= The expansion of \directlua =&lt;br /&gt;
&lt;br /&gt;
A call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is fully expandable; i.e. it can be used in contexts where full expansion is required, as in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\csname\directlua{tex.print(&amp;quot;TeX&amp;quot;)}\endcsname&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is a somewhat convoluted way of saying &amp;lt;tt&amp;gt;\TeX&amp;lt;/tt&amp;gt;. Besides, since Lua code is processed at once, things that were previously unthinkable can now be done easily. For instance, it is impossible to perform an assignment in an &amp;lt;tt&amp;gt;\edef&amp;lt;/tt&amp;gt; by TeX&amp;#039;s traditional means. I.e. the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\edef\macro{\count1=5}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; as &amp;lt;tt&amp;gt;\count1=5&amp;lt;/tt&amp;gt; but doesn&amp;#039;t perform the assignment (the &amp;lt;tt&amp;gt;\edef&amp;lt;/tt&amp;gt; does nothing more than a simple &amp;lt;tt&amp;gt;\def&amp;lt;/tt&amp;gt;). After the definition, the value of &amp;lt;tt&amp;gt;\count1&amp;lt;/tt&amp;gt; hasn&amp;#039;t changed. The same is not true, though, if such an assigment is made with Lua code. The following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\edef\macro{\directlua{tex.count[1] = 5}}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; emptily (since nothing remains after &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; has been processed) &amp;#039;&amp;#039;and&amp;#039;&amp;#039; sets count 1 to 5. Since such a behavior is totally unexpected in normal TeX, one should be wary when using &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; in such contexts.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=47</id>
		<title>Writing Lua in TeX</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=47"/>
		<updated>2010-12-18T15:37:57Z</updated>

		<summary type="html">&lt;p&gt;Paul: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Embedding Lua code in a TeX document =&lt;br /&gt;
&lt;br /&gt;
Although it is simpler to put Lua code in Lua files, from time to time one may want or need to go Lua in the middle of a document. To this end, LuaTeX has two commands: &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;. They work the same, except &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt; is processed when the page where it appears is shipped out, whereas &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is processed at once; the distinction is immaterial here, and what is said of &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; also applies to &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; can be called in three ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua name {&amp;lt;name&amp;gt;} {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua &amp;lt;number&amp;gt; {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Those three ways are equivalent when it comes to process &amp;lt;tt&amp;gt;&amp;lt;lua code&amp;gt;&amp;lt;/tt&amp;gt;, but in the second case processing will occur in a chunk named &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, and in the third it will occur in a chunk whose name is the entry &amp;lt;tt&amp;gt;&amp;lt;number&amp;gt;&amp;lt;/tt&amp;gt; in the table &amp;lt;tt&amp;gt;lua.name&amp;lt;/tt&amp;gt;. The difference manifests itself only when errors occur, in which case the name of the chunk, if any, is reported.&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, named or not, is processed in a separate chunk. That means that any &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variable is defined for this call only and is lost afterward. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  one = 1&lt;br /&gt;
  local two = 2&lt;br /&gt;
}&lt;br /&gt;
\directlua{&lt;br /&gt;
  texio.write_nl(type(one))&lt;br /&gt;
  texio.write_nl(type(two))&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will report &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; (&amp;lt;tt&amp;gt;texio.write_nl&amp;lt;/tt&amp;gt; writes to the log file). On the other hand, Lua code is completely insensitive to TeX&amp;#039;s grouping mechanism. In other words, calling &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; between &amp;lt;tt&amp;gt;\bgroup&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\egroup&amp;lt;/tt&amp;gt; doesn&amp;#039;t affect the code to be processed.&lt;br /&gt;
&lt;br /&gt;
= TeX catcodes in Lua =&lt;br /&gt;
&lt;br /&gt;
By default, the code passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is treated as normal TeX input and only then sent to the Lua interpreter. This may lead to unwanted results and must be acknowledged.&lt;br /&gt;
&lt;br /&gt;
== Expansion ==&lt;br /&gt;
&lt;br /&gt;
As with any other special, the code in &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; (and the &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, if specifed) is fully expanded. This means that macros can be safely passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; if one wants their values, but that they should also be properly escaped when needed. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; as the number &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;. To store the control sequence &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; instead, another course of action is needed: see the [[#Backslash|section on backslash]] below.&lt;br /&gt;
&lt;br /&gt;
== Line ends ==&lt;br /&gt;
&lt;br /&gt;
When TeX reads a file, it normally turns line ends into spaces. That means that what looks like several lines is actually fed to the Lua interpreter as one big line. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
amounts to the following, if it were written in a separate Lua file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That is perfectly legitimate, but strange things might happen. First, TeX macros gobble spaces as usual. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be fed to the interpreter as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1anothervar = 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is not legitimate at all. Second, the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt; will affect everything to the end of the &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call. That is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
--  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 -- anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which works but only defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt;. Third, when reporting error, the Lua interpreter will always mention the line number as 1, since it processes one big line only; that isn&amp;#039;t extremely useful when the code is large.&lt;br /&gt;
&lt;br /&gt;
The solution is to set &amp;lt;tt&amp;gt;\endlinechar=10&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\catcode`\^^M=12&amp;lt;/tt&amp;gt;. In both cases, line ends will be preserved and the code will be processed as it is input.&lt;br /&gt;
&lt;br /&gt;
== Special characters ==&lt;br /&gt;
&lt;br /&gt;
In TeX, some characters have a special behavior. That must be taken into account when writing Lua code: one must change their catcodes beforehand if one wants to handle them as Lua would, as has just been done for line ends. That means that &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, as such, is clearly insufficient to write any extended chunk of code. It is thus better to devise a special macro that sets the catcodes to the appropriate values, reads the Lua code, feeds it to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, and restores the catcodes. The following code does the job:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\luacode{%&lt;br /&gt;
  \bgroup&lt;br /&gt;
  \catcode`\{=12&lt;br /&gt;
  \catcode`\}=12&lt;br /&gt;
  \catcode`\^^M=12&lt;br /&gt;
  \catcode`\#=12&lt;br /&gt;
  \catcode`\~=12&lt;br /&gt;
  \catcode`\%=12&lt;br /&gt;
  \doluacode&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
\bgroup&lt;br /&gt;
\catcode`\^^M=12 %&lt;br /&gt;
\gdef\doluacode#1^^M#2\endluacode{\directlua{#2}\egroup}%&lt;br /&gt;
\egroup&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that not all special characters are set to normal (catcode 12) characters; that is explained for each below. Note also that &amp;lt;tt&amp;gt;\doluacode&amp;lt;/tt&amp;gt;, internally called by &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt;, is defined to get rid of anything up to the line end, and then pass anything up to &amp;lt;tt&amp;gt;\endluacode&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;. Discarding what follows &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is important, otherwise a simple code as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = 1&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
would actually create two lines, the first being empty; it is annoying because errors are then reported with the wrong line number (i.e. any error in this one-line code would be reported to happen on line 2).&lt;br /&gt;
&lt;br /&gt;
However, the rest of the line after &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; could also be processed, instead of discarded, to manage special effects (e.g. specifying a chunk&amp;#039;s name, storing the code in a control sequence, or even setting which catcodes should be changed or not).&lt;br /&gt;
&lt;br /&gt;
=== Backslash ===&lt;br /&gt;
&lt;br /&gt;
The backslash in TeX is used to form control sequences. In the definition of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; above, it isn&amp;#039;t changed and thus behaves as usual. It allows commands to be passed and expanded to the Lua code. Anyway a backslash in Lua is also an escape character in strings. Hence, if one wants to store the name of a macro in Lua code, the following won&amp;#039;t work:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because to the Lua interpreter the string is made of &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; followed by &amp;lt;tt&amp;gt;acro&amp;lt;/tt&amp;gt;; since &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; is not defined in Lua, the string is read as &amp;lt;tt&amp;gt;macro&amp;lt;/tt&amp;gt;, but in other circumstances strange things might happen: for instance, &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; is a newline. The proper way to pass a macro verbatim is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which Lua will correctly read as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\\macro&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with the backslash escaped to represent itself. Another solution is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = [[\noexpand\macro]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because the double brackets signals a string in Lua where no escape sequence occurs (and the string can also run on several lines). Note however that in the second case &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; will be defined with a trailing space, i.e. as &amp;lt;tt&amp;gt;&amp;quot;\macro &amp;quot;&amp;lt;/tt&amp;gt;, because of TeX&amp;#039;s habit to append a trailing space to unexpanded (or unexpandable) control sequences.&lt;br /&gt;
&lt;br /&gt;
=== Braces ===&lt;br /&gt;
&lt;br /&gt;
One may want to define a string in Lua which contains unbalanced braces, i.e.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the braces&amp;#039; catcodes hadn&amp;#039;t been changed beforehand, that would be impossible. Note, however, that this means that one can&amp;#039;t feed arguments to commands in the usual way. I.e. the following will produce nothing good:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\dosomething{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\dosomething&amp;lt;/tt&amp;gt; will be expanded with the left brace (devoid of its usual delimiter-ness) as its argument, and the rest of the line might produce chaos. Thus, one may also choose not to change the catcodes of braces, depending on how &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is most likely to be used. Note that strings with unbalanced braces can still be defined, even if braces have their usual catcodes, thanks to the following trick:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot; -- }&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the code is passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, braces are balanced because the Lua comment means nothing to TeX; when passed to the Lua interpreter, on the other hand, the right brace is ignored.&lt;br /&gt;
&lt;br /&gt;
=== Hash and comment ===&lt;br /&gt;
&lt;br /&gt;
The hash sign &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; in Lua is the length operator: prefixed to a string or table variable, it returns its length. If its catcode weren&amp;#039;t taken care of, LuaTeX would pass to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; a double hash for each hash, i.e. each &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; would be turned into &amp;lt;tt&amp;gt;##&amp;lt;/tt&amp;gt;. That is normal TeX behavior, but unwanted here.&lt;br /&gt;
&lt;br /&gt;
As for the commen sign &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, it is useful in Lua when manipulating strings. If it weren&amp;#039;t escaped it would discard parts of the code when TeX reads it, and a mutilated version of the input would be passed to the Lua interpreter. In turn, discarding a line by commenting it in &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; should be done with the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Active characters ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt; character is generally active and used as a no-break space in TeX. It it were passed as is to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, it would expand to uninterpretable control sequences, whereas in Lua it is used to form the unequal operator &amp;lt;tt&amp;gt;~=&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Other possible active characters should be taken care of, but which characters are active is unpredictable; punctuation marks might be so to accommodate special spacing, as with LaTeX&amp;#039;s &amp;#039;&amp;#039;babel&amp;#039;&amp;#039; package, but such tricks are unlikely to survive in LuaTeX (cleaner methods exist that add a space before punctuation marks when necessary).&lt;br /&gt;
&lt;br /&gt;
=== Other characters ===&lt;br /&gt;
&lt;br /&gt;
When processing verbatim text in TeX, one generally also changes the catcodes of &amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;&amp;amp;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;^&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; and the space character, because they too are special. When passed to the Lua interpreter, though, their usual catcodes won&amp;#039;t do any harm, that is why they are left unmodified here.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
Although it can&amp;#039;t do all of what&amp;#039;s been explained, the &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; command might be useful in some cases: it expands its argument (which must be enclosed in &amp;#039;&amp;#039;real&amp;#039;&amp;#039; braces) fully, then modify it so that dangerous characters are escaped: backslashes, hashes, quotes and line ends. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{&amp;quot;\noexpand\foo&amp;quot;}&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\luaescapestring{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be passed to Lua as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\&amp;quot;\\foo \&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so that &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; is defined as &amp;lt;tt&amp;gt;&amp;quot;\foo &amp;quot;&amp;lt;/tt&amp;gt;, with the quotes as parts of it. Note that the trailing space after &amp;lt;tt&amp;gt;\foo&amp;lt;/tt&amp;gt; still happens.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=46</id>
		<title>Writing Lua in TeX</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=46"/>
		<updated>2010-12-18T14:41:03Z</updated>

		<summary type="html">&lt;p&gt;Paul: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Embedding Lua code in a TeX document =&lt;br /&gt;
&lt;br /&gt;
Although it is simpler to put Lua code in Lua files, from time to time one may want or need to go Lua in the middle of a document. To this end, LuaTeX has two commands: &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;. They work the same, except &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt; is processed when the page where it appears is shipped out, whereas &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is processed at once; the distinction is immaterial here, and what is said of &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; also applies to &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; can be called in three ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua name {&amp;lt;name&amp;gt;} {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua &amp;lt;number&amp;gt; {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Those three ways are equivalent when it comes to process &amp;lt;tt&amp;gt;&amp;lt;lua code&amp;gt;&amp;lt;/tt&amp;gt;, but in the second case processing will occur in a chunk named &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, and in the third it will occur in a chunk whose name is the entry &amp;lt;tt&amp;gt;&amp;lt;number&amp;gt;&amp;lt;/tt&amp;gt; in the table &amp;lt;tt&amp;gt;lua.name&amp;lt;/tt&amp;gt;. The difference manifests itself only when errors occur, in which case the name of the chunk, if any, is reported.&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, named or not, is processed in a separate chunk. That means that any &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variable is defined for this call only and is lost afterward. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  one = 1&lt;br /&gt;
  local two = 2&lt;br /&gt;
}&lt;br /&gt;
\directlua{&lt;br /&gt;
  texio.write_nl(type(one))&lt;br /&gt;
  texio.write_nl(type(two))&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will report &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; (&amp;lt;tt&amp;gt;texio.write_nl&amp;lt;/tt&amp;gt; writes to the log file). On the other hand, Lua code is completely insensitive to TeX&amp;#039;s grouping mechanism. In other words, calling &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; between &amp;lt;tt&amp;gt;\bgroup&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\egroup&amp;lt;/tt&amp;gt; doesn&amp;#039;t affect the code to be processed.&lt;br /&gt;
&lt;br /&gt;
= TeX catcodes in Lua =&lt;br /&gt;
&lt;br /&gt;
By default, the code passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is treated as normal TeX input and only then sent to the Lua interpreter. This may lead to unwanted results and must be acknowledged.&lt;br /&gt;
&lt;br /&gt;
== Expansion ==&lt;br /&gt;
&lt;br /&gt;
As with any other special, the code in &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; (and the &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, if specifed) is fully expanded. This means that macros can be safely passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; if one wants their values, but that they should also be properly escaped when needed. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; as the number &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;. To store the control sequence &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; instead, another course of action is needed: see the [[#Backslash|section on backslash]] below.&lt;br /&gt;
&lt;br /&gt;
== Line ends ==&lt;br /&gt;
&lt;br /&gt;
When TeX reads a file, it normally turns line ends into spaces. That means that what looks like several lines is actually fed to the Lua interpreter as one big line. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
amounts to the following, if it were written in a separate Lua file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That is perfectly legitimate, but strange things might happen. First, TeX macros gobble spaces as usual. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be fed to the interpreter as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1anothervar = 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is not legitimate at all. Second, the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt; will affect everything to the end of the &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call. That is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
--  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 -- anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which works but only defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt;. Third, when reporting error, the Lua interpreter will always mention the line number as 1, since it processes one big line only; that isn&amp;#039;t extremely useful when the code is large.&lt;br /&gt;
&lt;br /&gt;
The solution is to set &amp;lt;tt&amp;gt;\endlinechar=10&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\catcode`\^^M=12&amp;lt;/tt&amp;gt;. In both cases, line ends will be preserved and the code will be processed as it is input.&lt;br /&gt;
&lt;br /&gt;
== Special characters ==&lt;br /&gt;
&lt;br /&gt;
In TeX, some characters have a special behavior. That must be taken into account when writing Lua code: one must change their catcodes beforehand if one wants to handle them as Lua would, as has just been done for line ends. That means that &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, as such, is clearly insufficient to write any extended chunk of code. It is thus better to devise a special macro that sets the catcodes to the appropriate values, reads the Lua code, feeds it to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, and restores the catcodes. The following code does the job:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\luacode{%&lt;br /&gt;
  \bgroup&lt;br /&gt;
  \catcode`\{=12&lt;br /&gt;
  \catcode`\}=12&lt;br /&gt;
  \catcode`\^^M=12&lt;br /&gt;
  \catcode`\#=12&lt;br /&gt;
  \catcode`\~=12&lt;br /&gt;
  \catcode`\%=12&lt;br /&gt;
  \doluacode&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
\bgroup&lt;br /&gt;
\catcode`\^^M=12 %&lt;br /&gt;
\gdef\doluacode#1^^M#2\endluacode{\directlua{#2}\egroup}%&lt;br /&gt;
\egroup&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that not all special characters are set to normal (catcode 12) characters; that is explained for each below. Note also that &amp;lt;tt&amp;gt;\doluacode&amp;lt;/tt&amp;gt;, internally called by &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt;, is defined to get rid of anything up to the line end, and then pass anything up to &amp;lt;tt&amp;gt;\endluacode&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;. Discarding what follows &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is important, otherwise a simple code as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = 1&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
would actually create two lines, the first being empty; it is annoying because errors are then reported with the wrong line number (i.e. any error in this one-line code would be reported to happen on line 2).&lt;br /&gt;
&lt;br /&gt;
However, the rest of the line after &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; could also be processed, instead of discarded, to manage special effects (e.g. specifying a chunk&amp;#039;s name, storing the code in a control sequence, or even setting which catcodes should be changed or not).&lt;br /&gt;
&lt;br /&gt;
=== Backslash ===&lt;br /&gt;
&lt;br /&gt;
The backslash in TeX is used to form control sequences. In the definition of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; above, it isn&amp;#039;t changed and thus behaves as usual. It allows commands to be passed and expanded to the Lua code. Anyway a backslash in Lua is also an escape character in strings. Hence, if one wants to store the name of a macro in Lua code, the following won&amp;#039;t work:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because to the Lua interpreter the string is made of &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; followed by &amp;lt;tt&amp;gt;acro&amp;lt;/tt&amp;gt;; since &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; is not defined in Lua, the string is read as &amp;lt;tt&amp;gt;macro&amp;lt;/tt&amp;gt;, but in other circumstances strange things might happen: for instance, &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; is a newline. The proper way to pass a macro verbatim is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which Lua will correctly read as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\\macro&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with the backslash escaped to represent itself. Another solution is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = [[\noexpand\macro]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because the double brackets signals a string in Lua where no escape sequence occurs (and the string can also run on several lines). Note however that in the second case &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; will be defined with a trailing space, i.e. as &amp;lt;tt&amp;gt;&amp;quot;\macro &amp;quot;&amp;lt;/tt&amp;gt;, because of TeX&amp;#039;s habit to append a trailing space to unexpanded (or unexpandable) control sequences.&lt;br /&gt;
&lt;br /&gt;
=== Braces ===&lt;br /&gt;
&lt;br /&gt;
One may want to define a string in Lua which contains unbalanced braces, i.e.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the braces&amp;#039; catcodes hadn&amp;#039;t been changed beforehand, that would be impossible. Note, however, that this means that one can&amp;#039;t feed arguments to commands in the usual way. I.e. the following will produce nothing good:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\dosomething{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\dosomething&amp;lt;/tt&amp;gt; will be expanded with the left brace (devoid of its usual delimiter-ness) as its argument, and the rest of the line might produce chaos. Thus, one may also choose not to change the catcodes of braces, depending on how &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is most likely to be used. Note that strings with unbalanced braces can still be defined, even if braces have their usual catcodes, thanks to the following trick:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot; -- }&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the code is passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, braces are balanced because the Lua comment means nothing to TeX; when passed to the Lua interpreter, on the other hand, the right brace is ignored.&lt;br /&gt;
&lt;br /&gt;
=== Hash and comment ===&lt;br /&gt;
&lt;br /&gt;
The hash sign &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; in Lua is the length operator: prefixed to a string or table variable, it returns its length. If its catcode weren&amp;#039;t taken care of, LuaTeX would pass to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; a double hash for each hash, i.e. each &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; would be turned into &amp;lt;tt&amp;gt;##&amp;lt;/tt&amp;gt;. That is normal TeX behavior, but unwanted here.&lt;br /&gt;
&lt;br /&gt;
As for the commen sign &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, it is useful in Lua when manipulating strings. If it weren&amp;#039;t escaped it would discard parts of the code when TeX reads it, and a mutilated version of the input would be passed to the Lua interpreter. In turn, discarding a line by commenting it in &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; should be done with the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Active characters ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt; character is generally active and used as a no-break space in TeX. It it were passed as is to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, it would expand to uninterpretable control sequences, whereas in Lua it is used to form the unequal operator &amp;lt;tt&amp;gt;~=&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Other possible active characters should be taken care of, but which characters are active is unpredictable; punctuation marks might be so to accommodate special spacing, as with LaTeX&amp;#039;s &amp;#039;&amp;#039;babel&amp;#039;&amp;#039; package, but such tricks are unlikely to survive in LuaTeX (cleaner methods exist that add a space before punctuation marks when necessary).&lt;br /&gt;
&lt;br /&gt;
=== Other characters ===&lt;br /&gt;
&lt;br /&gt;
When processing verbatim text in TeX, one generally also change the catcodes of &amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;&amp;amp;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;^&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; and the space character, because they too are special. When passed to the Lua interpreter, though, their usual catcodes won&amp;#039;t do any harm, that is why they are left unmodified here.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
Although it can&amp;#039;t do all of what&amp;#039;s been explained, the &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; command might be useful in some cases: it expands its argument (which must be enclosed in &amp;#039;&amp;#039;real&amp;#039;&amp;#039; braces) fully, then modify it so that dangerous characters are escaped: backslashes, hashes, quotes and line ends. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{&amp;quot;\noexpand\foo&amp;quot;}&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\luaescapestring{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be passed to Lua as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\&amp;quot;\\foo \&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so that &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; is defined as &amp;lt;tt&amp;gt;&amp;quot;\foo &amp;quot;&amp;lt;/tt&amp;gt;, with the quotes as parts of it. Note that the trailing space after &amp;lt;tt&amp;gt;\foo&amp;lt;/tt&amp;gt; still happens.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=45</id>
		<title>Writing Lua in TeX</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Writing_Lua_in_TeX&amp;diff=45"/>
		<updated>2010-12-18T14:39:00Z</updated>

		<summary type="html">&lt;p&gt;Paul: Explain how to use \directlua.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Embedding Lua code in a TeX document =&lt;br /&gt;
&lt;br /&gt;
Although it is simpler to put Lua code in Lua files, from time to time one may want or need to go Lua in the middle of a document. To this end, LuaTeX has two commands: &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;. They work the same, except &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt; is processed when the page where it appears is shipped out, whereas &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is processed at once; the distinction is immaterial here, and what is said of &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; also applies to &amp;lt;tt&amp;gt;\latelua&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; can be called in three ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua name {&amp;lt;name&amp;gt;} {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
\directlua &amp;lt;number&amp;gt; {&amp;lt;lua code&amp;gt;}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Those three ways are equivalent when it comes to process &amp;lt;tt&amp;gt;&amp;lt;lua code&amp;gt;&amp;lt;/tt&amp;gt;, but in the second case processing will occur in a chunk named &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, and in the third it will occur in a chunk whose name is the entry &amp;lt;tt&amp;gt;&amp;lt;number&amp;gt;&amp;lt;/tt&amp;gt; in the table &amp;lt;tt&amp;gt;lua.name&amp;lt;/tt&amp;gt;. The difference manifests itself only when errors occur, in which case the name of the chunk, if any, is reported.&lt;br /&gt;
&lt;br /&gt;
Each call to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, named or not, is processed in a separate chunk. That means that any &amp;lt;tt&amp;gt;local&amp;lt;/tt&amp;gt; variable is defined for this call only and is lost afterward. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  one = 1&lt;br /&gt;
  local two = 2&lt;br /&gt;
}&lt;br /&gt;
\directlua{&lt;br /&gt;
  texio.write_nl(type(one))&lt;br /&gt;
  texio.write_nl(type(two))&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will report &amp;lt;tt&amp;gt;number&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; (&amp;lt;tt&amp;gt;texio.write_nl&amp;lt;/tt&amp;gt; writes to the log file). On the other hand, Lua code is completely insensitive to TeX&amp;#039;s grouping mechanism. In other words, calling &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; between &amp;lt;tt&amp;gt;\bgroup&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;\egroup&amp;lt;/tt&amp;gt; doesn&amp;#039;t affect the code to be processed.&lt;br /&gt;
&lt;br /&gt;
= TeX catcodes in Lua =&lt;br /&gt;
&lt;br /&gt;
By default, the code passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; is treated as normal TeX input and only then sent to the Lua interpreter. This may lead to unwanted results and must be acknowledged.&lt;br /&gt;
&lt;br /&gt;
== Expansion ==&lt;br /&gt;
&lt;br /&gt;
As with any other special, the code in &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; (and the &amp;lt;tt&amp;gt;&amp;lt;name&amp;gt;&amp;lt;/tt&amp;gt;, if specifed) is fully expanded. This means that macros can be safely passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; if one wants their values, but that they should also be properly escaped when needed. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; as the number &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;. To store the control sequence &amp;lt;tt&amp;gt;\macro&amp;lt;/tt&amp;gt; instead, another course of action is needed: see the [[#Backslash|section on backslash]] below.&lt;br /&gt;
&lt;br /&gt;
== Line ends ==&lt;br /&gt;
&lt;br /&gt;
When TeX reads a file, it normally turns line ends into spaces. That means that what looks like several lines is actually fed to the Lua interpreter as one big line. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
amounts to the following, if it were written in a separate Lua file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That is perfectly legitimate, but strange things might happen. First, TeX macros gobble spaces as usual. Hence:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{1}&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = \macro&lt;br /&gt;
  anothervar = 2&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be fed to the interpreter as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1anothervar = 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is not legitimate at all. Second, the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt; will affect everything to the end of the &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; call. That is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\directlua{&lt;br /&gt;
  myvar = 1&lt;br /&gt;
--  anothervar = 2&lt;br /&gt;
  onelastvar = 3&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be processed as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = 1 -- anothervar = 2 onelastvar = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which works but only defines &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt;. Third, when reporting error, the Lua interpreter will always mention the line number as 1, since it processes one big line only; that isn&amp;#039;t extremely useful when the code is large.&lt;br /&gt;
&lt;br /&gt;
The solution is to set &amp;lt;tt&amp;gt;\endlinechar=10&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;\catcode`\^^M=12&amp;lt;/tt&amp;gt;. In both cases, line ends will be preserved and the code will be processed as it is input.&lt;br /&gt;
&lt;br /&gt;
== Special characters ==&lt;br /&gt;
&lt;br /&gt;
In TeX, some characters have a special behavior. That must be taken into account when writing Lua code: one must change their catcodes beforehand if one wants to handle them as Lua would, as has just been done for line ends. That means that &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, as such, is clearly insufficient to write any extended chunk of code. It is thus better to devise a special macro that sets the catcodes to the appropriate values, reads the Lua code, feeds it to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, and restores the catcodes. The following code does the job:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\luacode{%&lt;br /&gt;
  \bgroup&lt;br /&gt;
  \catcode`\{=12&lt;br /&gt;
  \catcode`\}=12&lt;br /&gt;
  \catcode`\^^M=12&lt;br /&gt;
  \catcode`\#=12&lt;br /&gt;
  \catcode`\~=12&lt;br /&gt;
  \catcode`\%=12&lt;br /&gt;
  \doluacode&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
\bgroup&lt;br /&gt;
\catcode`\^^M=12 %&lt;br /&gt;
\gdef\doluacode#1^^M#2\endluacode{\directlua{#2}\egroup}%&lt;br /&gt;
\egroup&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that not all special characters are set to normal (catcode 12) characters; that is explained for each below. Note also that &amp;lt;tt&amp;gt;\doluacode&amp;lt;/tt&amp;gt;, internally called by &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt;, is defined to get rid of anything up to the line end, and then pass anything up to &amp;lt;tt&amp;gt;\endluacode&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;. Discarding what follows &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is important, otherwise a simple code as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = 1&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
would actually create two lines, the first being empty; it is annoying because errors are then reported with the wrong line number (i.e. any error in this one-line code would be reported to happen on line 2).&lt;br /&gt;
&lt;br /&gt;
However, the rest of the line after &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; could also be processed, instead of discarded, to manage special effects (e.g. specifying a chunk&amp;#039;s name, storing the code in a control sequence, or even setting which catcodes should be changed or not).&lt;br /&gt;
&lt;br /&gt;
=== Backslash ===&lt;br /&gt;
&lt;br /&gt;
The backslash in TeX is used to form control sequences. In the definition of &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; above, it isn&amp;#039;t changed and thus behaves as usual. It allows commands to be passed and expanded to the Lua code. Anyway a backslash in Lua is also an escape character in strings. Hence, if one wants to store the name of a macro in Lua code, the following won&amp;#039;t work:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because to the Lua interpreter the string is made of &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; followed by &amp;lt;tt&amp;gt;acro&amp;lt;/tt&amp;gt;; since &amp;lt;tt&amp;gt;\m&amp;lt;/tt&amp;gt; is not defined in Lua, the string is read as &amp;lt;tt&amp;gt;macro&amp;lt;/tt&amp;gt;, but in other circumstances strange things might happen: for instance, &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; is a newline. The proper way to pass a macro verbatim is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\noexpand\\macro&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which Lua will correctly read as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\\macro&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with the backslash escaped to represent itself. Another solution is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = [[\noexpand\macro]]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
because the double brackets signals a string in Lua where no escape sequence occurs (and the string can also run on several lines). Note however that in the second case &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; will be defined with a trailing space, i.e. as &amp;lt;tt&amp;gt;&amp;quot;\macro &amp;quot;&amp;lt;/tt&amp;gt;, because of TeX&amp;#039;s habit to append a trailing space to unexpanded (or unexpandable) control sequences.&lt;br /&gt;
&lt;br /&gt;
=== Braces ===&lt;br /&gt;
&lt;br /&gt;
One may want to define a string in Lua which contains unbalanced braces, i.e.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the braces&amp;#039; catcodes hadn&amp;#039;t been changed beforehand, that would be impossible. Note, however, that this means that one can&amp;#039;t feed arguments to commands in the usual way. I.e. the following will produce nothing good:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\dosomething{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;\dosomething&amp;lt;/tt&amp;gt; will be expanded with the left brace (devoid of its usual delimiter-ness) as its argument, and the rest of the line might produce chaos. Thus, one may also choose not to change the catcodes of braces, depending on how &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; is most likely to be used. Note that strings with unbalanced braces can still be defined, even if braces have their usual catcodes, thanks to the following trick:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;{&amp;quot; -- }&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the code is passed to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, braces are balanced because the Lua comment means nothing to TeX; when passed to the Lua interpreter, on the other hand, the right brace is ignored.&lt;br /&gt;
&lt;br /&gt;
=== Hash and comment ===&lt;br /&gt;
&lt;br /&gt;
The hash sign &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; in Lua is the length operator: prefixed to a string or table variable, it returns its length. If its catcode weren&amp;#039;t taken care of, LuaTeX would pass to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt; a double hash for each hash, i.e. each &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt; would be turned into &amp;lt;tt&amp;gt;##&amp;lt;/tt&amp;gt;. That is normal TeX behavior, but unwanted here.&lt;br /&gt;
&lt;br /&gt;
As for the commen sign &amp;lt;tt&amp;gt;%&amp;lt;/tt&amp;gt;, it is useful in Lua when manipulating strings. If it weren&amp;#039;t escaped it would discard parts of the code when TeX reads it, and a mutilated version of the input would be passed to the Lua interpreter. In turn, discarding a line by commenting it in &amp;lt;tt&amp;gt;\luacode&amp;lt;/tt&amp;gt; should be done with the Lua comment &amp;lt;tt&amp;gt;--&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Active characters ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt; character is generally active and used as a no-break space in TeX. It it were passed as is to &amp;lt;tt&amp;gt;\directlua&amp;lt;/tt&amp;gt;, it would expand to uninterpretable control sequences, whereas in Lua it is used to form the unequal operator &amp;lt;tt&amp;gt;~=&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Other possible active characters should be taken care of, but which characters are active is unpredictable; punctuation marks might be so to accommodate special spacing, as with LaTeX&amp;#039;s &amp;#039;&amp;#039;babel&amp;#039;&amp;#039; package, but such tricks are unlikely to survive in LuaTeX (cleaner methods exist that add a space before punctuation marks when necessary).&lt;br /&gt;
&lt;br /&gt;
=== Other characters ===&lt;br /&gt;
&lt;br /&gt;
When processing verbatim text in TeX, one generally also change the catcodes of &amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;&amp;amp;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;^&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt; and the space character, because they too are special. When passed to the Lua interpreter, though, their usual catcodes won&amp;#039;t do any harm, that is why they are left unmodified here.&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Although it can&amp;#039;t do all of what&amp;#039;s been explained, the &amp;lt;tt&amp;gt;\luaescapestring&amp;lt;/tt&amp;gt; command might be useful in some cases: it expands its argument (which must be enclosed in &amp;#039;&amp;#039;real&amp;#039;&amp;#039; braces) fully, then modify it so that dangerous characters are escaped: backslashes, hashes, quotes and line ends. For instance:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\def\macro{&amp;quot;\noexpand\foo&amp;quot;}&lt;br /&gt;
\luacode&lt;br /&gt;
myvar = &amp;quot;\luaescapestring{\macro}&amp;quot;&lt;br /&gt;
\endluacode&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will be passed to Lua as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
myvar = &amp;quot;\&amp;quot;\\foo \&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so that &amp;lt;tt&amp;gt;myvar&amp;lt;/tt&amp;gt; is defined as &amp;lt;tt&amp;gt;&amp;quot;\foo &amp;quot;&amp;lt;/tt&amp;gt;, with the quotes as parts of it. Note that the trailing space after &amp;lt;tt&amp;gt;\foo&amp;lt;/tt&amp;gt; still happens.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Talk:Use_a_TrueType_font&amp;diff=44</id>
		<title>Talk:Use a TrueType font</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Talk:Use_a_TrueType_font&amp;diff=44"/>
		<updated>2010-12-08T09:14:45Z</updated>

		<summary type="html">&lt;p&gt;Paul: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Old location. http://luatex.bluwiki.com/go/Use_a_TrueType_font&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I&amp;#039;ve cleaned up the code, but more is to be done. Some things are useless, and it would be good to have a really minimal code. Other stuffs are missing (e.g. most kerns are in tables not stored with characters).&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Use_a_TrueType_font&amp;diff=43</id>
		<title>Use a TrueType font</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Use_a_TrueType_font&amp;diff=43"/>
		<updated>2010-12-08T09:11:12Z</updated>

		<summary type="html">&lt;p&gt;Paul: Cleaned up the code.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A TrueType (or Openype) font can be used without going through a TFM or an OFM file, like in XeTeX.&lt;br /&gt;
&lt;br /&gt;
The best way to do so is to use the package luaotfload, on the CTAN, and developped here: http://github.com/mpg/luaotfload/&lt;br /&gt;
&lt;br /&gt;
The code shows how we can use the table of a TrueType font to produce an internal TeX font table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\pdfoutput1&lt;br /&gt;
\directlua{&lt;br /&gt;
callback.register(&amp;quot;define_font&amp;quot;,&lt;br /&gt;
  function(name, size)&lt;br /&gt;
    local fonttype, f&lt;br /&gt;
&lt;br /&gt;
    filename = kpse.find_file(name, &amp;quot;opentype fonts&amp;quot;)&lt;br /&gt;
    if filename then&lt;br /&gt;
      fonttype = &amp;quot;opentype&amp;quot;&lt;br /&gt;
    else &lt;br /&gt;
      filename = kpse.find_file(name, &amp;quot;truetype fonts&amp;quot;)&lt;br /&gt;
    end&lt;br /&gt;
    if filename and not fonttype then&lt;br /&gt;
      fonttype = &amp;quot;truetype&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if fonttype then&lt;br /&gt;
      if size &amp;lt; 0 then&lt;br /&gt;
        size = (- 655.36) * size&lt;br /&gt;
      end&lt;br /&gt;
      ttffont = fontloader.to_table(fontloader.open(filename))&lt;br /&gt;
      if ttffont then&lt;br /&gt;
        f = { }&lt;br /&gt;
        f.name = ttffont.fontname&lt;br /&gt;
        f.fullname = ttffont.names[1].names.fullname&lt;br /&gt;
        f.parameters = { }&lt;br /&gt;
        f.designsize = size&lt;br /&gt;
        f.size = size&lt;br /&gt;
        direction = 0&lt;br /&gt;
        f.parameters.slant = 0&lt;br /&gt;
        f.parameters.space = size * 0.25&lt;br /&gt;
        f.parameters.space_stretch = 0.3 * size&lt;br /&gt;
        f.parameters.space_shrink = 0.1 * size&lt;br /&gt;
        f.parameters.x_height = 0.4 * size&lt;br /&gt;
        f.parameters.quad = 1.0 * size&lt;br /&gt;
        f.parameters.extra_space = 0&lt;br /&gt;
        f.characters = { }&lt;br /&gt;
        local mag = size / ttffont.units_per_em&lt;br /&gt;
&lt;br /&gt;
        local names_of_char = { }&lt;br /&gt;
        for char, glyph in pairs(ttffont.map.map) do&lt;br /&gt;
          names_of_char[ttffont.glyphs[glyph].name] = ttffont.map.backmap[glyph]&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        for char, glyph in pairs(ttffont.map.map) do&lt;br /&gt;
          local glyph_table = ttffont.glyphs[glyph]&lt;br /&gt;
&lt;br /&gt;
          f.characters[char] = {&lt;br /&gt;
            index = glyph,&lt;br /&gt;
            width = glyph_table.width * mag,&lt;br /&gt;
            name = glyph_table.name }&lt;br /&gt;
          if glyph_table.boundingbox[4] then&lt;br /&gt;
            f.characters[char].height = glyph_table.boundingbox[4] * mag&lt;br /&gt;
          end&lt;br /&gt;
          if glyph_table.boundingbox[2] then&lt;br /&gt;
            f.characters[char].depth = -glyph_table.boundingbox[2] * mag&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          if glyph_table.kerns then&lt;br /&gt;
            local kerns = { }&lt;br /&gt;
            for _, kern in pairs(glyph_table.kerns) do&lt;br /&gt;
              kerns[names_of_char[kern.char]] = kern.off * mag&lt;br /&gt;
            end&lt;br /&gt;
            f.characters[char].kerns = kerns&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        &lt;br /&gt;
        f.filename = filename&lt;br /&gt;
        f.type = &amp;quot;real&amp;quot;&lt;br /&gt;
        f.format = fonttype&lt;br /&gt;
        f.embedding = &amp;quot;subset&amp;quot;&lt;br /&gt;
        f.cidinfo = {&lt;br /&gt;
          registry = &amp;quot;Adobe&amp;quot;,&lt;br /&gt;
          ordering = &amp;quot;Identity&amp;quot;,&lt;br /&gt;
          supplement = 0,&lt;br /&gt;
          version = 1 }&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      f = font.read_tfm(name, size)&lt;br /&gt;
    end&lt;br /&gt;
  return f&lt;br /&gt;
  end)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The index of the &amp;lt;tt&amp;gt;f.characters&amp;lt;/tt&amp;gt; table is the Unicode value, among the properties of each entry of that table we have &amp;lt;tt&amp;gt;index&amp;lt;/tt&amp;gt; which is the glyph index. Using this property we get the mapping between Unicode characters and glyphs. Kerning is handled via TeX font LKP.&lt;br /&gt;
&lt;br /&gt;
What is missing in the code above is better calculation of standard TeX font parameters and OpenType features.&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Use_a_TrueType_font&amp;diff=42</id>
		<title>Use a TrueType font</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Use_a_TrueType_font&amp;diff=42"/>
		<updated>2010-12-08T09:00:26Z</updated>

		<summary type="html">&lt;p&gt;Paul: Removed the useless definition of names_of_glyph (not used in the code).&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A TrueType (or Openype) font can be used without going through a TFM or an OFM file, like in XeTeX.&lt;br /&gt;
&lt;br /&gt;
The best way to do so is to use the package luaotfload, on the CTAN, and developped here: http://github.com/mpg/luaotfload/&lt;br /&gt;
&lt;br /&gt;
The code shows how we can use the table of a TrueType font to produce an internal TeX font table:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\pdfoutput1&lt;br /&gt;
\directlua{&lt;br /&gt;
callback.register(&amp;#039;define_font&amp;#039;,&lt;br /&gt;
  function(name, size)&lt;br /&gt;
    fonttype = nil&lt;br /&gt;
    filename = kpse.find_file(name,&amp;quot;opentype fonts&amp;quot;)&lt;br /&gt;
    if (filename)&lt;br /&gt;
    then fonttype = &amp;#039;opentype&amp;#039;&lt;br /&gt;
    else filename = kpse.find_file(name, &amp;quot;truetype fonts&amp;quot;)&lt;br /&gt;
    end&lt;br /&gt;
    if filename and not fonttype then fonttype = &amp;#039;truetype&amp;#039; end&lt;br /&gt;
    if fonttype then&lt;br /&gt;
      if (size &amp;lt; 0) then size = (- 655.36) * size end&lt;br /&gt;
      ttffont = fontforge.to_table(fontforge.open(filename))&lt;br /&gt;
      if ttffont then&lt;br /&gt;
        f = { }&lt;br /&gt;
        f.name = ttffont.fontname&lt;br /&gt;
        f.fullname = ttffont.names[1].names.fullname&lt;br /&gt;
        f.parameters = { }&lt;br /&gt;
        f.designsize = size&lt;br /&gt;
        f.size = size&lt;br /&gt;
        direction = 0&lt;br /&gt;
        f.parameters.slant = 0&lt;br /&gt;
        f.parameters.space = size * 0.25&lt;br /&gt;
        f.parameters.space_stretch = 0.3 * size&lt;br /&gt;
        f.parameters.space_shrink = 0.1 * size&lt;br /&gt;
        f.parameters.x_height = 0.4 * size&lt;br /&gt;
        f.parameters.quad = 1.0 * size&lt;br /&gt;
        f.parameters.extra_space = 0&lt;br /&gt;
        f.characters = { }&lt;br /&gt;
        local mag = size / ttffont.units_per_em&lt;br /&gt;
&lt;br /&gt;
         names_of_char = { }&lt;br /&gt;
        for char, glyph in pairs(ttffont.map.map)&lt;br /&gt;
        do&lt;br /&gt;
          names_of_char[ttffont.glyphs[glyph].name]&lt;br /&gt;
            = ttffont.map.backmap[glyph]&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
       for char, glyph in pairs(ttffont.map.map)&lt;br /&gt;
        do&lt;br /&gt;
          glyph_table = ttffont.glyphs[glyph]&lt;br /&gt;
&lt;br /&gt;
          f.characters[char] = {&lt;br /&gt;
            index = glyph,&lt;br /&gt;
            width = glyph_table.width * mag,&lt;br /&gt;
            name = glyph_table.name,&lt;br /&gt;
          }&lt;br /&gt;
          if glyph_table.boundingbox[4] then&lt;br /&gt;
            f.characters[char].height = glyph_table.boundingbox[4] * mag&lt;br /&gt;
          end&lt;br /&gt;
          if glyph_table.boundingbox[2] then&lt;br /&gt;
            f.characters[char].depth = -glyph_table.boundingbox[2] * mag&lt;br /&gt;
          end&lt;br /&gt;
&lt;br /&gt;
          if glyph_table.kerns then&lt;br /&gt;
            local kerns = { }&lt;br /&gt;
            for _, kern in pairs(glyph_table.kerns)&lt;br /&gt;
            do&lt;br /&gt;
              kerns[names_of_char[kern.char]] = kern.off * mag&lt;br /&gt;
            end&lt;br /&gt;
          f.characters[char].kerns = kerns&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
        f.filename = filename&lt;br /&gt;
        f.type = &amp;#039;real&amp;#039;&lt;br /&gt;
        f.format = fonttype&lt;br /&gt;
        f.embedding = &amp;quot;subset&amp;quot;&lt;br /&gt;
        f.cidinfo = {&lt;br /&gt;
          registry = &amp;quot;Adobe&amp;quot;,&lt;br /&gt;
          ordering = &amp;quot;Identity&amp;quot;,&lt;br /&gt;
          supplement = 0,&lt;br /&gt;
          version = 1&lt;br /&gt;
        }&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      f = font.read_tfm(name, size)&lt;br /&gt;
    end&lt;br /&gt;
  return f&lt;br /&gt;
  end&lt;br /&gt;
)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The index of the &amp;lt;tt&amp;gt;f.characters&amp;lt;/tt&amp;gt; table is the Unicode value, among the properties of each entry of that table we have &amp;lt;tt&amp;gt;index&amp;lt;/tt&amp;gt; which is the glyph index. Using this property we get the mapping between Unicode characters and glyphs. Kerning is handled via TeX font LKP.&lt;br /&gt;
&lt;br /&gt;
What is missing in the code above is better calculation of standard TeX font parameters and OpenType features. � suivre...&lt;/div&gt;</summary>
		<author><name>Paul</name></author>
		
	</entry>
</feed>