Changes

Jump to navigation Jump to search
1,496 bytes removed ,  07:56, 12 May 2010
Lots of changes. Just replacing with the new version
< [[The ConTeXt Way]] | [[Inside ConTeXt]] | [[Project structure]] >
{{note | I am replacing this article with a detailed article that I am writing for TugBoat. For the time being I am leaving the original article at the end. Fell free to make any changes or to merge the examples of the original article. --[[User:Adityam|Aditya]]}} Many-a-timesVery often, you want to to generate multiple versions of the same document.:One one version for printing and one for viewing on the screen; , one version forstudents and one version for the instructor; , and so on. You can do this in asimple but naive way. Create : create different files with setup set up for the different versions and
<code>\input</code> the common material, or create some new conditional flags using
<code>\newif</code> and set them appropriately for conditional processing. Or you
could use '''<em>modes'''</em>&mdash;the ConTeXt way of doing conditional processing. 
= Introduction =
A mode is similar to a conditional flag, but with a few advantages. New : new modes
need not be explicitly defined (no need for something like <code>\newif</code>),
multiple modes can be simultaneously enabled or disabled, and the status of multiple
modes can be checked easily. Moreover, modes can be set from a command line
switch. SoAs a result, multiple versions of a document can be generated without changing
the source file.
spaces. Names starting with <code>*</code> are reserved for system modes.
In this article I explain how to activate a mode and how to check if a mode is
active or not.
= Setting modes =
ConTeXt has three commands for setting modes:
* <code><cmd>enablemode</cmd> [...]</code>
* <code><cmd>disablemode</cmd>[...]</code>
* <code><cmd>preventmode</cmd>[...]</code>
The names are self explanatory-descriptive. <cmd>enablemode</cmd> activates a mode,
<cmd>disablemode</cmd> deactivates a mode, and <cmd>preventmode</cmd> permanently
deactivates a mode. All three commands take a list of modes as an argument. For
example, you can activate modes named <ttcode>screen</ttcode> and <ttcode>solution</ttcode> modes bywith
<texcode>
\enablemode[screen,solution]
</texcode>
Modes can also be activated by a command line switch <code>--modes</code> to
<code>texexec</code> and or <code>context</code>. For example, another way to activate the <ttcode>screen</ttcode> and<ttcode>solution</ttcode> modes, you can to run ConTeXt usingone of:
texexec --mode=screen,solution ... context --mode=screen,solution ...
or
context --mode=screen,solution ...
To get different pdf output for different = Conditional processing based on modes, you can add <code>--result</code> switch=
context --You may want to process or ignore a chunk of code if a particular mode=screen,solution --result=solutionis enabledor disabled.pdf Such a chunk of code is specified using <code>\startmode</code> and<code>\startnotmode</code> environments.Their usable is best explained by an example.. = Conditional processing based on modes =
Suppose you want to change the paper size of a document depending on whether it
is for print or screen. This can be done in multiple ways. You could either seta the default paper size for print and change it for in screenmode:
<texcode>
\setuppapersize[letter][letter]  \startmode[screen] \setuppapersize[S6][S6] \stopmode
</texcode>
(S6 is one of the screen-optimized paper sizes in ConTeXt; the paper size has a
4:3 aspect ratio and a width equal to the width of A4 paper.)  Alternatively, youcould set a default paper size for the screen and change it if the screen mode is
not enabled:
<texcode>
\setuppapersize[S6][S6]  \startnotmode[screen] \setuppapersize[letter][letter] \stopnotmode
</texcode>
<cmdcode>\startmode</cmdcode> and <cmdcode>\startnotmode</cmdcode> can check for multiple modes. The,arguments to <cmd>startmode</cmd> and <cmd>startnotmode</cmd> can be by giving a list of modesas their arguments. <cmdcode>\startmode</cmdcode> processes its contents (everything until the next<cmdcode>\stopmode</cmdcode> which means that , thus <cmdcode>\startmode</cmdcode> cannot be nested.) if any of the modes are enabled, otherwise (i.e., when all the modes aredisabled) <cmdcode>\startmode</cmdcode> ignores it itscontents. The opposite is <cmdcode>\startnotmode</cmdcode> is theopposite. It : it processes its contents (everything until the next<cmdcode>\stopnotmode</cmdcode> ) if any of the modes are disabled, otherwise (i.e., &mdash;when allthe mods modes are enabled) <cmd>startnotmode</cmd> ignores its &mdash;the contentsare ignored.
<cmdcode>\startmode</cmdcode> and <cmdcode>\startnotmode</cmdcode> are "<em>or</em> " environments. They
process their contents if any of the modes satisfy the required condition. Their
"<em>and</em> " counterparts are also available: <cmdcode>\startallmodes</cmdcode> and <cmdcode>\startnotallmodes</cmdcode>,which process their contents only if all the given modes satisfy the requiredcondition. For example, suppose you want to enable interaction (e.g., hyperlinks) etc.only when both <ttcode>screen</ttcode> and <ttcode>solution</ttcode> modes are enabled. Then you can
use:
<texcode>
\startallmodes[screen,solution] \setupinteraction[state=start] \stopallmodes
</texcode>
<texcode>
\startmode[mode1, mode2, ...]
% Process Processed if one any of the modes is enabled
\stopmode
\startnotmode[mode1, mode2, ...]
% Process Processed if one any of the modes is disabled
\stopnotmode
\startallmodes[mode1, mode2, ...]
% Process Processed if all the modes are enabled
\stopallmodes
\startnotallmodes[mode1, mode2, ...]
% Process Processed if all the modes are disabled
\stopnotallmodes
</texcode>
These environments have a <code>\doif...</code> alternative alternatives that are useful for shortsetups and . Also, they can also be nested.
<texcode>
\doifnotallmodes {modes} {content}
</texcode>
\noindentation The logic for determining when the content is processes processed is exactly the same asfor the <code>start</code>-<code>stop</code> alternativescommands.
These <code>\doif</code> commands each have a variant that can to process something else alternative code if
the conditions are not satisfied (like the <code>\else</code> branch of <code>\if</code>).
<texcode>
\doifmodeelse {modes} {content} {alternativealt}\doifnotmodeelse {modes} {content} {alternativealt}\doifallmodeselse {modes} {content} {alternativealt}\doifnotallmodeselse {modes} {content} {alternativealt}
</texcode>
= System modes =
In addition to Besides allowing user-defined definable modes, ConTeXt provides some system modes. Thesemodes start with a <code>*</code>character. Here I will explainonly explain the more commonly usedsystem modes. You can ; see the ConTeXt [http://pragma-ade.com/general/manuals/mmodes.pdf modes manual]for a complete list of system modes.
Perhaps the most useful system modes are <code>*mkii</code> and <code>*mkiv</code> which
determine whether MkII MKII or MkIV MKIV is being used. These modes are handy when youwant different setups for MkII MKII and MkIVMKIV.
Other modes are useful for very specific situations. Some of these are described
below.
A document must be run multiple times to get the cross referencing, table of contents, etc.\right. However, sometimes you need to do some external processing (like e.g., graphicconversion) that only needs to be done only once. In such cases, the<code>*first</code> mode is handy, which checks for &mdash;it is active only on the first run of the document.
You can use the project-product-component structure for managing large projects
like a book series. See [[Project structure]]
for detailsof this approach. Both A product and or its components can may be compiled separately. Sometimes , and you may want to process do something different when a component differently depending on whether it product isbeing compiled directly, or if the complete product when a component is being compiled. This canTo do so, you need to check forbe checked using the modes <code>*project</code>, <code>*product</code>, <code>*component</code>,andand <code>*environment</code>; these modes are set when the corresponding structure fileis processed. For example, the <code>*product</code> mode is set whenever aproduct file is read (; more specifically, whenever a when <code>\startproduct</code> isencountered). Similarly, a mode <code>*text</code> is enabled whenever awhen<code>\starttext</code> is encountered, and likewise for the others.
A large document is also typically broken down into different section blocks like:
frontmatter, bodymatter, appendices, and backmatter. Internally, these section
blocks are refered referred to as <code>frontpart</code>, <code>backpartbodypart</code>, <code>appendix</code>, and<code>backmatterbackpart</code>. Each section block sets a system mode with the same name. So,if you want macros that work differently for in different section blocks, you cancheck for modes <code>*fronpart</code>, <code>*backpartfrontpart</code>, <code>*appendixbodypart</code>, and<code>*backmatter</code> modesso on.
ConTeXt provides support for multiple languagelanguages. Language Languages are recognized bytheir IETF language tags, like <code>en-us</code> for US English, <code>en-gb</code>
for British English, <code>nl</code> for Dutch, <code>de</code> for German, etc. A document
has a main language , set using with the command <code>\mainlanguage[...]</code> ,that is sued used for
translated labels like <em>chapter</em> and <em>figure</em>. You can also switch the
current language using <code>\language[...]</code> to change the hyphenation rules.
Whenever a language is chosen, its id identifier is set as a mode. The mode for the mainlanguage starts with two <code>*</code>. For example, when the main language is US
English and the current language is Dutch, the modes <code>**en-us</code> and
<code>*nl</code> are set (notice the extra <code>*</code> in <code>**en-us</code>).
Other system modes are: <code>*figure</code> which is set when a graphic is found,<code>*interaction</code> which is set when interaction is enabled, <code>*grid</code> whichis set when grid typesetting is enabled, and <code>*pdf</code> and <code>*dvi</code> whichare set depending on when the whether we are generating pdf output is PDF or dvi outputDVI. Othersare too esoteric to desribe describe here. If you are interested, see the [http://pragma-ade.com/general/manuals/mmodes.pdf modes manual]mentioned earlier.
= Earlier Article Specific Examples = Often you'd like to publish different versions of a document, say a presentation and a handout or a student's and a teacher's version.
ConTeXt supports such filtering with its '''modes''':== Different fonts ==
<texcode>ConTeXt is Suppose you want to generate two versions of a great TeX macro packagedocument, one with times font and one with palatino.\startmode[handout] % The following text will only appear in the handoutIt’s based on Plain TeX like the better known LaTeX.\stopmode</texcode>One way to do this is as follows:
<texcode>
\startnotmode[print]
Here's a link to my homepage: \url[...]
\stopnotmode
</texcode>
 
<texcode>
\doifmode{answers}{The answer to the homework is 42.}
</texcode>
 
<texcode>
\doifmodeelse{draft}{Draft Version}{Final Version}
</texcode>
 
<tt>...mode</tt> and <tt>...notmode</tt> let you filter your content sufficient in most cases.
You can also give several modes like <tt>\startmode[handout,print]</tt>.
 
Modes are also a convenient way to comment out sections (typical: "obsolete" mode).
 
You can typeset the different modes like:
 
<pre>
texexec myfile --mode=handout --result=handout.pdf
</pre>
 
You don't need the <tt>--result</tt>, but otherwise you'd get the same filename for both modes.
 
Modes are extremely powerful when applied to text. However, they can be applied to configuration problems as well. A user wanted to generate different versions of the same file with different fonts:
<texcode>
\startmode[palatino]
\usetypescript[adobekb][8r]
\usetypescript[palatino][8r]
\setupbodyfont[palatino,12pt]
\startmode[times]
\usetypescript[adobekb][8r]
\usetypescript[postscript][8r]
\setupbodyfont[postscript,12pt]
\starttext
\input knuth
\showfontstrip
\stoptext
</texcode>
and run with one of the following:
And run with one of the following:<pre> texexec --pdf --mode=palatino filename texexec --pdf --mode=times filename</pre>
If you want to enable some mode(s) without changing the command line (e.g. because you use some TeX GUI), you can use<cmd>enablemode</cmd><tt>[mymode]</tt> in your source. Put this before you load your environment! You can even enable several modes at once like <tt>[a4,print]</tt>. == First-run-mode: Doing things only Running external commands once == When you need to call external programs to process some of your data and return you some results, you probaly want to do that only once (instead of three times, if ConTeXt needs three runs). Here's how you can do it:
Suppose you want to run some external program, say to generate a figure. Unfortunately, the program only generates postscript figure. So you want to convert it to pdf. This can be done as follows:
<texcode>
\doifmode{startmode[*first}{%] % external program which creates a file figure1fig-1.ps
\executesystemcommand{some_external_program ...}
% convert PS into PDF
\executesystemcommand{texmfstart pstopdf figure1fig-1.ps}}\stopmode
% include the resulting PDF
\externalfigure[figure1fig-1]
</texcode>
= Summary =
 
In summary, modes provide generalized conditional processing. A rich set of
built-in modes is available.
[[Category:ConTeXt programming]]

Navigation menu