Changes

Jump to navigation Jump to search
6,770 bytes added ,  11:39, 11 March 2019
m
closing tag much needed
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-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 <code>screen</code> and <code>solution</code> with
<texcode>
\enablemode[screen,solution]
</texcode>
<code>solution</code> modes, to run ConTeXt using one of:
texexec --mode=screen,solution ... context --mode=screen,solution ...
== Pre-defining modes ==
Normally, the overhead for testing modes is negligible, but it can add up if modes are tested multiple times in a document (for example, as part of a macro). In such cases, you can ''define'' a mode before using them, to speed up the processing. Modes are defined using:
 
<texcode>
\definemode[...][...]
</texcode>
 
The first argument is a list of modes; the second argument may be `yes`, `no`, or `keep`. For example,
 
* <code>\definemode[screen][yes]</code> defines a mode and enables it;
* <code>\definemode[screen][no]</code> defines a mode and disables it;
* <code>\definemode[screen][keep]</code> defines a mode and keeps its previous status.
 
Typically, it is better to use <code>\definemode[...][keep]</code> so that the modes may be enabled or disabled from command line as well.
= Conditional processing based on modes =
You may want to process or ignore a chunk of code if a particular mode is enabled
or disabled. Such a chunk of code is specified using <code>\startmode</code> and
<code>\startnotmode</code> environments. Their usable use is best explained by an example.
Suppose you want to change the paper size of a document depending on whether it
the default paper size for print and change it in screen mode:
<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
not enabled:
<texcode>
\setuppapersize[S6][S6] \startnotmode[screen] \setuppapersize[letter][letter] \stopnotmode</texcode> <code>\startmode</code> (and <code>\startnotmode</code>) checks the value of the mode '''at the time it is executed'''. This is important when you are setting the modes using <code>\enablemode</code> or <code>\disablemode</code>. For example,<texcode>\enablemode[foo]\startmode[foo]...\stopmode</texcode>the contents of the mode environment are executed because <code>foo</code> is enabled when <code>\startmode</code> is encountered. However, in<texcode>\startmode[foo]...\stopmode\enablemode[foo]
</texcode>
the contents of the mode environment are not execited because <code>foo</code> is not enabled when <code>\startmode</code> is encountered.
 
== Checking for multiple modes (<code>or</code>/<code>and</code> statements for modes) ==
<code>\startmode</code> and <code>\startnotmode</code> can check for multiple modes,
by giving a list of modes as their arguments. <code>\startmode</code>
use:
<texcode>
\startallmodes[screen,solution] \setupinteraction[state=start] \stopallmodes
</texcode>
<texcode>
\doifmode {modesmode1, mode2, ...} {contentProcessed if any mode is enabled}\doifnotmode {modesmode1, mode2, ...} {contentProcessed if any mode is disabled}\doifallmodes {modesmode1, mode2, ...} {contentProcessed if all modes are enabled}\doifnotallmodes {modesmode1, mode2, ...} {contentProcessed if all modes are disabled}
</texcode>
The logic for determining when the content is processed is exactly the same as
the conditions are not satisfied (like the <code>\else</code> branch of <code>\if</code>).
<texcode>
\doifmodeelse {modesmode1, mode2, ...} {contentProcessed if any mode is enabled} {altelse this is processed}\doifnotmodeelse doifallmodeselse {modesmode1, mode2, ...} {contentProcessed if all modes are enabled} {altelse this is processed}\doifallmodeselse doifnotallmodeselse {modesmode1, mode2, ...} {contentProcessed if all modes are disabled} {altelse this is processed}</texcode> Note that there is no command <code>\doifnotmodeelse</code> because there is no need for it; <code>\doifmodeelse</code> may be used for the same effect (with the <code>if</code> and <code>else</code> branches switched). == Checking multiple modes in parallel (<code>case</code> statement for modes) == In addition to the above <em>"or"</em> and <em>"and"</em> environment which check modes is sequence, you can also check multiple modes in parallel. The syntax for such a <em>"case"</em> environment is as follows: <texcode>\doifnotallmodeselsestartmodeset [mode1, mode2, ...] {modesProcessed if either mode is enabled} [mode3, mode4, ...] {contentProcessed if either mode is enabled} [default] {altProcessed if none of the above modes match}\stopmodeset
</texcode>
 
The same mode can be referenced multiple times, and '''all''' matching branches are executed.
The {{cmd|startmodeset}} ... {{cmd|stopmodeset}} environments can be nested. So, you can use
 
<texcode>
\startmodeset
[mode1, mode2] {
Processed when either mode1 or mode2 is enabled
}
[mode3] {
\startmodeset
[mode1] {Processed when mode1 and mode3 are enabled}
[mode2] {Processed when mode2 and mode3 are enabled}
[default] {Processed when mode3 is enabled and mode1 and mode2 are disabled}
\stopmodeset
}
[default] {
Processed when mode1, mode2, and mode3 are disabled.:
}
\stopmodeset
</texcode>
 
== Checking modes in Lua ==
 
In MkIV, the state of any mode is accessible at the Lua end as <code>tex.modes</code> table. Specifically,
 
<texcode>
tex.modes["screen"]
</texcode>
 
returns <code>true</code> if mode <code>screen</code> is enabled and <code>false</code> otherwise. Thus, specific combinations of modes can be checked using boolean expressions. For example
<texcode>
if (tex.modes["mode1"] and tex.modes["mode2"]) then
...
end
</texcode>
checks if both <code>mode1</code> and <code>mode2</code> are enabled.
 
= System modes =
Besides allowing user-definable modes, ConTeXt provides some system
modes. These modes start with a <code>*</code> character. Here I will explainonly the more commonly used system modesare explained; see the ConTeXt [http://pragma-ade.com/general/manuals/mmodes.pdf modes manual]
for a complete list.
 
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
|'''*mkii'''
| Enabled when running [[MkII]]
|-
| '''*mkiv'''
| Enabled when running [[MkIV]]
|}
 
Perhaps the most useful system modes are <code>*mkii</code> and <code>*mkiv</code> which
Other modes are useful for very specific situations. Some of these are described
below.
 
 
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
| '''*first'''
| Enabled during the first compile run
|}
A document must be run multiple times to get the cross referencing,
<code>*first</code> mode is handy&mdash;it is active only on the first run of the
document.
 
 
 
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
| '''*export'''
| Enabled when <code>\setupbackend[export=yes]</code> is set
|}
 
You may want to use different images for XML [Export]. The <code>*export</code> mode is useful in such cases.
 
 
 
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
|'''*project'''
| Enabled when inside <code>\startproject</code> ... <code>\stopproject</code>
|-
|'''*component'''
| Enabled when inside <code>\startcomponent</code>...<code>\stopcomponent</code>
|-
| '''*environment'''
| Enabled when inside <code>\startenvironment</code> ... <code>\stopenvironment</code>
|-
| '''*text'''
| Enabled when inside <code>\starttext</code> ... <code>\stoptext</code>.
|}
You can use the project-product-component structure for managing large projects
encountered. Similarly, a mode <code>*text</code> is enabled when
<code>\starttext</code> is encountered, and likewise for the others.
 
 
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
|'''*frontpart'''
| Enabled when inside <code>\startfrontmatter</code> ... <code>\stopfrontmatter</code>
|-
| '''*bodypart'''
| Enabled when inside <code>\startbodymatter</code> ... <code>\stopbodymatter</code>
|-
| '''*backpart'''
| Enabled when inside <code>\startbackmatter</code> ... <code>\stopbackmatter</code>
|}
A large document is typically broken down into different section blocks:
if you want macros that work differently in different section blocks, you can
check for modes <code>*frontpart</code>, <code>*bodypart</code>, and so on.
 
 
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
|'''*list'''
| Enabled inside a list entry
|-
|'''*marking'''
| Enabled inside a marking
|-
|'''*register'''
| Enabled inside a register
|-
|'''*chapter''', '''*section''', etc.
| Enabled inside the corresponding section head.
|}
 
 
Sometimes you want a macro to behave differently if it is part of a section head, a section number, a list, a marking, or a register. For section heads, you can check for modes <code>*chapter</code>, <code>*section</code>, <code>*subsection</code>, etc. Similarly, <code>*list</code> is enabled inside a list, <code>*marking</code> is enabled inside a marking, and <code>*register</code> is enabled inside a register.
 
 
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
|'''*en-us''', '''*nl''', etc.
| Enabled when the current <code>\language</code> is <code>en-us</code>, <code>nl</code>, etc.
|-
|'''**en-us''', '''**nl''', etc.
| Enabled when the <code>\mainlanguage</code> is <code>en-us</code>, <code>nl</code>, etc.
|}
ConTeXt provides support for multiple languages. Languages are recognized by
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>).
 
 
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
|'''*figure'''
| Enabled when a graphic is found
|-
|'''*interaction'''
| Enabled when interaction is enabled
|-
|'''*grid'''
| Enabled when grid typesetting is enabled
|}
 
{|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;"
|'''*pdf'''
| Enabled when the main output is pdf
|-
|'''*dvi'''
| Enabled when the main output is dvi
|}
Other system modes: <code>*figure</code> is set when a graphic is found,
<texcode>
\startmode[palatino]
\usetypescript[palatino][8r]
\setupbodyfont[palatino,12pt]
\stopmode
\startmode[times]
\usetypescript[postscript][8r]
\setupbodyfont[postscript,12pt]
\stopmode
and run with one of the following:
texexec context --mode=palatino filename texexec context --mode=times filename
== Running external commands once ==
</texcode>
= Summary = In summary, modes provide generalized conditional processing. A rich set ofbuilt-in modes is available.{{Getting started navbox}}
[[Category:ConTeXt programming]]
139

edits

Navigation menu