### LuaTeX Example: Create Fontsampler Source: https://wiki.luatex.org/index.php?diff=next&oldid=3577&title=Main_Page Provides an example of creating a font sampler using plain LuaTeX and the `luaotfload` module. ```tex Create a fontsampler using plain LuaTeX and `luaotfload`. ``` -------------------------------- ### Lua Debug Traceback Example Source: https://wiki.luatex.org/index.php?diff=cur&oldid=57&title=Writing_Lua_in_TeX This example demonstrates how to use `debug.traceback(1)` in Lua within a \directlua command to get the current call stack. Be cautious if passing the output to TeX, as it might be interpreted as TeX code. ```tex \directlua{ print(debug.traceback(1)) } ``` -------------------------------- ### Create and Shipout a Node List Source: https://wiki.luatex.org/index.php?action=edit&title=TeX_without_TeX&undo=122&undoafter=121 Demonstrates creating a whatsit node, packing it into an hbox with a rule, and shipping it to the PDF. ```lua start_link.action = node.new("whatsit","pdf_action") start_link.action.action_type = 1 start_link.action.action_id = dest local rule = mkrule(tenpt) local hbox = hpack(start_link, rule, end_link) add_to_page(hbox) -- This pagelist consists of an hbox whose contents is "start_link", -- the 10pt x 10pt rule and the "end_link" node. shipout() --------------------------- -- Just to show you that you can get some memory usage statistics: print(string.format("\nnode_mem_usage=%s",status.node_mem_usage)) ``` -------------------------------- ### Visual Debugging with LuaLaTeX Source: https://wiki.luatex.org/index.php?direction=prev&oldid=163&printable=yes&title=Main_Page Example of using the `lua-visual-debug` package for visual debugging within LuaLaTeX. Requires the package to be installed. ```lua \usepackage{lua-visual-debug} % Your Lua code here ``` -------------------------------- ### Create a PDF Link Node Source: https://wiki.luatex.org/index.php?diff=10845&oldid=117&title=TeX_without_TeX Demonstrates creating a PDF link using whatsit nodes and packing them into an hbox. ```lua local start_link = node.new("whatsit","pdf_start_link") local end_link = node.new("whatsit","pdf_end_link") start_link.width = tenpt start_link.height = tenpt / 2 start_link.depth = tenpt / 2 start_link.link_attr = "/C [0.9 1 0] /Border [0 0 2]" --start_link.action = node.new("action") --there has been an update of the luatex start_link.action = node.new("whatsit","pdf_action") start_link.action.action_type = 1 start_link.action.action_id = dest local rule = mkrule(tenpt) local hbox = hpack(start_link, rule, end_link) add_to_page(hbox) -- This pagelist consists of an hbox whose contents is "start_link", -- the 10pt x 10pt rule and the "end_link" node. shipout() ``` -------------------------------- ### luaindex Package Example Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=163&undoafter=162 Demonstrates the use of the \`luaindex\` package to create an index in LuaLaTeX. This requires the \`luaindex\` package to be installed. ```tex \documentclass{article} \usepackage{luaindex} \begin{document} \index{example} This is an example document. \printindex \end{document} ``` -------------------------------- ### Create and Shipout a Node List Source: https://wiki.luatex.org/index.php?action=edit&title=TeX_without_TeX&undo=140&undoafter=139 Demonstrates creating a node list containing an action, a rule, and an hbox, then shipping it to the PDF. ```lua [0 0 2]" --start_link.action = node.new("action") --there has been an update of the luatex start_link.action = node.new("whatsit","pdf_action") start_link.action.action_type = 1 start_link.action.action_id = dest local rule = mkrule(tenpt) local hbox = hpack(start_link, rule, end_link) add_to_page(hbox) -- This pagelist consists of an hbox whose contents is "start_link", -- the 10pt x 10pt rule and the "end_link" node. shipout() --------------------------- -- Just to show you that you can get some memory usage statistics: print(string.format("\nnode_mem_usage=%s",status.node_mem_usage)) ``` -------------------------------- ### External Q&A Site Link Source: https://wiki.luatex.org/index.php?action=edit&oldid=81&title=Main_Page Example of linking to an external question and answer site. Use to guide users to relevant community support platforms. ```WikiText * [http://tex.stackexchange.com/ tex.stackexchange.com] is a q&a site for questions related to TeX, and you can ask (and answer) LuaTeX related questions there. ``` -------------------------------- ### Create PDF Pages with Lua Source: https://wiki.luatex.org/index.php?action=edit&title=TeX_without_TeX&undo=137&undoafter=136 A complete example demonstrating node manipulation, box creation, and PDF shipout using Lua. ```Lua do -- this will hold the items that go onto the page local pagelist -- Call tex.shipout() with the contents of the pagelist function shipout() local vbox,b = node.vpack(pagelist) -- we ignore the badness 'b' tex.box[666] = vbox tex.shipout(666) pagelist = nil -- Not strictly necessary. TeX holds the current page number in counter 0. -- TeX displays the contents of this counter when it puts a page into -- the pdf (tex.shipout()). If we don't change the counter, TeX will -- display [1] [1], instead of [1] [2] for our two page document. tex.count[0] = tex.count[0] + 1 end function add_to_page( list ) -- We attach the nodelist 'list' to the end of the pagelist -- if pagelist doesn't exist, 'list' is our new pagelist -- if it exists, we go to the end with node.tail() and adjust -- the prev and next pointers, so list becomes part -- of pagelist. if not pagelist then pagelist = list else local tail = node.tail(pagelist) tail.next = list list.prev = tail end end end -- This creates a new square rule and returns the pointer to it. function mkrule( size ) local r = node.new("rule") r.width = size r.height = size / 2 r.depth = size / 2 return r end do local destcounter = 0 -- Create a pdf anchor (dest object). It returns a whatsit node and the -- number of the anchor, so it can be used in a pdf link or an outline. function mkdest() destcounter = destcounter + 1 local d = node.new("whatsit","pdf_dest") d.named_id = 0 d.dest_id = destcounter d.dest_type = 3 return d, destcounter end end -- Take a list of nodes and put them into an hbox. The prev and next fields -- of the nodes will be set automatically. Return a pointer to the hbox. function hpack( ... ) local start, tmp, cur start = select(1,...) tmp = start for i=2,select("#",...) do cur = select(i,...) tmp.next = cur cur.prev = tmp tmp = cur end local h,b = node.hpack(start) -- ignore badness return h end local tenpt = 10 * 2^16 --------------------------- -- page 1 --------------------------- local n,dest = mkdest() -- dest is needed for the link to this anchor add_to_page(n) add_to_page(mkrule(2 * tenpt)) -- The pagelist contains a pdf dest node (a link destination) and a rule of size 20pt x 20pt. shipout() --------------------------- -- page 2 --------------------------- -- This is the page with the link to the anchor (dest) on page one. A -- link consists of three nodes: a pdf_start_link, a pdf_end_link and and -- action node that specifies the action to perform when the user clicks on -- the link. -- The pdf link must be inside a horizontal box, that's why we hpack() it. -- The link_attr (link attributes) is optional, here it draws a yellowish border -- around the link. local start_link = node.new("whatsit","pdf_start_link") local end_link = node.new("whatsit","pdf_end_link") start_link.width = tenpt start_link.height = tenpt / 2 start_link.depth = tenpt / 2 start_link.link_attr = "/C [0.9 1 0] /Border [0 0 2]" --start_link.action = node.new("action") --there has been an update of the luatex start_link.action = node.new("whatsit","pdf_action") start_link.action.action_type = 1 start_link.action.action_id = dest local rule = mkrule(tenpt) local hbox = hpack(start_link, rule, end_link) add_to_page(hbox) -- This p ``` -------------------------------- ### Create and Output a Simple Hbox with a Rule Source: https://wiki.luatex.org/index.php?action=edit&title=TeX_without_TeX&undo=118&undoafter=117 Demonstrates creating a pagelist with a start link, a rule, and an end link, then packing them into an hbox and outputting it to the page. ```lua node.new("whatsit","pdf_action") start_link.action.action_type = 1 start_link.action.action_id = dest local rule = mkrule(tenpt) local hbox = hpack(start_link, rule, end_link) add_to_page(hbox) -- This pagelist consists of an hbox whose contents is "start_link", -- the 10pt x 10pt rule and the "end_link" node. shipout() ``` -------------------------------- ### Example Output of Node Traversal Source: https://wiki.luatex.org/index.php?action=edit&oldid=56&title=Traversing_TeX_nodes This is an example of the output generated by the LuaTeX node traversal code when processing the example document. ```text This is luaTeX, Version 3.141592-snapshot-2007062922 (Web2C 7.5.6) ( NODE type=whatsit subtype=6 NODE type=hlist subtype=0 NODE type=glyph subtype=0 font=51 char=98 NODE type=glyph subtype=0 font=51 char=108 NODE type=glyph subtype=0 font=51 char=941 NODE type=glue subtype=0 NODE type=glyph subtype=0 font=51 char=98 NODE type=glyph subtype=0 font=51 char=108 NODE type=glyph subtype=0 font=51 char=97 NODE type=glue subtype=0 NODE type=glyph subtype=0 font=51 char=98 NODE type=glyph subtype=0 font=51 char=108 NODE type=glyph subtype=0 font=51 char=97 NODE type=glue subtype=0 NODE type=glyph subtype=0 font=51 char=98 NODE type=glyph subtype=0 font=51 char=108 NODE type=glyph subtype=0 font=51 char=97 NODE type=glue subtype=0 Underfull \hbox (badness 10000) in paragraph at lines 14--15 [1] ) ``` -------------------------------- ### Callback Examples Source: https://wiki.luatex.org/index.php?diff=cur&oldid=97&title=Callbacks Examples of specific callbacks and their usage. ```APIDOC ## Callback Examples ### Description Illustrates the usage and purpose of various callbacks within LuaTeX. ### Callbacks #### `process_output_buffer` ##### Description Called for each line TeX writes to an external file, excluding the log file, terminal, and `\write18` calls. ##### Parameters - **line** (string) - The line being written to the output file. - **modified_line** (string) - The modified line (can be altered by the callback). ##### Response - The callback can modify the `modified_line`. #### `process_jobname` ##### Description Processes the jobname when queried with `\jobname` or `tex.jobname`. The internal job name used for output and log files remains untouched. This callback appeared in v.0.71. ##### Parameters - **modified_string** (string) - The jobname string. ##### Response - The callback can modify the `modified_string`. #### `token_filter` ##### Description Called when TeX needs a token. No argument is passed to the callback. The next token must be fetched with `token.get_next()`. What is returned is immediately processed by TeX. ##### Parameters None ##### Response - The callback must return the next token to be processed by TeX. ``` -------------------------------- ### Create and Shipout a Node List Source: https://wiki.luatex.org/index.php?action=edit&title=TeX_without_TeX&undo=147&undoafter=141 Demonstrates creating a link node, a rule, and packing them into an hbox for PDF output. ```lua _link.width = tenpt start_link.height = tenpt / 2 start_link.depth = tenpt / 2 start_link.link_attr = "/C [0.9 1 0] /Border [0 0 2]" --start_link.action = node.new("action") --there has been an update of the luatex start_link.action = node.new("whatsit","pdf_action") start_link.action.action_type = 1 start_link.action.action_id = dest local rule = mkrule(tenpt) local hbox = hpack(start_link, rule, end_link) add_to_page(hbox) -- This pagelist consists of an hbox whose contents is "start_link", -- the 10pt x 10pt rule and the "end_link" node. shipout() --------------------------- -- Just to show you that you can get some memory usage statistics: print(string.format("\nnode_mem_usage=%s",status.node_mem_usage)) ``` -------------------------------- ### Create a fontsampler example Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=10858&undoafter=10141 Provides an example of creating a font sampler using plain LuaTeX and the \`luaotfload\` package. This demonstrates practical application of LuaTeX. ```TeX [[fontsampler|Create a fontsampler]] using plain LuaTeX and luaotfload. ``` -------------------------------- ### Lua Traceback Example Source: https://wiki.luatex.org/index.php?diff=10870&oldid=47&printable=yes&title=Writing_Lua_in_TeX An example of a Lua stack traceback output. ```text | +| 1 | +| stack traceback: | +| <\directlua >:1: in main chunk ``` -------------------------------- ### Create a Font Sampler with LuaTeX Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=10858&undoafter=161 Guide on creating a font sampler utility using plain LuaTeX and the `luaotfload` package. Useful for previewing font characteristics. ```lua require "luaotfload" -- code to generate font sampler ``` -------------------------------- ### Define Footnote Command Start and Stop Source: https://wiki.luatex.org/index.php?printable=yes&title=T_wo_T_r%3Acommands.lua Defines the start and stop functions for the 'Footnote' command. The start function is a no-op, while the stop function handles the actual footnote processing. ```lua local do_command_start = function() end State.do_command_start = do_command_start local do_command_stop = do_footnote push_do_command_stop(do_command_stop) ``` -------------------------------- ### luatexbase Package Example Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=163&undoafter=162 Illustrates the use of \`luatexbase\`, providing basic resource management for LuaTeX code. This example shows how to register a callback. ```tex \documentclass{article} \usepackage{luatexbase} \begin{document} \begin{luacode} function my_callback(arg) tex.print('Callback executed with: ' .. arg) end luatexbase.add_callback('my_callback', my_callback) \end{luacode} \directlua{my_callback('test')} \end{document} ``` -------------------------------- ### Markup Examples Source: https://wiki.luatex.org/index.php?action=edit§ion=6&title=Process_input_buffer Examples of input text strings used for markup processing. ```text This is in /italic/ and this is in *bold*. ``` ```text This is in \italic{italic} and this is in \bold{bold}. ``` ```text This will /not be/ in italic. ``` ```text # A section ## A subsection ### A subsubsection ``` ```text ... the file should be contained in user/directory/myfiles... ``` -------------------------------- ### Create a font sampler with LuaTeX and luaotfload Source: https://wiki.luatex.org/index.php/Special%3AExport/Main_Page This example demonstrates how to create a font sampler using plain LuaTeX and the luaotfload package. It is useful for previewing and selecting OpenType fonts. ```tex \documentclass{article} \usepackage{luaotfload} \begin{document} \fontspec{Times New Roman} This is a sample text in Times New Roman. \end{document} ``` -------------------------------- ### DirectLua Commands and Output Examples Source: https://wiki.luatex.org/index.php?diff=next&oldid=77&title=Writing_Lua_in_TeX Examples of using `\directlua` commands within a .tex file to interact with Lua and capture output, including examples of printing table contents and debug tracebacks. ```APIDOC ## DirectLua Commands and Output Examples ### Description Demonstrates how to use `\directlua` commands in a .tex file to execute Lua code and print its output to stdout. Includes examples for inspecting Lua tables and obtaining debug tracebacks. ### Method N/A (These are TeX commands using Lua integration) ### Endpoint N/A (Executed within a .tex file) ### Parameters N/A ### Request Example ```latex \typeout{==\directlua{for k,v in pairs(tex) do print(k,v) end}==} \typeout{==\directlua{for k,v in pairs(lua) do print(k,v) end}==} \typeout{==\directlua{print(debug.traceback(1))}==} ``` ### Response #### Success Response (200) Output from Lua's `print` function will be sent to stdout, delimited by `==`. #### Response Example ``` tprint function: 0x89086e8 box table: 0x895a0a0 ... ==== setluaname function: 0x895efb8 ... ==== 1 stack traceback: <\directlua >:1: in main chunk ==== ``` ### Error Handling Using `tex.print(debug.traceback())` directly can lead to errors in LaTeX due to special characters like '>'. Use `\directlua{print(debug.traceback())}` instead. ``` -------------------------------- ### Glyph substitution example Source: https://wiki.luatex.org/index.php?direction=next&oldid=15&title=Explore_the_table_obtained_from_a_TrueType_font Example of a glyph entry containing OpenType substitution data. ```text ......12 -> { .........boundingbox -> { ............1 -> 41 ............2 -> -315 ............3 -> 530 ............4 -> 1600 .........} .........name -> parenright .........unicodeenc -> 41 .........possub -> { ............1 -> { ...............flags -> 0 ...............type -> substitution ...............subs -> { ..................variant -> parenleft ...............} ...............script_lang_index -> 1 ...............tag -> rtla ............} .........} .........width -> 649 ......} ......13 -> { .........boundingbox -> { ............1 -> 70 ............2 -> 709 ............3 -> 846 ``` -------------------------------- ### LuaTeX Show Hyphenation Points Example Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=153&undoafter=143 Demonstrates how to show hyphenation points using a LuaTeX callback. Useful for linguistic analysis. ```text Show the hyphenation points ``` -------------------------------- ### LuaTeX Example: Splitting comma-separated lists Source: https://wiki.luatex.org/index.php?diff=next&oldid=112&printable=yes&title=Main_Page Code example for splitting a comma-separated list. ```lua you want to split a comma separated list; ``` -------------------------------- ### Create and Shipout a PDF Link Source: https://wiki.luatex.org/index.php?action=edit&title=TeX_without_TeX&undo=123&undoafter=122 Demonstrates creating a whatsit node for a PDF link and shipping it out to the page. ```lua t_link") local end_link = node.new("whatsit","pdf_end_link") start_link.width = tenpt start_link.height = tenpt / 2 start_link.depth = tenpt / 2 start_link.link_attr = "/C [0.9 1 0] /Border [0 0 2]" --start_link.action = node.new("action") --there has been an update of the luatex start_link.action = node.new("whatsit","pdf_action") start_link.action.action_type = 1 start_link.action.action_id = dest local rule = mkrule(tenpt) local hbox = hpack(start_link, rule, end_link) add_to_page(hbox) -- This pagelist consists of an hbox whose contents is "start_link", -- the 10pt x 10pt rule and the "end_link" node. shipout() --------------------------- -- Just to show you that you can get some memory usage statistics: print(string.format("\nnode_mem_usage=%s",status.node_mem_usage)) ``` -------------------------------- ### Markup Syntax Example Source: https://wiki.luatex.org/index.php?action=edit&oldid=168&title=Process_input_buffer Example of lightweight markup syntax for italic and bold text. ```text This is in /italic/ and this is in *bold*. ``` -------------------------------- ### luamplib Package Example Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=163&undoafter=162 Shows how to use LuaTeX's built-in MetaPost interpreter via the \`luamplib\` package. This example includes a simple MetaPost drawing. ```tex \documentclass{article} \usepackage{luamplib} \begin{document} \mplibsetfile{standalone} \begin{mplibcode} draw (0,0) -- (100,100); \end{mplibcode} \end{document} ``` -------------------------------- ### Lua debug output example Source: https://wiki.luatex.org/index.php?diff=prev&oldid=148&title=Writing_Lua_in_TeX Example output generated by the Lua debug inspection commands. ```text tprint function: 0x89086e8 box table: 0x895a0a0 getsfcode function: 0x895ae10 pdffontsize function: 0x895b048 shipout function: 0x895b2d8 getmath function: 0x895b380 setuccode function: 0x895ae48 getskip function: 0x8908860 pdfxformname function: 0x895b170 setlist function: 0x895ab38 setattribute function: 0x8908898 count table: 0x895b600 getuccode function: 0x895ae80 getbox function: 0x895ab00 uniformdeviate function: 0x895b080 fontname function: 0x895af58 primitives function: 0x895b220 badness function: 0x895b310 setmath function: 0x895b348 setskip function: 0x8908828 getmathcode function: 0x895ada0 getcount function: 0x8908950 toks table: 0x895b6b0 setsfcode function: 0x895add8 run function: 0x8908638 sfcode table: 0x895a130 pdfpageref function: 0x895b138 setcount function: 0x8908918 setdimen function: 0x89087b8 print function: 0x89086d0 setlccode function: 0x895acf8 fontidentifier function: 0x895af90 getlccode function: 0x895ad30 setmathcode function: 0x895ad68 round function: 0x895aeb8 getdimen function: 0x89087f0 write function: 0x89086b8 set function: 0x8908770 definefont function: 0x895b1b0 dimen table: 0x895b550 get function: 0x89087a0 number function: 0x895b0c0 nest table: 0x895a600 lists table: 0x895a550 setcatcode function: 0x895ac18 delcode table: 0x895a4a0 setnest function: 0x895aba8 mathcode table: 0x895a3f0 getdelcode function: 0x895acc0 catcode table: 0x895a340 pdffontname function: 0x895afd0 uccode table: 0x895a290 setdelcode function: 0x895ac88 lccode table: 0x895a1e0 sp function: 0x895af28 attribute table: 0x895b410 getcatcode function: 0x895ac50 extraprimitives function: 0x895b258 linebreak function: 0x895b3b8 setbox function: 0x895aac8 skip table: 0x895b4c0 pdffontobjnum function: 0x895b008 settoks function: 0x8908988 gettoks function: 0x895aa90 enableprimitives function: 0x895b298 getlist function: 0x895ab70 getnest function: 0x895abe0 getattribute function: 0x89088d8 romannumeral function: 0x895b0f8 scale function: 0x895aef0 hashtokens function: 0x895b1e8 error function: 0x8908720 finish function: 0x8908668 ==== setluaname function: 0x895efb8 setbytecode function: 0x895f028 name table: 0x895ef50 getluaname function: 0x895ef80 getbytecode function: 0x895eff0 version Lua 5.1 bytecode table: 0x895f080 ==== 1 stack traceback: <\directlua >:1: in main chunk ==== ``` -------------------------------- ### Metapost with LuaTeX example Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=10858&undoafter=10141 Shows how to use Metapost with LuaTeX. This is from the old bluwiki.com. ```TeX using mplib to write [[metapost with LuaTeX]] ``` -------------------------------- ### Sort a token list example Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=10858&undoafter=10141 Provides an example of how to sort a token list. This is from the old bluwiki.com. ```TeX you want to [[sort a token list]]; ``` -------------------------------- ### Create a fontsampler using LuaTeX and luaotfload Source: https://wiki.luatex.org/index.php?action=edit&title=Main_Page&undo=10858&undoafter=154 Provides an example of creating a font sampler utility using plain LuaTeX and the `luaotfload` library. Useful for previewing font features. ```lua require 'luaotfload' -- code to generate font sampler ``` -------------------------------- ### Markup Failure Example Source: https://wiki.luatex.org/index.php?action=edit&oldid=168&title=Process_input_buffer Example demonstrating that markup processing is line-based and does not span across lines. ```text This will /not be/ in italic. ```