Open main menu

Changes

6,486 bytes added ,  10:11, 16 December 2021
Add hints on bracket usage; c&p from the list: https://www.mail-archive.com/ntg-context@ntg.nl/msg87937.html
== Cocktails - How To Make A Rum Fix Cocktail =Programming Topics =
== ConTeXt Features ==* [[ImageModes]]:Example123213213689.jpgConditional processing of text* [[Setups]]: An alternative to macros for storing chunks of code
There are different ways in making '''== Commands and Arguments ==* [[http://www.mymixeddrinks.com cocktailsSystem Macros]](''', it's up on you what kind of '''cocktailRecommended reading''' you would like to make. This article will tell you non how to make a Rum Fix cocktailTopics: temporary variables, expansion control, argument grabbing and handling, definitions and assignments, branches and decisions, cases, comma separated lists, assignments and parameters, user interaction. Just like any other cocktail a Run Fix cocktail has an ingredients needed to complete it, there is also a step to be followed to get )* [[Programming in LuaTeX]] (Topic: alleviating the best taste more cumbersome sides of itTeX programming. You will enjoy this recipe for a Rum Fix cocktail)* [[Commands with KeyVal arguments|Commands with Key=Value arguments]]: (Topic: things like <code>\command[thiskey=thatvalue]</code>. A thirst quenching rum based drink ) * [[Commands with a splash of lemon taste. Lemon is optional arguments]]: (Topic: one of the important ingredients in making a cocktail it adds some looks to your '''cocktail''' and also adds flavor on itor more optional arguments within brackets.)
== Module Parameters ==* [[ImageModule Parameters]]:Example12321321555Passing parameters to modules.jpg]]
In making it a rum fix '''== Programming Techniques ==* [[Processing Lists]]: Processing lists of values* [[Counters]]: Manipulating counters in context* [[Expressions]]: Evaluating expressions of type number, dimen, glue or muglue* [[Executesystemcommand]]: process contents of an environment by another program* Loops and expansion [http://wwwrandomdeterminism.mymixeddrinkswordpress.com cocktails/2009/03/05/tex-programming-the-past-the-present-and-the-future/ (blog post)== Debugging == * [[Console Mode]]: Using ConTeXt on keyboard input directly, rather than loading a <tt>.tex</tt> file. = Use of brackets = One must '''not' '' confuse with the LaTeX convention where "mandatory"arguments are contained in curly braces and brackets indicate"optional" arguments. Curly braces not only give grouping but generallyare used for objects to be typeset, as for \in{Figure}{a} [fig:ref]. For new users, it is worth repeating here that arguments within bracescan be either a comma-separated list of words OR a comma-separatedlist of course keyword=value pairs, BUT NOT A MIXTURE OF BOTH. Generally, akeyword=value exists for all words, for example \cite[authoryear][ref]and \cite[alternative=authoryear,reference=ref] values can be grouped using curly braces, as in\cite[alternative=authoryear,lefttext={{see },}][ref1,ref2] where thelefttext is associated with the first thing cite reference (and none with thesecond). This can be tricky but is in fact rather straight-forward. = Using variables = There are several ways to do handle variables in ConTeXt.The recommended and easiest method is to prepare use the ingredients like caster sugar<tt>\setvariables</tt> and <tt>\getvariable</tt> macros.Doing it this way you also avoid to get in conflict withalready defined stuff (as variables use their own namespace). To store variables, wateryou can use the <tt>\setvariables</tt>macro. <texcode>% stores value in variable namespace:key\setvariables[namespace][key=value]% stores the expanded value\setevariables[namespace][key=value]% global\setgvariables[namespace][key=value]% global and expanded value\setxvariables[namespace][key=value]</texcode> Use <tt>\getvariable</tt> to process a variable. Reading an undefinedvariable results in the <tt>\empty</tt> token. This is not a serious problem,as long as you expect text only.But be warned: the compilation process breaks, lemon juiceif you expect a dimensionor number. So better take care, dark opr light rumthat you define your variables, maraschino cherrybefore you use them. <texcode>% gets value of the variable namespace:key\getvariable{namespace}{key}</texcode> To avoid problems, a lemon slice also pay attention to the following: You can set several variables (same namespace) at the same time.So the command <tt>\setvariables</tt> logically uses the '''plural''' formand works with '''square brackets'''.On the other hand you can only process one of variable at the most important ingredients same time, so<tt>\getvariable</tt> uses the ice cubes'''singular''' form and works with '''braces'''. Be sure  OK, here comes a simple example. Let's say, that we want to complete have variablespace before and after a letter macro called <tt>\Opening</tt>. <texcode>\long\def\Opening#1{% \getvariable{Letter:opening}{before} \noindent{\begstrut#1\endstrut} \getvariable{Letter:opening}{after}}</texcode> By using variables in your ingredients macros, you can separate the layout definition,so that your macros get much more flexible.Just ensure, that all variables are set, before making it you use them! In this example we want to make have a good taste blank line in front of the opening, andtwo blank lines right after it. The value for the second key containssquare brackets, so it must be enclosed in braces. <texcode>\setvariables[Letter:opening] [before=\blank, after={\blank[2*big]}, ]</texcode> You can now save this style setup (among others) in a separate file andinclude it at the start of your document (before <tt>\Opening</tt> isdefined or at least used). And don't forget:'''cocktailEnsure that all variables are set before you use them!''' == CLD ==How to pass variable from TeX to Lua and vice versa? See [[CLD_passing_variables#Variables|CLD passing variables]]. Now = Defining new commands = == Special characters in command names == Some commands have special characters in their names, first step that TeX normally does not consider to be letters: <tt>@</tt>, <tt>!</tt> and <tt>?</tt>. Before and after the use or definition of such protected commands in your input files, the catcode of these characters has to do be changed. This is measure done by {{cmd|unprotect}} and add {{cmd|protect}}: <texcode>\unprotect\def\!test{alfa} \protect </texcode> The newly defined command <tt>\!test</tt> can of course only be called upon when we are in the sugar{{cmd|unprotect}}ed state, water otherwise TeX reads the command <tt>\!</tt>, followed by the word <tt>test</tt> (and lemon juice probably complains loudly about not being in math mode). These protection/unprotection commands can be nested. When the glassnesting becomes deeper than one level, the system reports the current protection level. Then, stir contents well It is a good habit to always start your macro files with {{cmd|unprotect}} and end them with a stirrer or spoon{{cmd|protect}}. Add ice cubes   = Passing verbatim text as macro parameter = (For passing text to LuaTex verbatim, see the glass[[Programming_in_LuaTeX#Manipulating_verbatim_text_for_dummies|Programming in LuaTeX]] article on this wiki.) In case you want to write macros that should handle verbatim text, Measure you can use the tex primitives <tt>\obeyspaces</tt> and add <tt>\obeylines</tt>.<tt>\obeyspaces</tt> changes the rum category code of the space character,so that spaces become significant. <tt>\obeylines</tt> does the same for thenewline character. This works fine for the following example: <texcode>\framed{\obeyspaces{A gap from here to there!}}</texcode> <context>\framed{\obeyspaces{A gap from here to there!}}</context> But if you pass this text as a parameter for your own macro<tt>\TextWithSpaces</tt> <texcode>\def\TextWithSpaces#1{\framed{\obeyspaces#1}}%\TextWithSpaces{A gap from here to there!}</texcode> <context>\def\TextWithSpaces#1{\framed{\obeyspaces#1}}%\TextWithSpaces{A gap from here to there!}</context> the additional spaces are '''ignored'''.This happens because the category code change is not yet in effect whenthe argument is parsed, and stir ingredients together again and garnish your the spaces are removed during parsing. To keepthe spaces, the catcode change must be done '''drinkbefore''' with the argument is parsed. Here is a wedge two-part solution for the problem (''suggested by Taco Hoekwater''): <texcode>\def\TextWithSpaces{\bgroup\obeyspaces\doTextWithSpaces}\def\doTextWithSpaces#1{\framed{#1}\egroup}</texcode> Another way is to postpone argument loading (''suggested by Hans Hagen''). <texcode>\def \TextWithSpaces {\framed\bgroup\obeyspaces\doTextWithSpaces}\def\doTextWithSpaces #1{#1\egroup} </texcode> Both of lemon these produce the desired result: <context>\def \TextWithSpaces {\framed\bgroup\obeyspaces\doTextWithSpaces}\def\doTextWithSpaces #1{#1\egroup}  \TextWithSpaces{A gap from here to there!}</context> [[Category:Programming and the cherry.Databases]][[Category:Tools]]
57

edits