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.

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/