Open main menu

Changes

8,495 bytes added ,  18:22, 19 January 2011
m
Reverted edits by Cocktails019 (Talk) to last version by Adityam
== Cocktails - How To Make A Rum Fix Cocktail ==< [[Main Page]] >
[[Image:Example123213213689.jpg]]== Programming Topics ==
There are different ways in making '''=== ConTeXt Features ===* [http[Modes]]://www.mymixeddrinks.com cocktails]''', it's up on you what kind of '''cocktail''' you would like to make. This article will tell you non how to make a Rum Fix cocktail. 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 the best taste of it. You will enjoy this recipe for a Rum Fix cocktail. A thirst quenching rum based drink with a splash of lemon taste. Lemon is one Conditional processing of the important ingredients in making a cocktail it adds some looks to your '''cocktail''' and also adds flavor on it.text
=== Commands and Arguments ===* [[ImageSystem Macros]] (temporary variables, expansion control, argument grabbing and handling, definitions and assignments, branches and decisions, cases, comma separated lists, assignments and paramters, user intraction)* [[Programming in LuaTeX]]* [[Commands with KeyVal arguments|Commands with Key=Value arguments]]:Example12321321555For things like <code>\command[thiskey=thatvalue]</code>.jpg* [[Commands with optional arguments]]: One or more optional arguments within brackets.
In making it a rum fix '''=== Module Parameters ===* [[Module Parameters]]: Passing parameters to modules. === 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]''' of course first thing ]: Using ConTeXt on keyboard input directly, rather than loading a <tt>.tex</tt> file. == 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, you 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, wateras 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'''.   OK, here comes a simple example. Be sure 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 macros, you can separate the layout definition,so that your ingredients 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:''cocktail'Just ensure, that all variables are set, before you use them!'''  ==== Using setups for namespaces ====Using <tt>\setups</tt> for a variable namespace allows an easier control over thecontaining variables.All you have to do is to define the setups<tt>namespace:set</tt> and/or <tt>namespace:reset</tt>for a given namespace. Nowevery time a variable of that namespace is assigned (written), ConTeXtautomatically calls these setups. Reading of variables is totally unaffected by thesesettings.A possible use are default values, first step calculations and even verification. So once you have 'setup' your variables proper, you don't have to do worry aboutunset variables and alike any more. Also changes can be made easy, as there is onlyone common setup.The drawback is measure and add the sugarslower speed in use,as every assignment to a variable calls these setups.  To give you the idea, water try this example. The <tt>set</tt>-part is called* '''right after''' the definition of the namespace (initialisation) and lemon juice * '''after''' a value is assigned to a variable.The <tt>reset</tt>-part is called* '''right after''' any assignment, but still in front of the glass<tt>set</tt>-part. Then, stir contents well  <texcode>\setupoutput[pdftex] \startsetups namespace:set%\writestatus{VARIABLES}{namespace:set is beeing called..}%\ {\green [namespace:set]} % whatever must be done with your variables after you assign a stirrer or spoonvalue % % (initialisation with defaults,..)\stopsetups  \startsetups namespace:reset%\writestatus{VARIABLES}{namespace:set is beeing called..}%\ {\green [namespace:reset]} % whatever must be done after an assignment (verification, calculation,..)\stopsetups  % \setups[namespace:set] is automatically called right after 'set' is assigned\setvariables[namespace] [set={\setups[namespace:set]}, reset={\setups[namespace:reset]}, ] % watch for the colors\setupcolors[state=start] \starttext\hairline{\bf reading has no effect\par}{\tt Calling \type{\getvariable{namespace}{key}}...\getvariable{namespace}{key}} \blank{\bf writing calls reset and set\par}{\tt Calling \type{\setvariables[namespace][key=value]}... Add ice cubes \setvariables[namespace][key=value]} \stoptext </texcode> == Defining new commands == === Special characters in command names === Some commands have special characters in their names, that TeX normally does not consider to be letters: <tt>@</tt>, <tt>!</tt> and <tt>?</tt>. Before and after the glassuse or definition of such protected commands in your input files, Measure the catcode of these characters has to be changed. This is done by <cmd>unprotect</cmd> and add <cmd>protect</cmd>: <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 rum <cmd>unprotect</cmd>ed state, otherwise TeX reads the command <tt>\!</tt>, followed by the word <tt>test</tt> (and stir ingredients together again probably complains loudly about not being in math mode). These protection/unprotection commands can be nested. When the nesting becomes deeper than one level, the system reports the current protection level. It is a good habit to always start your macro files with <cmd>unprotect</cmd> and garnish end them with <cmd>protect</cmd>.   == Passing verbatim text as macro parameter == (For passing text to LuaTex verbatim, see the [[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,you can use the tex primitives <tt>\obeyspaces</tt> and <tt>\obeylines</tt>.<tt>\obeyspaces</tt> changes the 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 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> == Setups ==In ConTeXt it is easy to create local variables and grouping. Local variables can be simulated as in: <texcode>\startsetups whatever% some useful definitions here\stopsetups \definestartstop[whatever][commands=\setups{whatever}] \startwhateverUsing definitions here.\stopwhatever</texcode> But you can place setups almost everywhere and environment will not be affected by their execution. It is useful to wrap overlay definitions and the cherrysuch in setups as in (copied from [[Colorful_CD_Inlay]] page): <texcode>\defineoverlay [origin] [\setups{origin}] \startsetups origin \vbox to \overlayheight { \vfill\tfxx\setstrut \hsize\overlaywidth \hfill Fiona Apple\enspace EM\enspace2005\quad\strut\endgraf \kern1ex }\stopsetups</texcode> You can even do things like: <texcode>\starttext\startsetups settest\def\command{do something with}I want to \command{} command.\stopsetups \start\setups{settest}\stop \command aaa % will give "undefined control sequence" error \stoptext</texcode> [[Category:Inside ConTeXt]]