$$ \newcommand{\beqa}{\begin{eqnarray}} \newcommand{\eeqa}{\end{eqnarray}} \newcommand{\ep}{\thinspace . } \newcommand{\uvec}{\vec u} \newcommand{\Q}{\pmb{Q}} $$

 

 

 

Admonitions

DocOnce offers strong support for admonition environments, such as warning boxes, notification boxes, question boxes, and summary boxes. The boxes normally have an icon, a heading, and may also have a background color. A special box, the block, has never any icon and can be used when an icon would be disturbing or misleading. The variety of admonitions and their many typesetting styles are available through a demo.

Examples on Admonition Types

The following admonition environments are available: block, warning, notice, question, and summary. The box is defined by begin and end tags such as !bnotice and !enotice. The title of the box is fully customizable.

Here are a few examples:

!bwarning
Here is a warning!
!ewarning

!bnotice
This is a notice box with default title.
!enotice

!bnotice Hint
A *Hint* or *Tip* box can use the `notice` box.
!enotice

!bquestion
How many admonition types are there in DocOnce?
!equestion

!bblock This is a block.
A block has never any icon. A block has never any icon,
but may feature a title. It is often used in slides.
!eblock

!bnotice Going deeper
This is text meant to provide more details. The box has the
layout of the notice box, but a custom title, here *Going deeper*.
!enotice

Finally some summary:

!bsummary
The main message is to utilize the admonition styles for
marking different parts of the text
!esummary

The above DocOnce code is in the present format rendered as

Warning. Here is a warning!

Notice. This is a notice box with default title.

Hint. A Hint or Tip box can, e.g., use the notice box.

Question. How many admonition types are there in DocOnce?

This is a block. A block has never any icon. A block has never any icon, but may feature a title. It is often used in slides.

Going deeper. This is text meant to provide more details. The box has the layout of the notice box, but a custom title, here Going deeper.

Finally some summary:

Summary. The main message is to utilize the admonition styles for marking different parts of the text

The layout of admonitions depend on the format. In the rst and sphinx formats one applies the native admonitions, but in sphinx the automake_sphinx.py script manipulates the HTML file to set a gray background for all admonitions. In html, latex and pdflatex there is quite some functionality to control the layout of admonitions.

Layout of admonitions in HTML

The command-line argument --html_admon sets the admonition style for the html format (see demo):

The options --html_admon_bg_color=... and --html_admon_bd_color=... can be used to override the default background and boundary frame colors of the admon styles (respectively). These options have only effect for the apricot, yellow, and gray styles.

Some recommended combinations for admonitions in HTML are

Layout of admonitions in LaTeX

In latex and pdflatex, the type of admonition style is set by the command-line option --latex_admon=. Several values are available (see demo):

There is also an option --latex_admon_color=... that can be used to override the default color. Values are either saturated colors like gray!10 or an RGB tuple 0.95,0.91,0.97. The chosen color replaces all default colors for all admon styles except paragraph. For example, an oval gray box is produced by --latex_admon=mdfbox and '--latex_admon_color=gray!10' (note the quotes: they are necessary to avoid a Bash error due to the exclamation mark in the color specification).

The mdfbox admonition styles has boxes with many possibilities for customization by editing the .tex file. For example, linecolor sets the color of the frame border and frametitlebackgroundcolor sets the background color of the title area. A dark blue frame and a light blue background for the title is produced by this automatic edit:

doconce replace 'linecolor=black,' 'linecolor=darkblue,' mydoc.tex
doconce subst 'frametitlebackgroundcolor=.*?,' 'frametitlebackgroundcolor=blue!5 mydoc.tex

Admonition titles without a period, exclamation mark, or colon at the end will by default get a period at the end. For some LaTeX admonitions (e.g., mdfbox), it may be natural to view the title with a heading without any period. The command-line option --latex_admon_title_no_period avoids appending a period.

LaTeX code environments inside admonitions

