Difference between revisions of "Inside ConTeXt"

From Wiki
Jump to navigation Jump to search
m (Added a ToDo and some minor formatting.)
(This page is getting too long; moved some things to Processing Lists.)
Line 1: Line 1:
 
< [[Main Page]] >
 
< [[Main Page]] >
 +
 +
== Programming Topics ==
 +
 +
=== Commands and Arguments ===
 +
 +
* [[Processing Lists]]: Processing lists of values.
 +
  
 
== Using variables ==
 
== Using variables ==
Line 30: Line 37:
 
[[Commands with KeyVal arguments|Commands with Key=Value arguments]],  
 
[[Commands with KeyVal arguments|Commands with Key=Value arguments]],  
 
[[Commands with optional arguments]]
 
[[Commands with optional arguments]]
>
 
 
== Processing lists of values ==
 
=== Processing a comma-separated list of values ===
 
 
Suppose you defined a command like this one somewhere in your document:
 
<texcode>
 
\def\IHaveTo#1#2{I have to #1 on #2.\par}
 
</texcode>
 
So calling
 
<texcode>
 
\IHaveTo{tidy up}{Monday}
 
</texcode>
 
This will print out:
 
 
<context>
 
\def\IHaveTo#1#2{I have to #1 on #2.\par}
 
\IHaveTo{tidy up}{Monday}
 
</context>
 
 
But sometimes you have to repeat some task more than once. In this case you can define a new command:
 
<texcode>
 
\def\MyMumOrderedMeTo[#1]#2%
 
  {\processcommalist[#1]{\IHaveTo{#2}}}
 
</texcode>
 
Calling
 
<texcode>
 
\MyMumOrderedMeTo[Monday,Wednesday,Saturday]{tidy up}
 
</texcode>
 
will spare you some typing <i>(but not some tidying up!)</i>:
 
 
<context>
 
\def\IHaveTo#1#2{I have to #1 on #2.\par}
 
\def\MyMumOrderedMeTo[#1]#2%
 
  {\processcommalist[#1]{\IHaveTo{#2}}}
 
\MyMumOrderedMeTo[Monday,Wednesday,Saturday]{tidy up}
 
</context>
 
 
 
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:
 
<texcode>
 
\def\MyMumOrderedMeTo[#1]#2%
 
  {\begingroup
 
  \def\processitem##1{\IHaveTo[##1]{#2}}%
 
  \processcommalist[#1]\processitem
 
  \endgroup}
 
</texcode>
 
 
This, again, produces:
 
 
<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 ===
 
 
Sometimes you have more work to do than just that boring stuff at home. And as it is quite important as well, you don't want to loose your time enumerating all of the tasks. Being able to do something like
 
<texcode>
 
\IHaveToDoTheTasks[1-4,7,9-11]{until tomorrow}
 
</texcode>
 
may sound like a good idea.
 
 
Suppose you already defined:
 
<texcode>
 
\def\IHaveToDoTheTask[#1]#2{The task #1 has to be done #2.\par}
 
</texcode>
 
 
You have to define some macros first (thanks to Taco!):
 
<texcode>
 
% 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}
 
 
% 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}}}%
 
 
% 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 }
 
 
\protect
 
</texcode>
 
 
And then you're ready to define
 
<texcode>
 
\def\IHaveToDoTheTasks[#1]#2%
 
  {\begingroup
 
  \uncompresslist[#1]% <= Yeah!
 
  \def\processitem##1{\IHaveToDoTheTask[##1]{#2}}%
 
  \processcommacommand[\uncompressedlist]\processitem
 
  \endgroup}
 
</texcode>
 
 
Guess what! Your <tt>\IHaveToDoTheTasks[1-4,7,9-11]{until tomorrow}</tt> results in:
 
 
<context>
 
\def\IHaveToDoTheTask[#1]#2{The task #1 has to be done #2.\par}
 
 
% 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}
 
 
% 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}}}%
 
 
% 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 }
 
 
\protect
 
 
\def\IHaveToDoTheTasks[#1]#2%
 
  {\begingroup
 
  \uncompresslist[#1]% <= Yeah!
 
  \def\processitem##1{\IHaveToDoTheTask[##1]{#2}}%
 
  \processcommacommand[\uncompressedlist]\processitem
 
  \endgroup}
 
 
\IHaveToDoTheTasks[1-4,7,9-11]{until tomorrow}
 
</context>
 
 
So - what are you still waiting for? Go back to work and do them right away!
 
 
=== Comments ===
 
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.
 
  
  

Revision as of 05:14, 4 September 2005

< Main Page >

Programming Topics

Commands and Arguments


Using variables

\setvariables[namespace][key=value]
\getvariable{namespace}{key}

TODO: This could really use a specific example or two. (See: To-Do List)


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: @, ! and ?. Before and after the use or definition of such protected commands in your input files, the catcode of these characters has to be changed. This is done by \unprotect and \protect:

\unprotect
\def\!test{alfa} 
\protect 

The newly defined command \!test can of course only be called upon when we are in the \unprotected state, otherwise TeX reads the command \!, followed by the word test (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 \unprotect and end them with \protect.

See also

Commands with Key=Value arguments, Commands with optional arguments


Passing verbatim text as macro parameter

In case you want to write macros that should handle verbatim text, you can use the tex primitives \obeyspaces and \obeylines. \obeyspaces changes the category code of the space character, so that spaces become significant. \obeylines does the same for the newline character.

This works fine for the following example:

\framed{\obeyspaces{A gap from here     to there!}}

But if you pass this text as a parameter for your own macro \TextWithSpaces

\def\TextWithSpaces#1{\framed{\obeyspaces#1}}%
\TextWithSpaces{A gap from here     to there!}

the additional spaces are ignored. This happens because the category code change is not yet in effect when the argument is parsed, and the spaces are removed during parsing. To keep the spaces, the catcode change must be done before the argument is parsed.

Here is a two-part solution for the problem (suggested by Taco Hoekwater):

\def\TextWithSpaces{\bgroup\obeyspaces\doTextWithSpaces}
\def\doTextWithSpaces#1{\framed{#1}\egroup}

Another way is to postpone argument loading (suggested by Hans Hagen).

\def  \TextWithSpaces  {\framed\bgroup\obeyspaces\doTextWithSpaces}
\def\doTextWithSpaces     #1{#1\egroup} 

Both of these produce the desired result: