Difference between revisions of "Modes"
m (→Conditional processing based on modes: changing tags) |
m (Text replacement - "pragma-ade.com" to "pragma-ade.nl") |
||
(42 intermediate revisions by 11 users not shown) | |||
Line 1: | Line 1: | ||
− | + | Very often, you want to generate multiple versions of the same document: | |
− | + | one version for printing and one for viewing on the screen, one version for | |
− | + | students and one version for the instructor, and so on. You can do this in a | |
− | + | simple but naive way: create different files set up for the different versions and | |
− | |||
− | |||
− | students and one version for the instructor | ||
− | naive way | ||
<code>\input</code> the common material, or create some new conditional flags using | <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 | <code>\newif</code> and set them appropriately for conditional processing. Or you | ||
− | could use | + | could use <em>modes</em>—the ConTeXt way of doing conditional processing. |
− | |||
= Introduction = | = Introduction = | ||
− | A mode is similar to a conditional flag, but with a few advantages | + | A mode is similar to a conditional flag, but with a few advantages: new modes |
need not be explicitly defined (no need for something like <code>\newif</code>), | need not be explicitly defined (no need for something like <code>\newif</code>), | ||
− | multiple modes can be simultaneously enabled or disabled, the status of multiple | + | 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 | modes can be checked easily. Moreover, modes can be set from a command line | ||
− | switch. | + | switch. As a result, multiple versions of a document can be generated without changing the source file. |
− | the source file. | ||
The name or identifier of a mode can be any combination of letters, digits, or | The name or identifier of a mode can be any combination of letters, digits, or | ||
spaces. Names starting with <code>*</code> are reserved for system modes. | spaces. Names starting with <code>*</code> are reserved for system modes. | ||
− | |||
= Setting modes = | = Setting modes = | ||
Line 29: | Line 22: | ||
ConTeXt has three commands for setting modes: | ConTeXt has three commands for setting modes: | ||
− | * <code> | + | * <code>{{cmd|enablemode }}[...]</code> |
− | * <code> | + | * <code>{{cmd|disablemode}}[...]</code> |
− | * <code> | + | * <code>{{cmd|preventmode}}[...]</code> |
− | The names are self | + | The names are self-descriptive. {{cmd|enablemode}} activates a mode, |
− | + | {{cmd|disablemode}} deactivates a mode, and {{cmd|preventmode}} permanently | |
deactivates a mode. All three commands take a list of modes as an argument. For | deactivates a mode. All three commands take a list of modes as an argument. For | ||
− | example, | + | example, to active modes named <code>screen</code> and <code>solution</code> |
+ | use: | ||
<texcode> | <texcode> | ||
\enablemode[screen,solution] | \enablemode[screen,solution] | ||
Line 42: | Line 36: | ||
Modes can also be activated by a command line switch <code>--modes</code> to | Modes can also be activated by a command line switch <code>--modes</code> to | ||
− | + | <code>context</code>. For example, another way to activate the <code>screen</code> and | |
− | < | + | <code>solution</code> modes, to run ConTeXt using one of: |
+ | |||
+ | 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). To speed up the processing, a mode can be ''define''d before being | ||
+ | used: | ||
+ | |||
+ | <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 = | = 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 use is best explained by an example. | ||
Suppose you want to change the paper size of a document depending on whether it | 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 | + | is for print or screen. This can be done in multiple ways. You could set |
− | + | the default paper size for print and change it in screen mode: | |
<texcode> | <texcode> | ||
\setuppapersize[letter][letter] | \setuppapersize[letter][letter] | ||
− | |||
\startmode[screen] | \startmode[screen] | ||
\setuppapersize[S6][S6] | \setuppapersize[S6][S6] | ||
Line 68: | Line 76: | ||
</texcode> | </texcode> | ||
(S6 is one of the screen-optimized paper sizes in ConTeXt; the paper size has a | (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, you | + | 4:3 aspect ratio and a width equal to the width of A4 paper.) |
− | could set a default paper size for screen and change it if | + | |
− | not enabled: | + | Alternatively, you could set a default paper size for the screen and change it |
+ | if screen mode is not enabled: | ||
<texcode> | <texcode> | ||
\setuppapersize[S6][S6] | \setuppapersize[S6][S6] | ||
− | |||
\startnotmode[screen] | \startnotmode[screen] | ||
\setuppapersize[letter][letter] | \setuppapersize[letter][letter] | ||
Line 79: | Line 87: | ||
</texcode> | </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 executed 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> | ||
+ | processes its contents (everything until the next | ||
+ | <code>\stopmode</code>, thus <code>\startmode</code> cannot be | ||
+ | nested.) if any of the modes are enabled, otherwise (i.e., when all | ||
+ | the modes are disabled) <code>\startmode</code> ignores its | ||
+ | contents. The opposite is <code>\startnotmode</code>: it processes its | ||
+ | contents (everything until the next <code>\stopnotmode</code>) if any of the | ||
+ | modes are disabled, otherwise—when all the modes are enabled—the contents are ignored. | ||
+ | |||
+ | <code>\startmode</code> and <code>\startnotmode</code> are "<em>or</em>" environments. They | ||
process their contents if any of the modes satisfy the required condition. Their | process their contents if any of the modes satisfy the required condition. Their | ||
− | <em>and</em> counterparts are < | + | "<em>and</em>" counterparts are also available: <code>\startallmodes</code> and <code>\startnotallmodes</code> |
− | + | process their contents only if all the given modes satisfy the required | |
− | condition. For example, suppose you want to enable interaction (hyperlinks) | + | condition. For example, suppose you want to enable interaction (e.g., hyperlinks) |
− | only when both < | + | only when both <code>screen</code> and <code>solution</code> modes are enabled. Then you can |
use: | use: | ||
<texcode> | <texcode> | ||
Line 105: | Line 131: | ||
<texcode> | <texcode> | ||
\startmode[mode1, mode2, ...] | \startmode[mode1, mode2, ...] | ||
− | % | + | % Processed if any of the modes is enabled |
\stopmode | \stopmode | ||
\startnotmode[mode1, mode2, ...] | \startnotmode[mode1, mode2, ...] | ||
− | % | + | % Processed if any of the modes is disabled |
\stopnotmode | \stopnotmode | ||
\startallmodes[mode1, mode2, ...] | \startallmodes[mode1, mode2, ...] | ||
− | % | + | % Processed if all the modes are enabled |
\stopallmodes | \stopallmodes | ||
\startnotallmodes[mode1, mode2, ...] | \startnotallmodes[mode1, mode2, ...] | ||
− | % | + | % Processed if all the modes are disabled |
\stopnotallmodes | \stopnotallmodes | ||
</texcode> | </texcode> | ||
− | These environments have | + | These environments have <code>\doif...</code> alternatives that are useful for short |
− | setups | + | setups. Also, they can be nested. |
<texcode> | <texcode> | ||
− | \doifmode { | + | \doifmode {mode1, mode2, ...} {Processed if any mode is enabled} |
− | \doifnotmode { | + | \doifnotmode {mode1, mode2, ...} {Processed if any mode is disabled} |
− | \doifallmodes { | + | \doifallmodes {mode1, mode2, ...} {Processed if all modes are enabled} |
− | \doifnotallmodes { | + | \doifnotallmodes {mode1, mode2, ...} {Processed if all modes are disabled} |
</texcode> | </texcode> | ||
− | + | The logic for determining when the content is processed is exactly the same as | |
− | The logic for determining when the content is | + | for the <code>start</code>-<code>stop</code> commands. |
− | for the <code>start</code>-<code>stop</code> | ||
− | These <code>\doif</code> commands have a variant | + | These <code>\doif</code> commands each have a variant to process alternative code if |
the conditions are not satisfied (like the <code>\else</code> branch of <code>\if</code>). | the conditions are not satisfied (like the <code>\else</code> branch of <code>\if</code>). | ||
<texcode> | <texcode> | ||
− | \doifmodeelse { | + | \doifmodeelse {mode1, mode2, ...} {Processed if any mode is enabled} {else this is processed} |
− | \ | + | \doifallmodeselse {mode1, mode2, ...} {Processed if all modes are enabled} {else this is processed} |
− | \ | + | \doifnotallmodeselse {mode1, mode2, ...} {Processed if all modes are disabled} {else 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> | ||
+ | \startmodeset | ||
+ | % NOTE: Empty lines are not allowed here! | ||
+ | [mode1, mode2, ...] {Processed if either mode is enabled} | ||
+ | [mode3, mode4, ...] {Processed if either mode is enabled} | ||
+ | [default] {Processed if none of the above modes match} | ||
+ | \stopmodeset | ||
</texcode> | </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 == | ||
+ | |||
+ | 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 = | = System modes = | ||
− | + | Besides allowing user-definable modes, ConTeXt provides some system | |
− | modes start with a <code>*</code>. Here | + | modes. These modes start with a <code>*</code> character. Here |
− | system modes | + | only the more commonly used system modes are explained; see the ConTeXt [http://pragma-ade.nl/general/manuals/mmodes.pdf modes manual] |
− | for a complete list | + | 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 | Perhaps the most useful system modes are <code>*mkii</code> and <code>*mkiv</code> which | ||
− | determine whether | + | determine whether MKII or MKIV is being used. These modes are handy when you |
− | want different setups for | + | want different setups for MKII and MKIV. |
Other modes are useful for very specific situations. Some of these are described | Other modes are useful for very specific situations. Some of these are described | ||
below. | below. | ||
− | A document multiple times to get the cross referencing, table of contents, etc. | + | |
− | right. However, sometimes you need to do some external processing ( | + | {|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;" |
− | conversion) that only needs to be done | + | | '''*first''' |
− | <code>*first</code> mode is handy | + | | Enabled during the first compile run |
+ | |} | ||
+ | |||
+ | 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 (e.g., graphic | ||
+ | conversion) that only needs to be done once. In such cases, the | ||
+ | <code>*first</code> mode is handy—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 | You can use the project-product-component structure for managing large projects | ||
like a book series. See [[Project structure]] | like a book series. See [[Project structure]] | ||
− | for details. | + | for details of this approach. A product or its components may be compiled |
− | + | separately, and you may want to do something different when a product is | |
− | + | compiled or when a component is compiled. To do so, you need to check for | |
− | + | modes <code>*project</code>, <code>*product</code>, <code>*component</code>, and | |
− | + | <code>*environment</code>; these modes are set when the corresponding structure file | |
− | product file is read | + | is processed. For example, the <code>*product</code> mode is set whenever a |
− | encountered | + | product file is read; more specifically, when <code>\startproduct</code> is |
− | <code>\starttext</code> is encountered. | + | encountered. Similarly, a mode <code>*text</code> is enabled when |
+ | <code>\starttext</code> is encountered, and likewise for the others. | ||
− | A large document is | + | |
+ | |||
+ | {|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: | ||
frontmatter, bodymatter, appendices, and backmatter. Internally, these section | frontmatter, bodymatter, appendices, and backmatter. Internally, these section | ||
− | blocks are | + | blocks are referred to as <code>frontpart</code>, <code>bodypart</code>, <code>appendix</code>, and |
− | <code> | + | <code>backpart</code>. Each section block sets a system mode with the same name. So, |
− | if you want macros that work differently | + | if you want macros that work differently in different section blocks, you can |
− | check for <code>* | + | check for modes <code>*frontpart</code>, <code>*bodypart</code>, and so on. |
− | <code>* | + | |
+ | |||
+ | |||
+ | {|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. | ||
+ | |||
+ | |||
− | ConTeXt provides support for multiple | + | {|cellpadding="5" style="border-collapse: collapse;border-width: 1px; border-style: solid;" |
− | their IETF language tags, like <code>en-us</code> for US English, <code>en-gb</code> | + | |'''*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 | ||
+ | their 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 | for British English, <code>nl</code> for Dutch, <code>de</code> for German, etc. A document | ||
− | has a main language set | + | has a main language, set with the command <code>\mainlanguage[...]</code>, |
+ | that is used for | ||
translated labels like <em>chapter</em> and <em>figure</em>. You can also switch the | 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. | current language using <code>\language[...]</code> to change the hyphenation rules. | ||
− | Whenever a language is chosen, its | + | Whenever a language is chosen, its identifier is set as a mode. The mode for the main |
− | language starts with two <code>*</code>. For example, when the main language is US | + | language 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 | 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>). | <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 | ||
+ | |- | ||
+ | |'''*singlesided''' | ||
+ | | Enabled when singlesided layout is enabled | ||
+ | |- | ||
+ | |'''*doublesided''' | ||
+ | | Enabled when doublesided layout 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, |
− | + | <code>*interaction</code> is set when interaction is enabled, <code>*grid</code> | |
− | </ | + | is set when grid typesetting is enabled, and <code>*pdf</code> and <code>*dvi</code> |
+ | are set when the output is PDF or DVI. Others | ||
+ | are too esoteric to describe here. If you are interested, see the | ||
+ | modes manual mentioned earlier. | ||
− | + | = Specific Examples = | |
− | |||
− | |||
− | + | == Different fonts == | |
− | |||
− | + | Suppose you want to generate two versions of a document, one with times font and one with palatino. One way to do this is as follows: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<texcode> | <texcode> | ||
\startmode[palatino] | \startmode[palatino] | ||
− | |||
− | |||
\setupbodyfont[palatino,12pt] | \setupbodyfont[palatino,12pt] | ||
\stopmode | \stopmode | ||
\startmode[times] | \startmode[times] | ||
− | |||
− | |||
\setupbodyfont[postscript,12pt] | \setupbodyfont[postscript,12pt] | ||
\stopmode | \stopmode | ||
Line 254: | Line 404: | ||
\starttext | \starttext | ||
\input knuth | \input knuth | ||
− | |||
\stoptext | \stoptext | ||
</texcode> | </texcode> | ||
+ | and run with one of the following: | ||
− | + | context --mode=palatino filename | |
− | + | context --mode=times filename | |
− | |||
− | |||
− | |||
− | + | == Running external commands once == | |
− | |||
− | |||
− | == | ||
− | |||
− | |||
+ | 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> | <texcode> | ||
− | \ | + | \startmode[*first] |
− | % external program which creates a file | + | % external program which creates a file fig-1.ps |
\executesystemcommand{some_external_program ...} | \executesystemcommand{some_external_program ...} | ||
% convert PS into PDF | % convert PS into PDF | ||
− | \executesystemcommand{ | + | \executesystemcommand{pstopdf fig-1.ps} |
− | + | \stopmode | |
% include the resulting PDF | % include the resulting PDF | ||
− | \externalfigure[ | + | \externalfigure[fig-1] |
</texcode> | </texcode> | ||
− | + | [[Category:Tools]] | |
− | [[Category: |
Latest revision as of 08:26, 2 July 2022
Very often, you want to generate multiple versions of the same document:
one version for printing and one for viewing on the screen, one version for
students and one version for the instructor, and so on. You can do this in a
simple but naive way: create different files set up for the different versions and
\input
the common material, or create some new conditional flags using
\newif
and set them appropriately for conditional processing. Or you
could use modes—the ConTeXt way of doing conditional processing.
Introduction
A mode is similar to a conditional flag, but with a few advantages: new modes
need not be explicitly defined (no need for something like \newif
),
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. As a result, multiple versions of a document can be generated without changing the source file.
The name or identifier of a mode can be any combination of letters, digits, or
spaces. Names starting with *
are reserved for system modes.
Setting modes
ConTeXt has three commands for setting modes:
\enablemode [...]
\disablemode[...]
\preventmode[...]
The names are self-descriptive. \enablemode activates a mode,
\disablemode deactivates a mode, and \preventmode permanently
deactivates a mode. All three commands take a list of modes as an argument. For
example, to active modes named screen
and solution
use:
\enablemode[screen,solution]
Modes can also be activated by a command line switch --modes
to
context
. For example, another way to activate the screen
and
solution
modes, to run ConTeXt using one of:
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). To speed up the processing, a mode can be defined before being used:
\definemode[...][...]
The first argument is a list of modes; the second argument may be yes
, no
, or keep
. For example,
\definemode[screen][yes]
defines a mode and enables it;\definemode[screen][no]
defines a mode and disables it;\definemode[screen][keep]
defines a mode and keeps its previous status.
Typically, it is better to use \definemode[...][keep]
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 \startmode
and
\startnotmode
environments. Their use is best explained by an example.
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 set the default paper size for print and change it in screen mode:
\setuppapersize[letter][letter] \startmode[screen] \setuppapersize[S6][S6] \stopmode
(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, you could set a default paper size for the screen and change it if screen mode is not enabled:
\setuppapersize[S6][S6] \startnotmode[screen] \setuppapersize[letter][letter] \stopnotmode
\startmode
(and \startnotmode
) checks the value of the mode at the time it is executed. This is important when you are setting the modes using \enablemode
or \disablemode
. For example,
\enablemode[foo] \startmode[foo] ... \stopmode
the contents of the mode environment are executed because foo
is enabled when \startmode
is encountered. However, in
\startmode[foo] ... \stopmode \enablemode[foo]
the contents of the mode environment are not executed because foo
is not enabled when \startmode
is encountered.
Checking for multiple modes (or
/and
statements for modes)
\startmode
and \startnotmode
can check for multiple modes,
by giving a list of modes as their arguments. \startmode
processes its contents (everything until the next
\stopmode
, thus \startmode
cannot be
nested.) if any of the modes are enabled, otherwise (i.e., when all
the modes are disabled) \startmode
ignores its
contents. The opposite is \startnotmode
: it processes its
contents (everything until the next \stopnotmode
) if any of the
modes are disabled, otherwise—when all the modes are enabled—the contents are ignored.
\startmode
and \startnotmode
are "or" environments. They
process their contents if any of the modes satisfy the required condition. Their
"and" counterparts are also available: \startallmodes
and \startnotallmodes
process their contents only if all the given modes satisfy the required
condition. For example, suppose you want to enable interaction (e.g., hyperlinks)
only when both screen
and solution
modes are enabled. Then you can
use:
\startallmodes[screen,solution] \setupinteraction[state=start] \stopallmodes
To summarize, the four start-stop environments for checking modes are:
\startmode[mode1, mode2, ...] % Processed if any of the modes is enabled \stopmode \startnotmode[mode1, mode2, ...] % Processed if any of the modes is disabled \stopnotmode \startallmodes[mode1, mode2, ...] % Processed if all the modes are enabled \stopallmodes \startnotallmodes[mode1, mode2, ...] % Processed if all the modes are disabled \stopnotallmodes
These environments have \doif...
alternatives that are useful for short
setups. Also, they can be nested.
\doifmode {mode1, mode2, ...} {Processed if any mode is enabled} \doifnotmode {mode1, mode2, ...} {Processed if any mode is disabled} \doifallmodes {mode1, mode2, ...} {Processed if all modes are enabled} \doifnotallmodes {mode1, mode2, ...} {Processed if all modes are disabled}
The logic for determining when the content is processed is exactly the same as
for the start
-stop
commands.
These \doif
commands each have a variant to process alternative code if
the conditions are not satisfied (like the \else
branch of \if
).
\doifmodeelse {mode1, mode2, ...} {Processed if any mode is enabled} {else this is processed} \doifallmodeselse {mode1, mode2, ...} {Processed if all modes are enabled} {else this is processed} \doifnotallmodeselse {mode1, mode2, ...} {Processed if all modes are disabled} {else this is processed}
Note that there is no command \doifnotmodeelse
because there is no need for it; \doifmodeelse
may be used for the same effect (with the if
and else
branches switched).
Checking multiple modes in parallel (case
statement for modes)
In addition to the above "or" and "and" environment which check modes is sequence, you can also check multiple modes in parallel. The syntax for such a "case" environment is as follows:
\startmodeset % NOTE: Empty lines are not allowed here! [mode1, mode2, ...] {Processed if either mode is enabled} [mode3, mode4, ...] {Processed if either mode is enabled} [default] {Processed if none of the above modes match} \stopmodeset
The same mode can be referenced multiple times, and all matching branches are executed. The \startmodeset ... \stopmodeset environments can be nested. So, you can use
\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
Checking modes in Lua
The state of any mode is accessible at the Lua end as tex.modes
table. Specifically,
tex.modes["screen"]
returns true
if mode screen
is enabled and false
otherwise. Thus, specific combinations of modes can be checked using boolean expressions. For example
if (tex.modes["mode1"] and tex.modes["mode2"]) then ... end
checks if both mode1
and mode2
are enabled.
System modes
Besides allowing user-definable modes, ConTeXt provides some system
modes. These modes start with a *
character. Here
only the more commonly used system modes are explained; see the ConTeXt modes manual
for a complete list.
*mkii | Enabled when running MkII |
*mkiv | Enabled when running MkIV |
Perhaps the most useful system modes are *mkii
and *mkiv
which
determine whether MKII or MKIV is being used. These modes are handy when you
want different setups for MKII and MKIV.
Other modes are useful for very specific situations. Some of these are described below.
*first | Enabled during the first compile run |
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 (e.g., graphic
conversion) that only needs to be done once. In such cases, the
*first
mode is handy—it is active only on the first run of the
document.
*export | Enabled when \setupbackend[export=yes] is set
|
You may want to use different images for XML [Export]. The *export
mode is useful in such cases.
*project | Enabled when inside \startproject ... \stopproject
|
*component | Enabled when inside \startcomponent ...\stopcomponent
|
*environment | Enabled when inside \startenvironment ... \stopenvironment
|
*text | Enabled when inside \starttext ... \stoptext .
|
You can use the project-product-component structure for managing large projects
like a book series. See Project structure
for details of this approach. A product or its components may be compiled
separately, and you may want to do something different when a product is
compiled or when a component is compiled. To do so, you need to check for
modes *project
, *product
, *component
, and
*environment
; these modes are set when the corresponding structure file
is processed. For example, the *product
mode is set whenever a
product file is read; more specifically, when \startproduct
is
encountered. Similarly, a mode *text
is enabled when
\starttext
is encountered, and likewise for the others.
*frontpart | Enabled when inside \startfrontmatter ... \stopfrontmatter
|
*bodypart | Enabled when inside \startbodymatter ... \stopbodymatter
|
*backpart | Enabled when inside \startbackmatter ... \stopbackmatter
|
A large document is typically broken down into different section blocks:
frontmatter, bodymatter, appendices, and backmatter. Internally, these section
blocks are referred to as frontpart
, bodypart
, appendix
, and
backpart
. Each section block sets a system mode with the same name. So,
if you want macros that work differently in different section blocks, you can
check for modes *frontpart
, *bodypart
, and so on.
*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 *chapter
, *section
, *subsection
, etc. Similarly, *list
is enabled inside a list, *marking
is enabled inside a marking, and *register
is enabled inside a register.
*en-us, *nl, etc. | Enabled when the current \language is en-us , nl , etc.
|
**en-us, **nl, etc. | Enabled when the \mainlanguage is en-us , nl , etc.
|
ConTeXt provides support for multiple languages. Languages are recognized by
their IETF language tags, like en-us
for US
English, en-gb
for British English, nl
for Dutch, de
for German, etc. A document
has a main language, set with the command \mainlanguage[...]
,
that is used for
translated labels like chapter and figure. You can also switch the
current language using \language[...]
to change the hyphenation rules.
Whenever a language is chosen, its identifier is set as a mode. The mode for the main
language starts with two *
. For example, when the main language
is US
English and the current language is Dutch, the modes **en-us
and
*nl
are set (notice the extra *
in **en-us
).
*figure | Enabled when a graphic is found |
*interaction | Enabled when interaction is enabled |
*grid | Enabled when grid typesetting is enabled |
*singlesided | Enabled when singlesided layout is enabled |
*doublesided | Enabled when doublesided layout is enabled |
Enabled when the main output is pdf | |
*dvi | Enabled when the main output is dvi |
Other system modes: *figure
is set when a graphic is found,
*interaction
is set when interaction is enabled, *grid
is set when grid typesetting is enabled, and *pdf
and *dvi
are set when the output is PDF or DVI. Others
are too esoteric to describe here. If you are interested, see the
modes manual mentioned earlier.
Specific Examples
Different fonts
Suppose you want to generate two versions of a document, one with times font and one with palatino. One way to do this is as follows:
\startmode[palatino] \setupbodyfont[palatino,12pt] \stopmode \startmode[times] \setupbodyfont[postscript,12pt] \stopmode \starttext \input knuth \stoptext
and run with one of the following:
context --mode=palatino filename context --mode=times filename
Running external commands once
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:
\startmode[*first] % external program which creates a file fig-1.ps \executesystemcommand{some_external_program ...} % convert PS into PDF \executesystemcommand{pstopdf fig-1.ps} \stopmode % include the resulting PDF \externalfigure[fig-1]