Sometimes one sets a special background color in the admonitions, and colored code blocks, which look fine in the running text, may then have inappropriate colors inside admonitions. The option --latex_admon_envir_map=... is used to map an environment (usually for code) to a new style inside admonitions. Specifying a number, say 2, as in --latex_admon_envir_map=2, appends the number to all environments inside admonitions, so !bc pycod becomes effectively !bc pycod2. One can then in doconce ptex2tex (or in the ptex2tex configuration file) specify the typesetting of the pycod2 environment. Otherwise the specification must be a mapping for each envir that should be changed inside the admons:

--latex_admon_envir_map=pycod-pycod_yellow,fpro-fpro2

i.e., a from-to, from-to type of syntax. In this particular example, the !bc pycod environment becomes !bc pycod_yellow and !bc fpro becomes !bc fpro2 inside admonitions.

The Box Environment

A plain box without any title or colored background, just a rectangular frame, is also available. The syntax goes like

!bbox
!bt
\[ \nabla\cdot\boldsymbol{u} = 0  \quad\hbox{(mass balance)}\]
!et
!ebox

resulting in

$$ \nabla\cdot\boldsymbol{u} = 0\quad\hbox{(mass balance)}$$

Admonitions are usually used to typeset something that should be distinct from the running text: a warning, a remark, a notification, a question, a summary, while a box is often used to highlight a key finding in the running text: an equation, a hypothesis, a theorem, a rule, or a conclusion.

User-Defined Environments

LaTeX writers are often fond of their favorite environments and think that formats like DocOnce, Markdown, and HTML are primitive without these favorite environments. DocOnce, however, offers user-defined environments with begin and end tags, as typical in LaTeX. This section explains how to define such user-defined environments.

Remark. Special environments can always be implemented by Mako code as illustrated in the section Example: Defining a Theorem Environment, but the DocOnce user-defined environments are easier to use.

For example, suppose you want to typeset examples using a native example environment in LaTeX, while other formats can simply typeset examples as subsections. You can then introduce your own example environment in DocOnce and write the environment like this:

!bu-example Addition label=ex:math:1p1
We have

!bt
\[ 1 + 1 = 2 \]
!et
|!eu-example

The convention for this type of examples is to have a title, and the title line may feature label=xxx, where we can use xxx as a label for the example. The example environment above is in the ${FORMAT} output format typeset like this:

Example 1: Addition

We have $$ 1 + 1 = 2 $$

The definition of the example environment must be done in a Python module userdef_environments, located in the same directory as the DocOnce source code or the parent directory. In this file (userdef_environments.py), we must provide a dictionary envir2format for translating an environment into the right code for a specific format:

envir2format = {
    'intro': {
        'latex': r"""
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{example}{Example}[section]
""",},
    'example': {
        'latex': example,
        'do': do_example,
        },
}

The intro key contains initializing statements for the LaTeX and HTML formats (the preamble and the head tag, respectively). Here, we need the amsthm package and a definition of the example environment in LaTeX. No initialization is needed for HTML. The other keys are the names of the environments. For each environment, we use a function to format the code for a specific output format. The do key is a default DocOnce formatting, expressed in DocOnce syntax and applied if the output format is not present amont the other keys. The LaTeX output is here defined by the function example:

def example(text, titleline, counter, format):
    """LaTeX typesetting of example environment."""
    label, titleline = get_label(titleline)
    s = r"""
\begin{example}
"""
    if label:
        s += 'label{%s}\n' % label  # no \ (is added by DocOnce)
    s += r"""
\noindent\emph{%s}.

%s
\end{example}
""" % (titleline, text)
    return s

def get_label(titleline):
    label = ''
    if 'label=' in titleline:
        pattern = r'label=([^\s]+)'
        m = re.search(pattern, titleline)
        if m:
            label = m.group(1)
            titleline = re.sub(pattern, '', titleline).strip()
    return label, titleline

