===== Writing Guidelines (Especially for LaTeX Users!) =====
label{manual:latex:guide}

LaTeX writers often have their own writing habits with use of their
own favorite LaTeX packages. DocOnce is a much simpler format and
corresponds to writing in quite plain LaTeX and making the ascii text
look nice (be careful with the use of white space!). This means that
although DocOnce has borrowed a lot from LaTeX, there are a few points
LaTeX writers should pay attention to. Experience shows that these
points are so important that we list them *before* we list typical
DocOnce syntax!

Any LaTeX syntax in mathematical formulas is accepted when DocOnce
translates the text to LaTeX, but if output in the `sphinx`, `pandoc`,
`mwiki`, `html`, or `ipynb` formats is also important, one should
follow the rules below.

 * AMS LaTeX mathematics is supported, also for the `html`, `sphinx`,
   and `ipynb` formats.
 * If you want LaTeX math blocks to work with `latex`, `html`, `sphinx`,
   `markdown`, and `ipynb`, it is recommended to use only
   the following equation environments: `\[ ... \]`,
   `equation*`, `equation`, `align*`, `align`. `alignat*`, `alignat`.
   Other environments, such as `split`, `multiline`, `gather` are
   supported in modern MathJax in HTML and Sphinx, but may face
   rendering problems (to a larger extent than `equation` and `align`).
   DocOnce performs extensions to `sphinx`, `ipynb`,
   and other formats such that
   labels in `align` and `alignat` environments work well.
   If you face problems with fancy LaTeX equation environments in
   web formats, try rewriting with plain `align`, `nonumber`, etc.
 * Do not use comments inside equations.
 * Newcommands in mathematical formulas are allowed, but not in
   the running text. Newcommands must be defined in files with names
   `newcommands*.tex`. Use `\newcommands` and not `\def`.
   Each newcommand must be defined on a single line.
   Use Mako functions if you need macros in the running text.
 * Use labels and refer to them
   for sections, figures, movies, and equations only.
   MediaWiki (`mwiki`) does not support references to equations.
 * Spaces are not allowed in labels.
 * There is just one `ref` command (no `\eqref` for equations) and references to
   equations must use parentheses. Never use the tilde `~` (non-breaking
   space) character before references to figures, sections, etc., but
   tilde is allowed for references to equations.
 * Never use `\pageref` as pages are not a concept in web documents
   (there is only a `ref` command in DocOnce and it refers to labels).
 * Only figures and movies are floating elements in DocOnce, all other elements
   (code, tables, algorithms) must appear *inline* without numbers or labels
   for reference[^exception] (refer to inline elements by a section label).
   The reason is that floating elements are in general
   not used in web documents, but we made an exception with figures
   and movies.
 * Keep figure captions shorts as they are used as references in the
   Sphinx format. Avoid inline mathematics since Sphinx will strip it
   away in the figure reference.
   (Many writing styles encourage rich captions that
   explains everything about the figure, but this will work well
   only in the HTML and LaTeX formats.)
 * You cannot use `subfigure` to combine several image files in one
   figure, but you can combine the files to one file using
   the `doconce combine_images` tool. Refer to individual image files
   in the caption or text by (e.g.) ``left'' and ``right'', or
   ``upper left'', ``lower right'', etc.
 * Footnotes can be used as usual in LaTeX, but some HTML formats are not
   able to display mathematics or inline verbatim or other formatted
   code (emphasis, boldface, color) in footnotes - have that in mind.
 * Use plain `cite` for references (e.g., `\citeauthor` has no counterpart
   in DocOnce). The bibliography must be prepared in the Publish format,
   but import from (clean) BibTeX is possible.
 * Use `idx` for index entries, but put the definitions between paragraphs,
   not inside them (required by Sphinx).
 * Use the `\bm` command (from the `bm` package, always included by DocOnce)
   for boldface in mathematics.
 * Make sure all ordinary text starts in column 1 on each line. Equations
   can be indented. The `\begin{}` and `\end{}` directives should start
   in column 1.
 * If you depend on various LaTeX environments for your writings, you have
   to give up these, or implement *user-defined environments* in DocOnce.
   For instance, examples are normally typeset as subsections in DocOnce,
   but can also utilize a user-defined example environment.
   Learn about the exercise support in DocOnce for typesetting
   exercises, problems, and projects.
 * Learn about the preprocessors Preprocess and Mako - these are smart
   tools for, e.g., commenting out/in large portions of text and creating
   macros.
 * Use *generalized referenes* when referring to companion documents
   that may later become part of this document (or migrated out of this document).
 * Follow "recommendations for DocOnce books": "https://github.com/hplgit/setup4book-doconce" if you plan to write a book.

[^exception]: There is an exception: by using *user-defined environments*
within `!bu-name` and `!eu-name` directives, it is possible to
label any type of text and refer to it. For example, one can have
environments for examples, tables, code snippets, theorems, lemmas, etc.
One can also use Mako functions to implement enviroments.

!bnotice Use the preprocessor to tailor output
If you really need special LaTeX constructs in the LaTeX output
from DocOnce, you may use use preprocessor if-tests on
the format (typically `#if FORMAT in ("latex", "pdflatex")`) to
include such special LaTeX code. With an else clause you can easily
create corresponding constructions for other formats. This way
of using Preprocess or Mako
allows advanced LaTeX features, or HTML features for the HTML formats,
and thereby fine tuning of the resulting
document. More tuning can be done by automatic editing of the
output file (e.g., `.tex` or `.html`) produced by DocOnce using
your own scripts or the `doconce replace` and `doconce subst` commands.
!enotice

!bnotice Autotranslation of LaTeX to DocOnce?
The tool `doconce latex2doconce` may help you translating LaTeX files
to DocOnce syntax. However, if you use computer code in floating
list environments, special packages for typesetting algorithms,
example environments, `subfigure` in figures,
or a lot of newcommands in the running text, there will be need for
a lot of manual edits and adjustments.

For examples, figure environments can be translated by the program
`doconce latex2doconce` only if the label is inside the caption and
the figure is typeset like

!bc latexcod
\begin{figure}
  \centering
  \includegraphics[width=0.55\linewidth]{figs/myfig.pdf}
  \caption{This is a figure. \labe{myfig}}
\end{figure}
!ec
If the LaTeX is consistent with respect to placement of the label, a
simple script can autoedit the label inside the caption, but many
LaTeX writers put the label at different places in different figures,
and then it becomes more difficult to autoedit figures and translate
them to the DocOnce `FIGURE:` syntax.

Tables are hard to interpret and translate, because the headings and
caption can be typeset in many different ways. The type of table
that is recognized looks like

!bc latexcod
\begin{table}
\caption{Here goes the caption.}
\begin{tabular}{lr}
\hline
\multicolumn{1}{c}{$v_0$} & \multicolumn{1}{c}{$f_R(v_0)$}\\
\hline
1.2 & 4.2\\
1.1 & 4.0\\
0.9 & 3.7
\hline
\end{tabular}
\end{table}
!ec
Recall that table captions do not make sense in DocOnce since tables
must be inlined and explained in the surrounding text.

Footnotes are also problematic for `doconce latex2doconce` since DocOnce
footnotes must have the explanation outside the paragraph where the
footnote is used. This calls for manual work. The translator from
LaTeX to DocOnce will insert `_PROBLEM_` and mark footnotes. One
solution is to avoid footnotes in the LaTeX document if fully automatic
translation is desired.
!enotice
