Open main menu

Changes

4,593 bytes added ,  07:39, 25 January 2017
m
typo correction
< [[Main Page]]> == Programming Topics == === ConTeXt Features ===* [[Modes]]: Conditional processing of text* [[Setups]]: An alternative to macros for storing chunks of code === Commands and Arguments ===* [[System Macros]] (''Recommended reading''. Topics: temporary variables, expansion control, argument grabbing and handling, definitions and assignments, branches and decisions, cases, comma separated lists, assignments and parameters, user interaction.)* [[Programming in LuaTeX]] (Topic: alleviating the more cumbersome sides of TeX programming.)* [[Commands with KeyVal arguments|Commands with Key=Value arguments]]: (Topic: things like <code>\command[thiskey=thatvalue]</code>.) * [[Commands with optional arguments]]: (Topic: one or more optional arguments within brackets.) === 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://randomdeterminism.wordpress.com/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.
== Using variables ==
 
There are several ways to handle variables in ConTeXt.
The recommended and easiest method is to use the
<tt>\setvariables</tt> and <tt>\getvariable</tt> macros.
Doing it this way you also avoid to get in conflict with
already 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 undefined
variable 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, if you expect a dimension
or number. So better take care, that you define your variables, before you use them.
 
<texcode>
% gets value of the variable namespace:key
\getvariable{namespace}{key}
</texcode>
 
To avoid problems, 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''' form
and works with '''square brackets'''.
On the other hand you can only process one variable at the same time, so
<tt>\getvariable</tt> uses the '''singular''' form and works with '''braces'''.
 
OK, here comes a simple example. Let's say, that we want to have variable
space 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 macros get much more flexible.
Just ensure, that all variables are set, before you use them!
 
In this example we want to have a blank line in front of the opening, and
two blank lines right after it. The value for the second key contains
square 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 and
include it at the start of your document (before <tt>\Opening</tt> is
defined or at least used).
 
And don't forget:
'''Ensure that all variables are set before you use them!'''
 
== Defining new commands ==
'''=== Special characters in command names'''===
Some commands have special characters in their names, that TeX normally does not consider to be
The newly defined command <tt>\!test</tt> can of course only be called upon when we are in the <cmd>unprotect</cmd>ed state, otherwise TeX reads the command <tt>\!</tt>, followed by the word <tt>test</tt> (and 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 end them with <cmd>protect</cmd>.
'''See also''':
[[Commands with KeyVal arguments|Commands with Key=Value arguments]],
[[Commands with optional arguments]]
>
== Processing a comma-separated list of values 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:
Suppose you defined a command like this one somewhere in your document:
<texcode>
\defframed{\IHaveTo#1#2obeyspaces{I have A gap from here to #1 on #2.\parthere!}}
</texcode>
So calling<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>
\IHaveTodef\TextWithSpaces#1{tidy up\framed{\obeyspaces#1}}%\TextWithSpaces{MondayA gap from here to there!}
</texcode>
Will print out
I have to tidy up on Monday.
But sometimes you have <context>\def\TextWithSpaces#1{\framed{\obeyspaces#1}}%\TextWithSpaces{A gap from here to repeat some task more than oncethere!}</context> the additional spaces are '''ignored'''. In this case you can define 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 '''before''' the argument is parsed. Here is a new commandtwo-part solution for the problem (''suggested by Taco Hoekwater'')
<texcode>
\def\MyMumOrderedMeTo[#1]#2%TextWithSpaces{\bgroup\obeyspaces\doTextWithSpaces} {\processcommalist[def\doTextWithSpaces#1]{\IHaveToframed{#2}1}\egroup}
</texcode>
CallingAnother way is to postpone argument loading (''suggested by Hans Hagen''). 
<texcode>
\MyMumOrderedMeTo[Monday,Wednesday,Saturday]def \TextWithSpaces {tidy up\framed\bgroup\obeyspaces\doTextWithSpaces}\def\doTextWithSpaces #1{#1\egroup}
</texcode>
will spare you some typing <i>(however not tidying up!)</i>:
I have to tidy up on Monday. I have to tidy up on Wednesday. I have to tidy up on Saturday.Both of these produce the desired result:
In case a command <tt>\IHaveTo</tt> is already defined in a slightly different way:<texcodecontext>\def \IHaveTo[#1]#2TextWithSpaces {I have to #2 on #1.\par}</texcode>you can define <tt>framed\MyMumOrderedMeTo</tt> as:<texcode>bgroup\defobeyspaces\MyMumOrderedMeTo[#1]#2%doTextWithSpaces} {\begingroup \def\processitem#doTextWithSpaces #1{\IHaveToDo[##1]{#2\egroup}}% \processcommalist[#1]\processitem \endgroupTextWithSpaces{A gap from here to there!}</texcodecontext[[Category:Inside ConTeXt]][[Category:ConTeXt programming]]
19

edits