6.3. Examples

An example is a figure element which has a class attribute containing "role-example". This kind of figure is listed in the "List of Examples" (that is, book element lox) only if it also has a figcaption child element. Example:

1
2
3
4
5
6
7
8
9
10
11
<figure class="role-example">
  <figcaption>"Hello World" program in the C language</figcaption>
  <pre>/* Hello World */
#include &lt;stdio.h&ght;

int main()
{
    printf("Hello World\n");
    return 0;
}</pre>
</figure>

is rendered as:

Example 6-1. "Hello World" program in the C language
/* Hello World */
#include <stdio.h>

int main()
{
    printf("Hello World\n");
    return 0;
}

§ Program listings

A program listing can have its lines automatically numbered and/or can feature syntax highlighting. This is done by adding "role-listing-NUMBER-LANGUAGE-tabWIDTH" to the class attribute of a pre element. Options NUMBER, LANGUAGE, tabWIDTH, may be specified in any order. Moreover some or all of these options may be omitted.

  • NUMBER, a strictly positive integer, specifies the number of the first line of the program listing. This option may be omitted if you don't want automatic line numbering.
  • LANGUAGE, one of (case-insensitive): "bourne" (or "shell" or "sh"), "c", "cmake" (or "make" or "makefile"), "cpp", "csharp", "css21" (or "css"), "delphi", "ini", "java", "javascript", "lua", "m2" (Modula 2), "perl", "php", "python", "ruby", "sql1999", "sql2003", "sql92" (or "sql"), "tcl", "upc" (Unified Parallel C), "html", "xml", specifies the programming language or markup language used in the program listing. This option may be omitted if you don't want syntax highlighting.
  • tabWIDTH where WIDTH is a positive integer, specifies whether tab characters should be expanded to a number of space characters. WIDTH is the maximum number of space characters for an expanded tab character, hence this value specifies the location of “tab stops”. Example: <pre class="role-listing-1-java-tab4"> means expand tabs to up to 4 space characters in this line-numbered Java listing. Other example: <pre class="role-listing-tab0-shell"> means: do not replace tabs in this Bourne shell listing. When tabWIDTH is omitted, it is equivalent to having an implicit tab8.

Example 1 (in the following C program, lines are indented using tab characters):

1
2
3
4
5
6
7
8
<pre class="role-listing-1-c-tab4">/* Hello World */
#include &lt;stdio.h&ght;

int main()
{
        printf("Hello World\n");
        return 0;
}</pre>

is rendered as:

1
2
3
4
5
6
7
8
/* Hello World */
#include <stdio.h>

int main()
{
    printf("Hello World\n");
    return 0;
}
Superfluous indentation is removed too

Note that in addition to replacing tab characters by a number of space characters, the tabWIDTH facility also removes the space characters which are common to the beginning of all text lines. That is, it removes the superfluous “indentation” in the program listing, if any.

Moreover, the tabWIDTH facility also removes the (useless) space characters found just before a newline character.

See example 2 below in which the indentation is automatically removed.

Example 2 (implicit role-listing-1-tab8; first line "    /tmp/" starts with 4 space characters):

1
2
3
4
5
6
7
8
9
10
11
<pre class="role-listing-1">    /tmp/
    /usr/                            
        bin/
        lib/
        <b>local/</b>
                <b>bin/</b>
                <b>lib/</b>
                <b>src/</b>
        src/
    /var/                            
</pre>

is rendered as:

1
2
3
4
5
6
7
8
9
10
11
/tmp/
/usr/
    bin/
    lib/
    local/
            bin/
            lib/
            src/
    src/
/var/