Changes

Jump to navigation Jump to search
588 bytes added ,  22:37, 6 June 2012
m
Change command mentions to {{cmd| }} links
writing their own modules. It allows you to set variables in the call and
use them '''within''' the module's code; this was possible, but much less
convenient before.  Here's a brief sample explaining how this mechanismworks. It consists of a test module and an example file; they are dull (Iadmit) but instructive (I hope). Most of what I write here I've learnt from TacoIn real life, this example module could be a whole lot shorter, so all praise is due to him.but that would defeat the educational value :)
Our module will allow users to set the background color for a document. So
we call it <tt>t-bgcolor</tt>.
It The module starts, surprisingly enough, with a line saying
<pretexcode>
\startmodule[bgcolor]
</pretexcode>
Since we need to use some internal parameters, we have to "unprotect" the
contents of the module:
<pretexcode>
\unprotect
</pretexcode>
The next step is to set up the module 's variables with default parametersthe settings asked for by the user. This is handled by a single command that first processes its own argument list, and then it fetches the optionlist the input file that loaded the module has asked for (suchuser options overrule the default settings in the argument list):
<pretexcode>\setupmodule[color=rednewman]</pretexcode>
Our strategy will be to define a variable <tt>\BColor</tt> for the background color whichwill be set by the module; for this, we will use the[[Commands_with_KeyVal_arguments|processaction]] mechanism .
So we define a macro <tt>\BColor</tt> and define it, like this:
<pretexcode>
\processaction[\currentmoduleparameter{color}]
[blue newman=> \def\BColor{bluered}, red gottlieb=> \def\BColor{redyellow}, yellow rothko=> \def\BColor{yellowblue}, \v!unknown=> \def\BColor{white}, \v!default=> \def\BColor{red}]</pretexcode>
We then use this variable to define the background of our document:
<pretexcode>
\setupbackgrounds[page][background=color,backgroundcolor=\BColor]
</pretexcode>
And that's it! We now just have to finish the module with these lines:
<pretexcode>
\protect
\stopmodule
\endinput
</pretexcode>
A simple test document will look like this:
<pretexcode>
\setupcolors[state=start]
\usemodule[bgcolor][color=bluerothko]
\starttext
\stoptext
</pretexcode>
This is just meant as a first example; of course, there are many more possibilities to use this mechanism. If you want to use the parameters directly in your code, you can use the form {{cmd|currentmoduleparameter}}<tt>\getmoduleparameter{color}</tt>, or alternatively, {{cmd|moduleparameter}}<tt>{bgcolor}{color}</tt>.
-- [[User:Thomas|Thomas]] 18:28, 26 April 2006 (CEST) --
-- [[User:Taco|Taco]] 12:46, 27 April 2006 (CEST) --
 
[[Category:Modules]]
[[Category:Inside ConTeXt]]

Navigation menu