Changes

Jump to navigation Jump to search
1,220 bytes removed ,  07:39, 25 January 2017
m
typo correction
< [[Main Page]]>
== Using variables Programming Topics ==
<texcode>=== ConTeXt Features ===\setvariables* [[namespaceModes]]: Conditional processing of text* [[key=valueSetups]]\getvariable{namespace}{key}</texcode>: An alternative to macros for storing chunks of code
== Defining new commands = Commands and Arguments ===* [[System Macros]] (''Recommended reading''Special characters . 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 names'''[thiskey=thatvalue]</code>.) * [[Commands with optional arguments]]: (Topic: one or more optional arguments within brackets.)
Some commands have special characters in their names, that TeX normally does not consider to be === Module Parameters ===letters* [[Module Parameters]]: <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 Passing parameters to be changedmodules. This is done by <cmd>unprotect</cmd> and <cmd>protect</cmd>:
<texcode>=== Programming Techniques ===\unprotect* [[Processing Lists]]: Processing lists of values\def\!test{alfa} * [[Counters]]: Manipulating counters in context\protect * [[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/texcode>(blog post)]
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>.=== Debugging ===
'''See also''':* [[Commands with KeyVal arguments|Commands with Key=Value argumentsConsole Mode]]: Using ConTeXt on keyboard input directly, [[Commands with optional arguments]]rather than loading a <tt>.tex</tt>file.
== Processing lists of values ===== Processing a comma-separated list of values =Using variables ==
Suppose you defined a command like this one somewhere There are several ways to handle variables in your document:ConTeXt.The recommended and easiest method is to use the<texcodett>\def\IHaveTo#1#2{I have to #1 on #2.\par}setvariables</texcodett>So callingand <texcodett>\IHaveTo{tidy up}{Monday}getvariable</texcodett>macros.Doing it this way you also avoid to get in conflict withThis will print out:already defined stuff (as variables use their own namespace).
To store variables, you can use the <contexttt>\def\IHaveTo#1#2{I have to #1 on #2.\par}\IHaveTo{tidy up}{Monday}setvariables</contexttt>macro.
But sometimes you have to repeat some task more than once. In this case you can define a new command:
<texcode>
% stores value in variable namespace:key\defsetvariables[namespace][key=value]% stores the expanded value\MyMumOrderedMeTosetevariables[#1namespace]#2[key=value]%global {\processcommalistsetgvariables[#1namespace]{[key=value]% global and expanded value\IHaveTo{#2}}}setxvariables[namespace][key=value]
</texcode>
Calling
<texcode>
\MyMumOrderedMeTo[Monday,Wednesday,Saturday]{tidy up}
</texcode>
will spare you some typing <i>(but not some tidying up!)</i>:
Use <contexttt>\def\IHaveTo#1#2{I have getvariable</tt> to #1 on #2process a variable.Reading an undefinedvariable results in the <tt>\par}empty</tt> token. This is not a serious problem,\def\MyMumOrderedMeTo[#1]#2%as long as you expect text only. {\processcommalist[#1]{\IHaveTo{#2}}}But be warned: the compilation process breaks, if you expect a dimension\MyMumOrderedMeTo[Mondayor number. So better take care,Wednesdaythat you define your variables,Saturday]{tidy up}</context>before you use them.
 
In case a command <tt>\IHaveTo</tt> is already defined in a slightly different way:
<texcode>
\def\IHaveTo[#1]#2{I have to #2 on #1.\par}</texcode>you can define <tt>\MyMumOrderedMeTo</tt> as% gets value of the variable namespace:<texcode>key\def\MyMumOrderedMeTo[#1]#2% getvariable{\begingroup \def\processitem##1namespace}{\IHaveTo[##1]{#2}}% \processcommalist[#1]\processitem \endgroupkey}
</texcode>
ThisTo avoid problems, again, producesalso pay attention to the following<context>\def\IHaveTo[#1]#2{I have to #2 on #1.\par}\def\MyMumOrderedMeTo[#1]#2% {\begingroup \def\processitem##1{\IHaveTo[##1]{#2}}% \processcommalist[#1]\processitem \endgroup}\MyMumOrderedMeTo[Monday,Wednesday,Saturday]{tidy up}</context>
=== Processing a dash-separated list of values ===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 variable at the same time, so<tt>\getvariable</tt> uses the '''singular''' form and works with '''braces'''.
Sometimes you have more work to do than just that boring stuff at homeOK, here comes a simple example. And as it is quite important as wellLet's say, you don't that we want to loose your time enumerating all of the tasks. Being able to do something likehave variablespace before and after a letter macro called <texcodett>\IHaveToDoTheTasks[1-4,7,9-11]{until tomorrow}Opening</texcodett>may sound like a good idea.
Suppose you already defined:
<texcode>
\long\def\IHaveToDoTheTask[Opening#1]#2{The task % \getvariable{Letter:opening}{before} \noindent{\begstrut#1 has to be done #2.\parendstrut} \getvariable{Letter:opening}{after}}
</texcode>
You have to define some By using variables in your macros first (thanks to Taco!):, you can separate the layout definition,<texcode>% a few auxiliary core so that your macros are needed to uncompress the listget much more flexible.%% \uncompresslist is the twin of the already existing \compresslist% which works in the other direction (syst-new)%\unprotect % I guess this function is already available but couldnt find it...%\def\apptomac#1#2% {\ifx#1\empty\def#1{#2}\else \@EA\def\@EA#1\@EA{#1Just ensure,#2}\fi} % the next macro does this:%% \itemwithdash<<9-11>>- => \dorecurse {<<1+11-9>>}% {\apptomac\uncompressedlist<<9-1+\recurselevel>>}%% (the 1+ and -1 that all variables are needed to solve a counter offset.)\def\itemwithdash#1-#2-% {\@EA\dorecurse\@EA {\the\numexpr 1+#2-#1\relax}% {\@EA\apptomac\@EA\uncompressedlist\@EA {\the\numexpr #1-1+\recurselevel\relax}}}% % top level. The result will be in \uncompressedlist\def\uncompresslist[#1]% {\def\uncompressedlist{}% \def\processitem##1% {\doifinstringelse{-}{##1} {\itemwithdash##1-} {\apptomac\uncompressedlist{##1}}}% \processcommalist[#1]\processitem }set, before you use them!
\protectIn this example we want to have a blank line in front of the opening, and</texcode>two blank lines right after it. The value for the second key containssquare brackets, so it must be enclosed in braces.
And then you're ready to define
<texcode>
\def\IHaveToDoTheTaskssetvariables[#1Letter:opening]#2% {[before=\begingroupblank, \uncompresslist[#1]% <after= Yeah! \def\processitem##1{\IHaveToDoTheTaskblank[##12*big]{#2}}%, \processcommacommand[\uncompressedlist ]\processitem \endgroup}
</texcode>
Guess what! Your You can now save this style setup (among others) in a separate file andinclude it at the start of your document (before <tt>\IHaveToDoTheTasks[1-4,7,9-11]{until tomorrow}Opening</tt> results in:isdefined or at least used).
<context>And don't forget:\def\IHaveToDoTheTask[#1]#2{The task #1 has to be done #2.\par}'''Ensure that all variables are set before you use them!'''
% a few auxiliary core macros are needed to uncompress the list.
%
% \uncompresslist is the twin of the already existing \compresslist
% which works in the other direction (syst-new)
%
\unprotect
% I guess this function is already available but couldnt find it...%\def\apptomac#1#2% {\ifx#1\empty\def#1{#2}\else \@EA\def\@EA#1\@EA{#1,#2}\fi}== Defining new commands ==
% the next macro does this:%% \itemwithdash<<9-11>>- => \dorecurse {<<1+11-9>>}% {\apptomac\uncompressedlist<<9-1+\recurselevel>>}%% (the 1+ and -1 are needed to solve a counter offset.)\def\itemwithdash#1-#2-% {\@EA\dorecurse\@EA {\the\numexpr 1+#2-#1\relax}% {\@EA\apptomac\@EA\uncompressedlist\@EA {\the\numexpr #1-1+\recurselevel\relax}}}%== Special characters in command names ===
% top level. The result will Some commands have special characters in their names, that TeX normally does not consider to be in \uncompressedlist\def\uncompresslist[#1]%letters: <tt>@</tt>, <tt>!</tt> and <tt>?</tt>. {\def\uncompressedlist{}%Before and after the use or definition of such protected commands in your input files, the catcode of these \def\processitem##1% {\doifinstringelse{-}{##1} {\itemwithdash##1-} {\apptomac\uncompressedlist{##1}}}% \processcommalist[#1]\processitem }characters has to be changed. This is done by <cmd>unprotect</cmd> and <cmd>protect</cmd>:
<texcode>\unprotect\def\!test{alfa} \protect</texcode>
The newly defined command <tt>\def!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>\IHaveToDoTheTasks[#1]#2% {\begingroup \uncompresslist[#1]% !<= Yeah! \def\processitem##1{\IHaveToDoTheTask[##1]{#2}}% \processcommacommand[\uncompressedlist]\processitem \endgroup} \IHaveToDoTheTasks[1-4/tt>,7followed 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,9-11]{until tomorrow}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</contextcmd>.
So - what are you still waiting for? Go back to work and do them right away!
=== Comments =Passing verbatim text as macro parameter ==Resulted from thread [http://archive.contextgarden.net/thread/20050704.151237.f815d89d.html] and will be used in some modules such as [[RawSteps]]. It would be nice if processing dash-separated lists of values would make it into the ConTeXt core.
 == Passing (For passing text to LuaTex verbatim text as macro parameter ==, 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,
\TextWithSpaces{A gap from here to there!}
</context>
 
[[Category:Inside ConTeXt]]
[[Category:ConTeXt programming]]
19

edits

Navigation menu