Prev Up Next
Go backward to 5 Getting started
Go up to Top
Go forward to 7 Carrying on

6 Conditional Compilation: Escaping into one mode

In many situations you want to achieve slightly (or maybe even drastically) different behavior of the LaTeX code and the HTML-output. Hyperlatex offers several different ways of letting your document depend on the mode.

6.1 LaTeX versus Html mode

The easiest way to put a command or text in your document that is only included in one of the two output modes it by using a \texonly or \htmlonly command. They ignore their argument, if in the wrong mode, and otherwise simply expand it:
   We are now in \texonly{\LaTeX}\htmlonly{HTML}-mode.
In cases such as this you can simplify the notation by using the \texorhtml command, which has two arguments:
   We are now in \texorhtml{\LaTeX}{HTML}-mode.

Another possibility is by prefixing a line with \T or \W. \T acts like a comment in HTML-mode, and as a noop in LaTeX-mode, and for \W it is the other way round:

   We are now in
   \T \LaTeX-mode.
   \W HTML-mode.

The last way of achieving this effect is useful when there are large chunks of text that you want to skip in one mode--a HTML-document might skip a section with a detailed mathematical analysis, a LaTeX-document will not contain a node with lots of hyperlinks to other documents. This can be done using the iftex and ifhtml environments:

   We are now in
   \begin{iftex}
     \LaTeX-mode.
   \end{iftex}
   \begin{ifhtml}
     HTML-mode.
   \end{ifhtml}

In LaTeX, commands that are defined inside an enviroment are "forgotten" at the end of the environment. So LaTeX commands defined inside a iftex environment are defined, but then immediately forgotten by LaTeX. A simple trick to avoid this problem is to use the following idiom:

   \W\begin{iftex}
   ... command definitions
   \W\end{iftex}

Now the command definitions are correctly made in the Latex, but not in the Html version.

Instead of the iftex environment, you can also use the tex environment. It is different from iftex only if you have used \NotSpecial in the preamble.

The environment latexonly has been provided as a service to latex2html users. Its effect is the same as iftex.

6.2 Ignoring more input

The contents of the comment environment is ignored.

6.3 Flags -- more on conditional compilation

You can also have sections of your document that are included depending on the setting of a flag:

  \begin{ifset}{flag}
    Flag flag is set!
  \end{ifset}

  \begin{ifclear}{flag}
    Flag flag is not set!
  \end{ifset}
A flag is simply the name of a TeX command. A flag is considered set if the command is defined and its expansion is neither empty nor the single character "0" (zero).

You could for instance select in the preamble which parts of a document you want included (in this example, parts A and D are included in the processed document):

   \newcommand{\IncludePartA}{1}
   \newcommand{\IncludePartB}{0}
   \newcommand{\IncludePartC}{0}
   \newcommand{\IncludePartD}{1}
     ...
   \begin{ifset}{IncludePartA}
     Text of part A
   \end{ifset}
     ...
   \begin{ifset}{IncludePartB}
     Text of part B
   \end{ifset}
     ...
   \begin{ifset}{IncludePartC}
     Text of part C
   \end{ifset}
     ...
   \begin{ifset}{IncludePartD}
     Text of part D
   \end{ifset}
     ...
Note that it is permitted to redefine a flag (using \renewcommand) in the document. That is particularly useful if you use these environments in a macro.


Otfried Cheong, January 5, 2000

Prev Up Next