The arguments are text for the body of the environment, titleline for the title (everything that comes after !bc-example on the line), an integer counter that counts the number of the current environment (1, 2, and so on, which can be used for reference as an alternative to the label), and the format string holding the user's output format. In the present case, we add the label inside the example environment if we have a label in the title, and we typeset the title in the emphasize font. Otherwise, we rely on the standard example (or newtheorem) LaTeX environment.

In HTML and other formats, we simply typeset the example as a subsection:

def do_example(text, titleline, counter, format):
    """General typesetting of example environment via a section."""
    label, titleline = get_label(titleline)
    s = """

===== Example %d: %s =====
""" % (counter, titleline)
    if label:
        s += 'label{%s}\n' % label
    s += '\n%s\n\n' % text
    return s

We quickly encounter a problem when referring to a specific example. In LaTeX, we want to write Example ref{label}, using the label defined in the example heading. In other formats, this label is a section number, or usually the section (example) heading. Different wording is needed for different formats. This is easiest accomplished by a little Mako function in the top of the DocOnce source:

<%
def refex(label, capital=False):
    if FORMAT in ('latex', 'pdflatex'):
        return 'Example ref{%s}' % label
    else:
        s = 'The ' if capital else 'the '
        s += 'example in Section ref{%s}' % label
        return s
%>

We can then write something like

${refex('ex:test:1p1', capital=True)} demonstrates how to do 1+1.
That is, the calculation 1+1 appears in ${refex('ex:test:1p1')}.

Note that we distinguish between capital=True, which means that the example reference opens a sentence, and capital=False, which refers to a reference later in the sentence. In HTML we see the difference:

The example in the section
<a href="#ex:test:1p1">Example 1: A test function</a>
demonstrates how to do 1+1.
That is, the calculation 1+1 appears in the example in the section
<a href="#ex:test:1p1">Example 1: A test function</a>.

while there is no difference in LaTeX since we refer to Example with capital E anyway:

Example~\ref{ex:test:1p1} demonstrates how to do 1+1.
That is, the calculation 1+1 appears in Example~\ref{ex:test:1p1}.

To summarize, the example environment together with the refex Mako function allows you to work with native LaTeX example environments, while there is a neat alternative solution for all other formats.

You may take a look at a complete userdef_environments.py file to see this example environment and another highligh environment where we define blue boxes in LaTeX and HTML and rely on a standard notice admon for all other formats. The highlight environment is written like

!bu-highlight Highlight box!
This environment is used to highlight something:

!bt
\[ E = mc^2 \]
!et
!eu-highlight

and typeset (in ${FORMAT}) as

Highlight box!
This environment is used to highlight something: $$ E = mc^2 $$

Tip: Test userdef_environments.py! Make sure you run the userdef_environments.py file to check that all syntax is correct:

Terminal> python userdef_environments.py

A common error is to have the envir2format dictionary defined before the functions it refers to.

A complete example showing how one can create tailored environments for computer code, using --latex_code_style, in particular the following one (with a caption),





appears at the end of the document Demonstration of DocOnce support for LaTeX code block environments.

Bibliography (References)

DocOnce applies the software tool Publish to handle the bibliography in a document. With Publish it is easy to import BibTeX data and maintain a database in a clean, self-explanatory textual format. From the Publish format it is easy to go BibTeX and reST or straightforward DocOnce typesetting (and from there to HTML, plain text, wiki formats, and so on).

Installing Publish is trivial: either do

Terminal> pip install -e \
   hg+https://bitbucket.org/logg/publish#egg=publish#egg=publish

or checkout the code on bitbucket.org:

Terminal> hg clone https://bitbucket.org/logg/publish
Terminal> cd publish
Terminal> sudo python setup.py install

Importing your data to the Publish database

Many scientists have their bibliographic data in the BibTex format. Here we assume that you have two files, refs1.bib and refs2.bib. These can be imported to a Publish database, residing in the file papers.pub, by the commands

