{"id":3309,"date":"2014-04-27T00:39:42","date_gmt":"2014-04-27T00:39:42","guid":{"rendered":"http:\/\/www.readytext.co.uk\/?p=3309"},"modified":"2014-06-01T16:04:59","modified_gmt":"2014-06-01T16:04:59","slug":"neat-trick-with-luatex-passing-lua-tables-from-tex-to-lua","status":"publish","type":"post","link":"https:\/\/www.readytext.co.uk\/?p=3309","title":{"rendered":"Neat trick with LuaTeX: Passing Lua tables from TeX to Lua"},"content":{"rendered":"<p>It has been a long time since I wrote a blog post so I thought I&#8217;d share a rather nice &#8220;trick&#8221; I discovered today. You can pass a Lua table from a TeX macro to Lua. Here&#8217;s how.<\/p>\n<pre class=\"brush: plain; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n% We start by creating a simple macro that takes one argument, but when we expand\r\n% the macro we enclose its argument in braces {...}, which is then coerced, by Lua, \r\n% into a table constructor! \r\n\r\n\\def\\tabtest#1{\r\n\\directlua{\r\n% This simple function is just for demo purposes\r\nfunction join(a,b)\r\nprint(&quot;I was called with (&quot;..a..&quot;,&quot;..b..&quot;)&quot;)\r\nend\r\n\r\n%Here's the neat bit, our TeX macro argument, enclosed in braces {...}, becomes a Lua table!\r\nstr=#1 % Yay, str becomes a Lua table!\r\n% Here we call the join function with values in the table \r\nstr&#x5B;1]&#x5B;1](str&#x5B;1].x,str&#x5B;1].y)\r\n}}\r\n\r\n% Here's our macro call --- \\tabtest{{&lt;--this becomes a table--&gt;}}\r\n% We include a pointer to the function &quot;join&quot;, together with the values to call it with\r\n\\tabtest{%\r\n{% start of Lua table to send\r\n{&#x5B;1]=join, x=1, y=2}\r\n}% end table to send\r\n}% end \\tabdef call\r\n\r\n<\/pre>\n<p>The result of running the above calls join(1,2) and is printed to the terminal: <code>I was called with (1,2)<\/code>. Rather nice, I thought!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It has been a long time since I wrote a blog post so I thought I&#8217;d share a rather nice &#8220;trick&#8221; I discovered today. You can pass a Lua table from a TeX macro to Lua. Here&#8217;s how. % We start by creating a simple macro that takes one argument, but when we expand % [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-3309","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\/3309","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=3309"}],"version-history":[{"count":7,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3309\/revisions"}],"predecessor-version":[{"id":3346,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3309\/revisions\/3346"}],"wp:attachment":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}