Changes

Jump to navigation Jump to search
Merge sections
< [[Inside ConTeXt]]
== Using Lua == There is a lovely set of functions in the <tt>utilities.parsers</tt> table that turns strings into nice Lua-accessible tables of various kinds. An example is given below; the functions are documented on the [[String_manipulation#util-prs.lua|string manipulation]] page. <texcode>\startluacodeuserdata = userdata or { } function userdata.mycommand(keywords, keyvals) keyword_options = utilities.parsers.settings_to_array(keywords) named_values = utilities.parsers.settings_to_hash(keyvals) -- do stuff based on that array and that hashtable.end\stopluacode \def\mycommand[#1][#2]{\ctxlua{ userdata.mycommand('#1', '#2')}  \mycommand[top, inmargin, now][color=green, roof=gabled]</texcode> == Using ConTeXt /TeX == This section is based on a post on the mailing list by Taco Hoekwater from [[http://www.mail-archive.com/ntg-context@ntg.nl/msg03235.html 2004-06-28]] and Hans Hagen from [[https://www.mail-archive.com/ntg-context@ntg.nl/msg100795.html 2021-12-14]]. === Example 1 ===
To parse your own optional keyval argument from a command. You may want to adapt the logic according to your needs. Note that in the example below, <tt>[#1]</tt> is not optional.
Of course, one can pass on <tt>#1</tt> as argument to other commands. If <tt>MyOwnKeyOne</tt> is not matched by its argument list, it's ignored.
See also https://www.mail-archive.com/ntg-context@ntg.nl/msg100795.html. == Using Lua == There is a lovely set of functions in the <tt>utilities.parsers</tt> table that turns strings into nice Lua-accessible tables of various kinds. An example is given below; the functions are documented on the [[String_manipulation#util-prs.lua|string manipulation]] page. <texcode>\startluacodeuserdata = userdata or { } function userdata.mycommand(keywords, keyvals) keyword_options = utilities.parsers.settings_to_array(keywords) named_values = utilities.parsers.settings_to_hash(keyvals) -- do stuff based on that array for more complex cases and that hashtable.end\stopluacode \def\mycommand[#1][#2]{\ctxlua{ userdata.mycommand('#1', '#2')}  \mycommand[top, inmargin, now][color=green, roof=gabled]</texcode> == Using TeX == This section is based on a post on the mailing list by Taco Hoekwater from [[http://www.mail-archive.com/ntg-context@ntg.nl/msg03235.html 2004-06-28]]other variants.
=== Example code 2 ===
<texcode>
</texcode>
=== The same example, Example 2 with commentary ===
The 'key' to the keyval functionality in ConTeXt are two macros called
57

edits

Navigation menu