{"id":1189,"date":"2011-10-13T20:43:14","date_gmt":"2011-10-13T20:43:14","guid":{"rendered":"http:\/\/www.readytext.co.uk\/?p=1189"},"modified":"2013-11-28T09:02:12","modified_gmt":"2013-11-28T09:02:12","slug":"hopefully-useful-example-of-directlua-expansion","status":"publish","type":"post","link":"https:\/\/www.readytext.co.uk\/?p=1189","title":{"rendered":"Hopefully useful example of \\directlua{} expansion"},"content":{"rendered":"<p>The following example may help to understand a little more about <code>\\directlua{}<\/code> expansion.<\/p>\n<p><code>\\documentclass[11pt,twoside]{article}<br \/>\n\\begin{document}<\/code><\/p>\n<p><code>\\def\\xx{Hello}<br \/>\n\\def\\yy{(}<br \/>\n\\def\\zz{)}<br \/>\n\\newcommand{\\hellofromTeX}[1]{<\/code><\/p>\n<p><code>\\directlua{<\/p>\n<p>function Hello(str)<br \/>\ntex.print(str)<br \/>\nend<\/p>\n<p>\\xx\\yy#1\\zz<br \/>\n}}<\/p>\n<p><\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>\\hellofromTeX{\"Hello World\"}<br \/>\n\\end{document}<\/code><\/p>\n<p><strong>Here&#8217;s how it works.<\/strong><br \/>\nCode within <code>\\directlua{}<\/code> is expanded according to TeX rules and then sent to the Lua interpreter. In the above, LuaTeX &#8220;sees&#8221; <code>\\xx\\yy#1\\zz<\/code> and expands it as follows:<\/p>\n<p><code><br \/>\n\\xx --&gt; Hello<br \/>\n\\yy --&gt; (<br \/>\n#1  --&gt; \"Hello World\"<br \/>\n\\zz --&gt; )<br \/>\n<\/code><\/p>\n<p>After this expansion, the code is fed to the Lua interpreter, which sees  <code>Hello(\"Hello World\")<\/code> and executes<\/p>\n<p><code><br \/>\nfunction Hello(str)<br \/>\ntex.print(str)<br \/>\nend<br \/>\n<\/code><\/p>\n<p>The function <code>tex.print(\"Hello World\") <\/code> is called and typesets some text.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following example may help to understand a little more about \\directlua{} expansion. \\documentclass[11pt,twoside]{article} \\begin{document} \\def\\xx{Hello} \\def\\yy{(} \\def\\zz{)} \\newcommand{\\hellofromTeX}[1]{ \\directlua{ function Hello(str) tex.print(str) end \\xx\\yy#1\\zz }} &nbsp; \\hellofromTeX{&#8220;Hello World&#8221;} \\end{document} Here&#8217;s how it works. Code within \\directlua{} is expanded according to TeX rules and then sent to the Lua interpreter. In the above, LuaTeX &#8220;sees&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-1189","post","type-post","status-publish","format-standard","hentry","category-examples","category-luatex"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1189"}],"version-history":[{"count":17,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1189\/revisions"}],"predecessor-version":[{"id":3266,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1189\/revisions\/3266"}],"wp:attachment":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}