For best results, figures, tables and code blocks should get
captions and they should be referenced with and :xref:
for
figures/tables/code-blocks (:sref: for sections). :xref:
and
:sref:
are defined as :numref:
, when appropriate, otherwise
as :ref:
. Do not use the image
directive to keep figures in
place.
Label prefixes allow using the same caption for different entities, e.g., listing and figure (see section 4.1.1, Directive figure).
Emacs support functions for generating labels and their shortcuts are shown in table 4.1.
shortcut | command |
---|---|
C-c r l s | M-x sdx-make-section-label |
C-c r l f | M-x sdx-make-figure-label |
C-c r l t | M-x sdx-make-table-label |
C-c r l l | M-x sdx-make-listing-label |
C-c r l c | M-x sdx-make-caption-label |
C-c r l r | M-x sdx-make-figctr-label |
Sphinx allows certain entities to be referenced by number (see figure 4.1).
See :xref:`fig:UML test` in :sref:`sec:Directive uml`.
renders as:
See figure 4.3 in section 4.1.4, Directive uml.
This requires the option numfig
to be set to True in
conf.py
as shown in listing 4.1.
1 2 3 4 5 6 7 | numfig = True
numfig_format = {
'section': 'section {number}, {name}',
'figure': 'figure %s',
'table': 'table %s',
'code-block': 'listing %s',
}
|
The supported entities are:
As documented in reStructuredText Directives, figure. See listing 4.2) and figure 4.2).
1 2 3 4 | .. _`fig:Figure example Magrittes Pipe`:
.. figure:: _static/MagrittePipe.jpg
Figure example Magritte's Pipe
|
The body must contain a table, however, a single table cell (rectangle) is sufficient (see listing 4.3 and table 4.2).
1 2 3 4 5 6 7 8 9 10 | .. _`tab:single table cell rectangle`:
.. table:: single table cell (rectangle)
+-----------------------+
| .. code-block:: text |
| |
| code-block without |
| caption inside a |
| single table cell |
+-----------------------+
|
code-block without
caption inside a
single table cell
|
code-block directives with a :caption: option can be referenced by number, e.g., “See :xref:`lst:Definition of a general code-block listing`”: renders as “See listing 4.4”.
1 2 3 4 5 6 | .. _`lst:<LABEL>`:
.. code-block:: <LANGUAGE>
:caption: <TITLE>
:linenos:
<BODY>
|
An example is shown in listing 4.5.
1 2 3 4 5 6 7 8 | .. _`lst:Some Emacs Lisp code`:
.. code-block:: elisp
:caption: Some Emacs Lisp code
:linenos:
(if (not nil)
(let (x) (princ x))
(let(y) princ y))
|
The rendered example is shown in listing 4.6.
1 2 3 | (if (not nil)
(let (x) (princ x))
(let(y) princ y))
|
The uml directive for plantuml(1) diagrams also works as figure, as shown in listing 4.7 and figure 4.3.
1 2 3 4 5 6 7 8 | .. _`fig:UML test`:
.. uml::
:caption: UML test
@startuml
A --> B : send
A <-- B : receive
@enduml
|
Obviously, the graphviz directive for dot(1) diagrams also works as figure, as shown in listing 4.8 and figure 4.4.
1 2 3 4 5 6 7 | .. _`fig:dot test`:
.. graphviz::
:caption: dot test
digraph xx {
A -> B;
}
|
Since single cell tables end up in the table namespace, the extension
ws_figure_container provides the figctr
directive, which works
like a container in the figure namespace (see listing 4.9 and figure 4.5).
.. _`fig:Fig Ctr Caption`:
.. figctr:: Fig Ctr Caption
Arbitrary things like this:
.. code-block:: sh
if test -r "file"
then
cat "file"
fi
Arbitrary things like this:
if test -r "file"
then
cat "file"
fi
Before a README is done, it is useful to check, if every figure has a caption, directive and a reference.
figure-regexp:
^ *\([.][.] *\(\(table\|figure\|uml\|graphviz\|graph\|digraph\)::\|_`\(tab\|fig\):\)\|:caption:\)\|:xref: figure-grep-cmd:: /bin/grep --color -nH README-fillme.txt -e '{figure-regexp}' Search with - :kbd:`M-x occur RET {figure-regexp} RET` - :kbd:`M-x grep RET {figure-grep-cmd} RET` - For creating figure labels, use emacs commands - :kbd:`C-c r l c`, :kbd:`M-x sdx-make-caption-label` - :kbd:`C-c r l f`, :kbd:`M-x sdx-make-figure-label` - :kbd:`C-c r l t`, :kbd:`M-x sdx-make-table-label` See :xref:`fig:complete figure requirements` .. _`fig:complete figure requirements`: .. uml:: :caption: complete figure requirements @startuml package "complete figure requirements" { start floating note right figure requirements: * caption * directive * reference end note :grep for uml/table (figure-regexp); while (for each matched directive line) is (do) while (caption, label or reference is missing?) if (caption is missing?) then (yes) :* add caption * correct label, if present * correct reference, if present; elseif (label is missing?) then (yes) :* move before figure * add label with `C-c r l [cft]` * correct reference, if present; else (reference\nis missing) :add reference; endif end while end while stop } @enduml
Do not inflate the use of section labels! if a section is not referenced, it should not have a label.
Why would want write the section title three times?
The correct template would be:
:xref:`sec:::fillme::`
.. _`sec:::fillme::`:
================
:sec: ::fillme::
================
Filling in the section title must be done anyway:
Alternative 1: Copy title and replace label fillme
Minimum keystrokes: 8, Maximum keystrokes: aleph_0
Alternative 2: Write title and generate label with emacs function (C-c r l s)
Minimum keystrokes: 5, Maximum keystrokes: 5