publish import refs1.bib
publish import refs2.bib

During import, Publish may ask you for accepting the name of new institutions or journals. Publish already have a database of journals and institutions/departments, but when you add new, you also get a file venues.list (in the current working directory) which will be used for future imports in this directory. Make sure you store publish.pub and venues.list along with your DocOnce document files (they do not need to reside in the same directory, but make sure you add them to your version control system).

Importing big BibTeX databases may imply a lot of work. If your BibTeX database does not confirm to the strict BibTeX syntax required by Publish (entry names must be in lower case, their values must be surrounded by curly braces), you will get error messages. Publish will also check the names of all journals and detect duplicate entries. For databases automatically created by some of the modern web tools for references, the BibTeX file may need a lot of edits before it can be accepted by Publish. Consider using a script to automate many of the edits.

Requirements to input data

Notice. Note that Publish only accepts BibTeX files where the keys (author, title, etc.) are in lower case and where the data are enclosed in curly braces. You may need to edit your BibTeX files to meet this demand.

The utility doconce fix_bibtex4publish file.bib fixes several known issues with BibTeX files such that Publish has a better chance of accepting the entries. Run this utility first, then run Publish, respond to any requirements that Publish spits out, remove papers.pub if it exists, and run the import statements again.

Although references are visible as numbers only in the output, it is recommended to have apply a nice, consistent typesetting of your keys. It is suggested to use the following scheme:

Langtangen_2003a          # single author
Langtangen_Pedersen_2002  # two authors
Langtangen_et_al_2002     # three or more authors

One can add a, b, c, and so forth if several keys feature the same authors and year.

Adding new references to the database

When you get some new BibTeX references you simply put them in a file, say refs3.pub and run the publish import refs3.pub command to update the database. You may also consider editing the papers.pub file directly when adding new references.

Exporting the database

Export of everything in the database to BibTeX is done by

publish export mybibtexfile.bib

You can easily export subsets of the database, e.g., only papers associated with a particular author (the Publish manual has details on how this is done). DocOnce will automatically export the database to BibTeX if the output format is latex or pdflatex.

Referring to publications

We use the command

cite{key}

to refer to a publication with bibliographic key key. Here is an example: [3] discussed propagation of large destructive water waves, [4] gave an overview of numerical methods for solving the Navier-Stokes equations, while the use of Backward Kolmogorov equations for analyzing random vibrations was investigated in [5]. The book chapter [6] contains information on C++ software tools for programming multigrid methods. A real retro reference is [7] about a big FORTRAN package. Multiple references are also possible, e.g., see [3] [6].

A LaTeX-style cite command with additional detailed reference is also possible,

cite[details]{key}

for example as in [4] (Section 2) or [4] (Equation (4.2)).

In LaTeX, the cite command is directly translated to the corresponding LaTeX version of the command with a backslash; in reST and Sphinx the citations becomes links, with the citation keys as names; in HTML the citations are numbered from 1, 2, and so forth according to their appearance, and the numbers appear as links; while in other formats the citations are simply the keys inside square brackets and the corresponding references are listed in the order they are cited.

Specifying the Publish database

The specification of the Publish database file in the DocOnce document is done one a line containing BIBFILE: papers.pub (you may give the database file another name and store it in another directory). The references will be inserted at the place where this command appears. Before the command you will often want to have a headline with "References", "Bibliography", or similar. Here is an example:

======= References =======

BIBFILE: papers.pub

In LaTeX and pdfLaTeX the papers.pub file is exported to BibTeX format and included in the document, while in all other formats, suitable text is produced from the database.

LaTeX Bibliography Style

The bibliography style is "plain" in LaTeX output. To change this, just edit the .p.tex file. For example,

doconce format latex mydoc
doconce replace 'bibliographystyle{plain}' 'bibliographystyle{abbrev}' mydoc.p.tex