| 1 |
.. highlight:: rst |
| 2 |
|
| 3 |
========== |
| 4 |
Directives |
| 5 |
========== |
| 6 |
|
| 7 |
:ref:`As previously discussed <rst-directives>`, a directive is a generic block |
| 8 |
of explicit markup. While Docutils provides a number of directives, Sphinx |
| 9 |
provides many more and uses directives as one of the primary extension |
| 10 |
mechanisms. |
| 11 |
|
| 12 |
See :doc:`/usage/domains/index` for roles added by domains. |
| 13 |
|
| 14 |
.. seealso:: |
| 15 |
|
| 16 |
Refer to the :ref:`reStructuredText Primer <rst-directives>` for an overview |
| 17 |
of the directives provided by Docutils. |
| 18 |
|
| 19 |
|
| 20 |
.. _toctree-directive: |
| 21 |
|
| 22 |
Table of contents |
| 23 |
----------------- |
| 24 |
|
| 25 |
.. index:: pair: table of; contents |
| 26 |
|
| 27 |
Since reStructuredText does not have facilities to interconnect several documents, |
| 28 |
or split documents into multiple output files, |
| 29 |
Sphinx uses a custom directive to add relations between |
| 30 |
the single files the documentation is made of, as well as tables of contents. |
| 31 |
The ``toctree`` directive is the central element. |
| 32 |
|
| 33 |
.. note:: |
| 34 |
|
| 35 |
Simple "inclusion" of one file in another can be done with the |
| 36 |
:dudir:`include` directive. |
| 37 |
|
| 38 |
.. note:: |
| 39 |
|
| 40 |
To create table of contents for current document (.rst file), use the |
| 41 |
standard reStructuredText :dudir:`contents directive <table-of-contents>`. |
| 42 |
|
| 43 |
.. rst:directive:: toctree |
| 44 |
|
| 45 |
This directive inserts a "TOC tree" at the current location, using the |
| 46 |
individual TOCs (including "sub-TOC trees") of the documents given in the |
| 47 |
directive body. Relative document names (not beginning with a slash) are |
| 48 |
relative to the document the directive occurs in, absolute names are relative |
| 49 |
to the source directory. A numeric ``maxdepth`` option may be given to |
| 50 |
indicate the depth of the tree; by default, all levels are included. [#]_ |
| 51 |
|
| 52 |
The representation of "TOC tree" is changed in each output format. The |
| 53 |
builders that output multiple files (e.g. HTML) treat it as a collection of |
| 54 |
hyperlinks. On the other hand, the builders that output a single file (e.g. |
| 55 |
LaTeX, man page, etc.) replace it with the content of the documents on the |
| 56 |
TOC tree. |
| 57 |
|
| 58 |
Consider this example (taken from the Python docs' library reference index):: |
| 59 |
|
| 60 |
.. toctree:: |
| 61 |
:maxdepth: 2 |
| 62 |
|
| 63 |
intro |
| 64 |
strings |
| 65 |
datatypes |
| 66 |
numeric |
| 67 |
(many more documents listed here) |
| 68 |
|
| 69 |
This accomplishes two things: |
| 70 |
|
| 71 |
* Tables of contents from all those documents are inserted, with a maximum |
| 72 |
depth of two, that means one nested heading. ``toctree`` directives in |
| 73 |
those documents are also taken into account. |
| 74 |
* Sphinx knows the relative order of the documents ``intro``, |
| 75 |
``strings`` and so forth, and it knows that they are children of the shown |
| 76 |
document, the library index. From this information it generates "next |
| 77 |
chapter", "previous chapter" and "parent chapter" links. |
| 78 |
|
| 79 |
**Entries** |
| 80 |
|
| 81 |
Document titles in the :rst:dir:`toctree` will be automatically read from the |
| 82 |
title of the referenced document. If that isn't what you want, you can |
| 83 |
specify an explicit title and target using a similar syntax to reStructuredText |
| 84 |
hyperlinks (and Sphinx's :ref:`cross-referencing syntax <xref-syntax>`). This |
| 85 |
looks like:: |
| 86 |
|
| 87 |
.. toctree:: |
| 88 |
|
| 89 |
intro |
| 90 |
All about strings <strings> |
| 91 |
datatypes |
| 92 |
|
| 93 |
The second line above will link to the ``strings`` document, but will use the |
| 94 |
title "All about strings" instead of the title of the ``strings`` document. |
| 95 |
|
| 96 |
You can also add external links, by giving an HTTP URL instead of a document |
| 97 |
name. |
| 98 |
|
| 99 |
The special entry name ``self`` stands for the document containing the |
| 100 |
toctree directive. This is useful if you want to generate a "sitemap" from |
| 101 |
the toctree. |
| 102 |
|
| 103 |
In the end, all documents in the :term:`source directory` (or subdirectories) |
| 104 |
must occur in some ``toctree`` directive; Sphinx will emit a warning if it |
| 105 |
finds a file that is not included, because that means that this file will not |
| 106 |
be reachable through standard navigation. |
| 107 |
|
| 108 |
Use :confval:`exclude_patterns` to explicitly exclude documents or |
| 109 |
directories from building completely. Use :ref:`the "orphan" metadata |
| 110 |
<metadata>` to let a document be built, but notify Sphinx that it is not |
| 111 |
reachable via a toctree. |
| 112 |
|
| 113 |
The "root document" (selected by :confval:`root_doc`) is the "root" of the TOC |
| 114 |
tree hierarchy. It can be used as the documentation's main page, or as a |
| 115 |
"full table of contents" if you don't give a ``:maxdepth:`` option. |
| 116 |
|
| 117 |
.. versionchanged:: 0.6 |
| 118 |
Added support for external links and "self" references. |
| 119 |
|
| 120 |
.. rubric:: Options |
| 121 |
|
| 122 |
.. rst:directive:option:: class: class names |
| 123 |
:type: a list of class names, separated by spaces |
| 124 |
|
| 125 |
Assign `class attributes`_. |
| 126 |
This is a :dudir:`common option <common-options>`. |
| 127 |
For example:: |
| 128 |
|
| 129 |
.. toctree:: |
| 130 |
:class: custom-toc |
| 131 |
|
| 132 |
.. _class attributes: https://docutils.sourceforge.io/docs/ref/doctree.html#classes |
| 133 |
|
| 134 |
.. versionadded:: 7.4 |
| 135 |
|
| 136 |
.. rst:directive:option:: name: label |
| 137 |
:type: text |
| 138 |
|
| 139 |
An implicit target name that can be referenced using :rst:role:`ref`. |
| 140 |
This is a :dudir:`common option <common-options>`. |
| 141 |
For example:: |
| 142 |
|
| 143 |
.. toctree:: |
| 144 |
:name: mastertoc |
| 145 |
|
| 146 |
foo |
| 147 |
|
| 148 |
.. versionadded:: 1.3 |
| 149 |
|
| 150 |
.. rst:directive:option:: caption |
| 151 |
:type: text |
| 152 |
|
| 153 |
Add a caption to the toctree. |
| 154 |
For example:: |
| 155 |
|
| 156 |
.. toctree:: |
| 157 |
:caption: Table of Contents |
| 158 |
|
| 159 |
foo |
| 160 |
|
| 161 |
.. versionadded:: 1.3 |
| 162 |
|
| 163 |
.. rst:directive:option:: numbered |
| 164 |
numbered: depth |
| 165 |
|
| 166 |
If you want to have section numbers even in HTML output, |
| 167 |
add the ``:numbered:`` option to the *top-level* toctree. |
| 168 |
For example:: |
| 169 |
|
| 170 |
.. toctree:: |
| 171 |
:numbered: |
| 172 |
|
| 173 |
foo |
| 174 |
bar |
| 175 |
|
| 176 |
Section numbering then starts at the heading of ``foo``. |
| 177 |
Sub-toctrees are automatically numbered |
| 178 |
(don't give the ``numbered`` flag to those). |
| 179 |
|
| 180 |
Numbering up to a specific depth is also possible, |
| 181 |
by giving the depth as a numeric argument to ``numbered``. |
| 182 |
|
| 183 |
.. versionadded:: 0.6 |
| 184 |
|
| 185 |
.. versionchanged:: 1.1 |
| 186 |
Added the numeric *depth* argument. |
| 187 |
|
| 188 |
.. rst:directive:option:: titlesonly |
| 189 |
|
| 190 |
Only list document titles, not other headings of the same level. |
| 191 |
For example:: |
| 192 |
|
| 193 |
.. toctree:: |
| 194 |
:titlesonly: |
| 195 |
|
| 196 |
foo |
| 197 |
bar |
| 198 |
|
| 199 |
.. versionadded:: 1.0 |
| 200 |
|
| 201 |
.. rst:directive:option:: glob |
| 202 |
|
| 203 |
Parse glob wildcards in toctree entries. |
| 204 |
All entries are matched against the list of available documents, |
| 205 |
and matches are inserted into the list alphabetically. |
| 206 |
For example:: |
| 207 |
|
| 208 |
.. toctree:: |
| 209 |
:glob: |
| 210 |
|
| 211 |
intro* |
| 212 |
recipe/* |
| 213 |
* |
| 214 |
|
| 215 |
This includes first all documents whose names start with ``intro``, |
| 216 |
then all documents in the ``recipe`` folder, then all remaining documents |
| 217 |
(except the one containing the directive, of course.) [#]_ |
| 218 |
|
| 219 |
.. versionadded:: 0.3 |
| 220 |
|
| 221 |
.. rst:directive:option:: reversed |
| 222 |
|
| 223 |
Reverse the order of the entries in the list. |
| 224 |
This is particularly useful when using the ``:glob:`` option. |
| 225 |
|
| 226 |
.. versionadded:: 1.5 |
| 227 |
|
| 228 |
.. rst:directive:option:: hidden |
| 229 |
|
| 230 |
A hidden toctree only defines the document hierarchy. |
| 231 |
It will not insert links into the document at the location of the directive. |
| 232 |
|
| 233 |
This makes sense if you have other means of navigation, |
| 234 |
e.g. through manual links, HTML sidebar navigation, |
| 235 |
or if you use the ``:includehidden:`` option on the top-level toctree. |
| 236 |
|
| 237 |
.. versionadded:: 0.6 |
| 238 |
|
| 239 |
.. rst:directive:option:: includehidden |
| 240 |
|
| 241 |
If you want one global table of contents showing the complete document structure, |
| 242 |
you can add the ``:includehidden:`` option to the *top-level* toctree directive. |
| 243 |
All other toctrees on child pages can then be made invisible |
| 244 |
with the ``:hidden:`` option. |
| 245 |
The top-level toctree with ``:includehidden:`` will then include their entries. |
| 246 |
|
| 247 |
.. versionadded:: 1.2 |
| 248 |
|
| 249 |
|
| 250 |
Special names |
| 251 |
^^^^^^^^^^^^^ |
| 252 |
|
| 253 |
.. index:: pair: genindex; toctree |
| 254 |
pair: modindex; toctree |
| 255 |
pair: search; toctree |
| 256 |
|
| 257 |
Sphinx reserves some document names for its own use; you should not try to |
| 258 |
create documents with these names -- it will cause problems. |
| 259 |
|
| 260 |
The special document names (and pages generated for them) are: |
| 261 |
|
| 262 |
* ``genindex`` |
| 263 |
|
| 264 |
This is used for the general index, |
| 265 |
which is populated with entries from :rst:dir:`index` directives |
| 266 |
and all index-generating :ref:`object descriptions <basic-domain-markup>`. |
| 267 |
For example, see Sphinx's :ref:`genindex`. |
| 268 |
|
| 269 |
* ``modindex`` |
| 270 |
|
| 271 |
This is used for the Python module index, |
| 272 |
which contains one entry per :rst:dir:`py:module` directive. |
| 273 |
For example, see Sphinx's :ref:`py-modindex`. |
| 274 |
|
| 275 |
* ``search`` |
| 276 |
|
| 277 |
This is used for the search page, |
| 278 |
which contains a form that uses the generated JSON search index and JavaScript |
| 279 |
to full-text search the generated documents for search words; |
| 280 |
it works on every major browser. |
| 281 |
For example, see Sphinx's :ref:`search`. |
| 282 |
|
| 283 |
* Every name beginning with ``_`` |
| 284 |
|
| 285 |
Though few such names are currently used by Sphinx, |
| 286 |
you should not create documents or document-containing directories with such names. |
| 287 |
(Using ``_`` as a prefix for a custom template directory is fine.) |
| 288 |
|
| 289 |
.. warning:: |
| 290 |
|
| 291 |
Be careful with unusual characters in filenames. |
| 292 |
Some formats may interpret these characters in unexpected ways: |
| 293 |
|
| 294 |
* Do not use the colon ``:`` for HTML based formats. |
| 295 |
Links to other parts may not work. |
| 296 |
* Do not use the plus ``+`` for the ePub format. |
| 297 |
Some resources may not be found. |
| 298 |
|
| 299 |
|
| 300 |
Paragraph-level markup |
| 301 |
---------------------- |
| 302 |
|
| 303 |
These directives create short paragraphs and can be used inside information |
| 304 |
units as well as normal text. |
| 305 |
|
| 306 |
|
| 307 |
Admonitions, messages, and warnings |
| 308 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 309 |
|
| 310 |
.. index:: admonition, admonitions |
| 311 |
pair: attention; admonition |
| 312 |
pair: caution; admonition |
| 313 |
pair: danger; admonition |
| 314 |
pair: error; admonition |
| 315 |
pair: hint; admonition |
| 316 |
pair: important; admonition |
| 317 |
pair: note; admonition |
| 318 |
pair: tip; admonition |
| 319 |
pair: warning; admonition |
| 320 |
|
| 321 |
The admonition directives create 'admonition' elements, |
| 322 |
a standardised system of communicating different types of information, |
| 323 |
from a helpful :rst:dir:`tip` to matters of paramount :rst:dir:`danger`. |
| 324 |
These directives can be used anywhere an ordinary body element can, |
| 325 |
and can contain arbitrary body elements. |
| 326 |
There are nine specific named admonitions |
| 327 |
and the generic :rst:dir:`admonition` directive. |
| 328 |
|
| 329 |
.. rst:directive:: .. attention:: |
| 330 |
|
| 331 |
Information that requires the reader's attention. |
| 332 |
The content of the directive should be written in complete sentences |
| 333 |
and include all appropriate punctuation. |
| 334 |
|
| 335 |
Example: |
| 336 |
|
| 337 |
.. attention:: |
| 338 |
|
| 339 |
Please may I have your attention. |
| 340 |
|
| 341 |
.. rst:directive:: .. caution:: |
| 342 |
|
| 343 |
Information with regard to which the reader should exercise care. |
| 344 |
The content of the directive should be written in complete sentences |
| 345 |
and include all appropriate punctuation. |
| 346 |
|
| 347 |
Example: |
| 348 |
|
| 349 |
.. caution:: |
| 350 |
|
| 351 |
Exercise due caution. |
| 352 |
|
| 353 |
.. rst:directive:: .. danger:: |
| 354 |
|
| 355 |
Information which may lead to near and present danger if not heeded. |
| 356 |
The content of the directive should be written in complete sentences |
| 357 |
and include all appropriate punctuation. |
| 358 |
|
| 359 |
Example: |
| 360 |
|
| 361 |
.. danger:: |
| 362 |
|
| 363 |
Let none think to fly the danger for soon or late love is his own avenger. |
| 364 |
|
| 365 |
.. rst:directive:: .. error:: |
| 366 |
|
| 367 |
Information relating to failure modes of some description. |
| 368 |
The content of the directive should be written in complete sentences |
| 369 |
and include all appropriate punctuation. |
| 370 |
|
| 371 |
Example: |
| 372 |
|
| 373 |
.. error:: |
| 374 |
|
| 375 |
ERROR 418: I'm a teapot. |
| 376 |
|
| 377 |
.. rst:directive:: .. hint:: |
| 378 |
|
| 379 |
Information that is helpful to the reader. |
| 380 |
The content of the directive should be written in complete sentences |
| 381 |
and include all appropriate punctuation. |
| 382 |
|
| 383 |
Example: |
| 384 |
|
| 385 |
.. hint:: |
| 386 |
|
| 387 |
Look under the flowerpot. |
| 388 |
|
| 389 |
.. rst:directive:: .. important:: |
| 390 |
|
| 391 |
Information that is of paramount importance |
| 392 |
and which the reader must not ignore. |
| 393 |
The content of the directive should be written in complete sentences |
| 394 |
and include all appropriate punctuation. |
| 395 |
|
| 396 |
Example: |
| 397 |
|
| 398 |
.. important:: |
| 399 |
|
| 400 |
This is a statement of paramount importance. |
| 401 |
|
| 402 |
.. rst:directive:: .. note:: |
| 403 |
|
| 404 |
An especially important bit of information that the reader should know. |
| 405 |
The content of the directive should be written in complete sentences |
| 406 |
and include all appropriate punctuation. |
| 407 |
|
| 408 |
Example: |
| 409 |
|
| 410 |
.. note:: |
| 411 |
|
| 412 |
This function is not suitable for sending tins of spam. |
| 413 |
|
| 414 |
.. rst:directive:: .. tip:: |
| 415 |
|
| 416 |
Some useful tidbit of information for the reader. |
| 417 |
The content of the directive should be written in complete sentences |
| 418 |
and include all appropriate punctuation. |
| 419 |
|
| 420 |
Example: |
| 421 |
|
| 422 |
.. tip:: |
| 423 |
|
| 424 |
Remember your sun cream! |
| 425 |
|
| 426 |
.. rst:directive:: .. warning:: |
| 427 |
|
| 428 |
An important bit of information that the reader should be very aware of. |
| 429 |
The content of the directive should be written in complete sentences |
| 430 |
and include all appropriate punctuation. |
| 431 |
|
| 432 |
Example: |
| 433 |
|
| 434 |
.. warning:: |
| 435 |
|
| 436 |
Beware of the dog. |
| 437 |
|
| 438 |
.. rst:directive:: .. admonition:: title |
| 439 |
|
| 440 |
A generic admonition, with an optional title. |
| 441 |
The content of the directive should be written in complete sentences |
| 442 |
and include all appropriate punctuation. |
| 443 |
|
| 444 |
Example: |
| 445 |
|
| 446 |
.. admonition:: This is a title |
| 447 |
|
| 448 |
This is the content of the admonition. |
| 449 |
|
| 450 |
|
| 451 |
.. rst:directive:: seealso |
| 452 |
|
| 453 |
Many sections include a list of references to module documentation or |
| 454 |
external documents. |
| 455 |
These lists are created using the :rst:dir:`seealso` directive. |
| 456 |
|
| 457 |
The :rst:dir:`!seealso` directive is typically placed in a section |
| 458 |
just before any subsections. |
| 459 |
The content of the :rst:dir:`seealso` directive should be |
| 460 |
either a single line or a reStructuredText `definition list`_. |
| 461 |
|
| 462 |
.. _definition list: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#definition-lists |
| 463 |
|
| 464 |
Example:: |
| 465 |
|
| 466 |
.. seealso:: |
| 467 |
|
| 468 |
Python's :py:mod:`zipfile` module |
| 469 |
Documentation of Python's standard :py:mod:`zipfile` module. |
| 470 |
|
| 471 |
`GNU tar manual, Basic Tar Format <https://example.org>`_ |
| 472 |
Documentation for tar archive files, including GNU tar extensions. |
| 473 |
|
| 474 |
.. seealso:: |
| 475 |
|
| 476 |
Python's :py:mod:`zipfile` module |
| 477 |
Documentation of Python's standard :py:mod:`zipfile` module. |
| 478 |
|
| 479 |
`GNU tar manual, Basic Tar Format <https://example.org>`_ |
| 480 |
Documentation for tar archive files, including GNU tar extensions. |
| 481 |
|
| 482 |
|
| 483 |
.. _collapsible-admonitions: |
| 484 |
|
| 485 |
.. rubric:: Collapsible text |
| 486 |
|
| 487 |
.. versionadded:: 8.2 |
| 488 |
|
| 489 |
Each admonition directive supports a ``:collapsible:`` option, |
| 490 |
to make the content of the admonition collapsible |
| 491 |
(where supported by the output format). |
| 492 |
This can be useful for content that is not always relevant. |
| 493 |
By default, collapsible admonitions are initially open, |
| 494 |
but this can be controlled with the ``open`` and ``closed`` arguments |
| 495 |
to the ``:collapsible:`` option, which change the default state. |
| 496 |
In output formats that don't support collapsible content, |
| 497 |
the text is always included. |
| 498 |
For example: |
| 499 |
|
| 500 |
.. code-block:: rst |
| 501 |
|
| 502 |
.. note:: |
| 503 |
:collapsible: |
| 504 |
|
| 505 |
This note is collapsible, and initially open by default. |
| 506 |
|
| 507 |
.. admonition:: Example |
| 508 |
:collapsible: open |
| 509 |
|
| 510 |
This example is collapsible, and initially open. |
| 511 |
|
| 512 |
.. hint:: |
| 513 |
:collapsible: closed |
| 514 |
|
| 515 |
This hint is collapsible, but initially closed. |
| 516 |
|
| 517 |
.. note:: |
| 518 |
:collapsible: |
| 519 |
|
| 520 |
This note is collapsible, and initially open by default. |
| 521 |
|
| 522 |
.. admonition:: Example |
| 523 |
:collapsible: open |
| 524 |
|
| 525 |
This example is collapsible, and initially open. |
| 526 |
|
| 527 |
.. hint:: |
| 528 |
:collapsible: closed |
| 529 |
|
| 530 |
This hint is collapsible, but initially closed. |
| 531 |
|
| 532 |
|
| 533 |
Describing changes between versions |
| 534 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 535 |
|
| 536 |
.. index:: pair: added; in version |
| 537 |
pair: changes; in version |
| 538 |
pair: removed; in version |
| 539 |
|
| 540 |
.. rst:directive:: .. version-added:: version [brief explanation] |
| 541 |
.. versionadded:: version [brief explanation] |
| 542 |
|
| 543 |
This directive documents the version of the project |
| 544 |
which added the described feature. |
| 545 |
When this applies to an entire module or component, |
| 546 |
it should be placed at the top of the relevant section before any prose. |
| 547 |
|
| 548 |
The first argument must be given and is the version in question; you can add |
| 549 |
a second argument consisting of a *brief* explanation of the change. |
| 550 |
|
| 551 |
.. attention:: |
| 552 |
There must be no blank line between the directive head and the explanation; |
| 553 |
this is to make these blocks visually continuous in the markup. |
| 554 |
|
| 555 |
.. version-changed:: 9.0 |
| 556 |
The :rst:dir:`versionadded` directive was renamed to :rst:dir:`version-added`. |
| 557 |
The previous name is retained as an alias. |
| 558 |
|
| 559 |
Example:: |
| 560 |
|
| 561 |
.. version-added:: 2.5 |
| 562 |
The *spam* parameter. |
| 563 |
|
| 564 |
.. version-added:: 2.5 |
| 565 |
The *spam* parameter. |
| 566 |
|
| 567 |
.. rst:directive:: .. version-changed:: version [brief explanation] |
| 568 |
.. versionchanged:: version [brief explanation] |
| 569 |
|
| 570 |
Similar to :rst:dir:`version-added`, but describes when and what changed in |
| 571 |
the named feature in some way (new parameters, changed side effects, etc.). |
| 572 |
|
| 573 |
.. version-changed:: 9.0 |
| 574 |
The :rst:dir:`versionchanged` directive was renamed to :rst:dir:`version-changed`. |
| 575 |
The previous name is retained as an alias. |
| 576 |
|
| 577 |
Example:: |
| 578 |
|
| 579 |
.. version-changed:: 2.8 |
| 580 |
The *spam* parameter is now of type *boson*. |
| 581 |
|
| 582 |
.. version-changed:: 2.8 |
| 583 |
The *spam* parameter is now of type *boson*. |
| 584 |
|
| 585 |
.. rst:directive:: .. version-deprecated:: version [brief explanation] |
| 586 |
.. deprecated:: version [brief explanation] |
| 587 |
|
| 588 |
Similar to :rst:dir:`version-added`, but describes when the feature was |
| 589 |
deprecated. |
| 590 |
A *brief* explanation can also be given, |
| 591 |
for example to tell the reader what to use instead. |
| 592 |
|
| 593 |
.. version-changed:: 9.0 |
| 594 |
The :rst:dir:`deprecated` directive was renamed to :rst:dir:`version-deprecated`. |
| 595 |
The previous name is retained as an alias |
| 596 |
|
| 597 |
Example:: |
| 598 |
|
| 599 |
.. version-deprecated:: 3.1 |
| 600 |
Use :py:func:`spam` instead. |
| 601 |
|
| 602 |
.. version-deprecated:: 3.1 |
| 603 |
Use :py:func:`!spam` instead. |
| 604 |
|
| 605 |
.. rst:directive:: .. version-removed:: version [brief explanation] |
| 606 |
.. versionremoved:: version [brief explanation] |
| 607 |
|
| 608 |
Similar to :rst:dir:`version-added`, but describes when the feature was removed. |
| 609 |
An explanation may be provided to tell the reader what to use instead, |
| 610 |
or why the feature was removed. |
| 611 |
|
| 612 |
.. version-added:: 7.3 |
| 613 |
|
| 614 |
.. version-changed:: 9.0 |
| 615 |
The :rst:dir:`versionremoved` directive was renamed to :rst:dir:`version-removed`. |
| 616 |
The previous name is retained as an alias. |
| 617 |
|
| 618 |
Example:: |
| 619 |
|
| 620 |
.. version-removed:: 4.0 |
| 621 |
The :py:func:`spam` function is more flexible, and should be used instead. |
| 622 |
|
| 623 |
.. version-removed:: 4.0 |
| 624 |
The :py:func:`!spam` function is more flexible, and should be used instead. |
| 625 |
|
| 626 |
|
| 627 |
Presentational |
| 628 |
^^^^^^^^^^^^^^ |
| 629 |
|
| 630 |
.. rst:directive:: .. rubric:: title |
| 631 |
|
| 632 |
A rubric is like an informal heading that doesn't correspond to the document's structure, |
| 633 |
i.e. it does not create a table of contents node. |
| 634 |
|
| 635 |
.. note:: |
| 636 |
|
| 637 |
If the *title* of the rubric is "Footnotes" (or the selected language's |
| 638 |
equivalent), this rubric is ignored by the LaTeX writer, since it is |
| 639 |
assumed to only contain footnote definitions and therefore would create an |
| 640 |
empty heading. |
| 641 |
|
| 642 |
.. rubric:: Options |
| 643 |
|
| 644 |
.. rst:directive:option:: class: class names |
| 645 |
:type: a list of class names, separated by spaces |
| 646 |
|
| 647 |
Assign `class attributes`_. |
| 648 |
This is a :dudir:`common option <common-options>`. |
| 649 |
|
| 650 |
.. rst:directive:option:: name: label |
| 651 |
:type: text |
| 652 |
|
| 653 |
An implicit target name that can be referenced using :rst:role:`ref`. |
| 654 |
This is a :dudir:`common option <common-options>`. |
| 655 |
|
| 656 |
.. rst:directive:option:: heading-level: n |
| 657 |
:type: number from 1 to 6 |
| 658 |
|
| 659 |
.. versionadded:: 7.4.1 |
| 660 |
|
| 661 |
Use this option to specify the heading level of the rubric. |
| 662 |
In this case the rubric will be rendered as ``<h1>`` to ``<h6>`` for HTML output, |
| 663 |
or as the corresponding non-numbered sectioning command for LaTeX |
| 664 |
(see :confval:`latex_toplevel_sectioning`). |
| 665 |
|
| 666 |
|
| 667 |
.. rst:directive:: centered |
| 668 |
|
| 669 |
This directive creates a centered boldfaced line of text. |
| 670 |
|
| 671 |
.. deprecated:: 1.1 |
| 672 |
This presentation-only directive is a legacy from older versions. |
| 673 |
Use a :ref:`rst-class <rstclass>` directive instead and add an |
| 674 |
appropriate style. |
| 675 |
|
| 676 |
.. rst:directive:: hlist |
| 677 |
|
| 678 |
This directive must contain a bullet list. It will transform it into a more |
| 679 |
compact list by either distributing more than one item horizontally, or |
| 680 |
reducing spacing between items, depending on the builder. |
| 681 |
|
| 682 |
.. rubric:: Options |
| 683 |
|
| 684 |
.. rst:directive:option:: columns: n |
| 685 |
:type: int |
| 686 |
|
| 687 |
The number of columns; defaults to 2. |
| 688 |
For example:: |
| 689 |
|
| 690 |
.. hlist:: |
| 691 |
:columns: 3 |
| 692 |
|
| 693 |
* A list of |
| 694 |
* short items |
| 695 |
* that should be |
| 696 |
* displayed |
| 697 |
* horizontally |
| 698 |
|
| 699 |
.. versionadded:: 0.6 |
| 700 |
|
| 701 |
|
| 702 |
.. _code-examples: |
| 703 |
|
| 704 |
Showing code examples |
| 705 |
--------------------- |
| 706 |
|
| 707 |
.. index:: pair: code; examples |
| 708 |
single: sourcecode |
| 709 |
|
| 710 |
There are multiple ways to show syntax-highlighted literal code blocks in |
| 711 |
Sphinx: |
| 712 |
|
| 713 |
* using :ref:`reStructuredText doctest blocks <rst-doctest-blocks>`; |
| 714 |
* using :ref:`reStructuredText literal blocks <rst-literal-blocks>`, optionally in |
| 715 |
combination with the :rst:dir:`highlight` directive; |
| 716 |
* using the :rst:dir:`code-block` directive; |
| 717 |
* and using the :rst:dir:`literalinclude` directive. |
| 718 |
|
| 719 |
Doctest blocks can only be used |
| 720 |
to show interactive Python sessions, while the remaining three can be used for |
| 721 |
other languages. Of these three, literal blocks are useful when an entire |
| 722 |
document, or at least large sections of it, use code blocks with the same |
| 723 |
syntax and which should be styled in the same manner. On the other hand, the |
| 724 |
:rst:dir:`code-block` directive makes more sense when you want more fine-tuned |
| 725 |
control over the styling of each block or when you have a document containing |
| 726 |
code blocks using multiple varied syntaxes. Finally, the |
| 727 |
:rst:dir:`literalinclude` directive is useful for including entire code files |
| 728 |
in your documentation. |
| 729 |
|
| 730 |
In all cases, Syntax highlighting is provided by `Pygments |
| 731 |
<https://pygments.org>`_. When using literal blocks, this is configured using |
| 732 |
any :rst:dir:`highlight` directives in the source file. When a ``highlight`` |
| 733 |
directive is encountered, it is used until the next ``highlight`` directive is |
| 734 |
encountered. If there is no ``highlight`` directive in the file, the global |
| 735 |
highlighting language is used. This defaults to ``python`` but can be |
| 736 |
configured using the :confval:`highlight_language` config value. The following |
| 737 |
values are supported: |
| 738 |
|
| 739 |
* ``none`` (no highlighting) |
| 740 |
* ``default`` (similar to ``python3`` but with a fallback to ``none`` without |
| 741 |
warning highlighting fails; the default when :confval:`highlight_language` |
| 742 |
isn't set) |
| 743 |
* ``guess`` (let Pygments guess the lexer based on contents, only works with |
| 744 |
certain well-recognizable languages) |
| 745 |
* ``python`` |
| 746 |
* ``rest`` |
| 747 |
* ``c`` |
| 748 |
* ... and any other `lexer alias that Pygments supports`__ |
| 749 |
|
| 750 |
If highlighting with the selected language fails (i.e. Pygments emits an |
| 751 |
"Error" token), the block is not highlighted in any way. |
| 752 |
|
| 753 |
.. important:: |
| 754 |
|
| 755 |
The list of lexer aliases supported is tied to the Pygment version. If you |
| 756 |
want to ensure consistent highlighting, you should fix your version of |
| 757 |
Pygments. |
| 758 |
|
| 759 |
__ https://pygments.org/docs/lexers |
| 760 |
|
| 761 |
.. rst:directive:: .. highlight:: language |
| 762 |
|
| 763 |
Example:: |
| 764 |
|
| 765 |
.. highlight:: c |
| 766 |
|
| 767 |
This language is used until the next ``highlight`` directive is encountered. |
| 768 |
As discussed previously, *language* can be any lexer alias supported by |
| 769 |
Pygments. |
| 770 |
|
| 771 |
.. rubric:: Options |
| 772 |
|
| 773 |
.. rst:directive:option:: linenothreshold: threshold |
| 774 |
:type: number (optional) |
| 775 |
|
| 776 |
Enable to generate line numbers for code blocks. |
| 777 |
|
| 778 |
This option takes an optional number as threshold parameter. If any |
| 779 |
threshold given, the directive will produce line numbers only for the code |
| 780 |
blocks longer than N lines. If not given, line numbers will be produced |
| 781 |
for all of code blocks. |
| 782 |
|
| 783 |
Example:: |
| 784 |
|
| 785 |
.. highlight:: python |
| 786 |
:linenothreshold: 5 |
| 787 |
|
| 788 |
.. rst:directive:option:: force |
| 789 |
:type: no value |
| 790 |
|
| 791 |
If given, minor errors on highlighting are ignored. |
| 792 |
|
| 793 |
.. versionadded:: 2.1 |
| 794 |
|
| 795 |
.. rst:directive:: .. code-block:: [language] |
| 796 |
.. sourcecode:: [language] |
| 797 |
.. code:: [language] |
| 798 |
|
| 799 |
Example:: |
| 800 |
|
| 801 |
.. code-block:: ruby |
| 802 |
|
| 803 |
Some Ruby code. |
| 804 |
|
| 805 |
The directive's alias name :rst:dir:`sourcecode` works as well. This |
| 806 |
directive takes a language name as an argument. It can be `any lexer alias |
| 807 |
supported by Pygments <https://pygments.org/docs/lexers/>`_. If it is not |
| 808 |
given, the setting of :rst:dir:`highlight` directive will be used. If not |
| 809 |
set, :confval:`highlight_language` will be used. To display a code example |
| 810 |
*inline* within other text, rather than as a separate block, you can use the |
| 811 |
:rst:role:`code` role instead. |
| 812 |
|
| 813 |
.. versionchanged:: 2.0 |
| 814 |
The ``language`` argument becomes optional. |
| 815 |
|
| 816 |
.. rubric:: Options |
| 817 |
|
| 818 |
.. rst:directive:option:: linenos |
| 819 |
:type: no value |
| 820 |
|
| 821 |
Enable to generate line numbers for the code block:: |
| 822 |
|
| 823 |
.. code-block:: ruby |
| 824 |
:linenos: |
| 825 |
|
| 826 |
Some more Ruby code. |
| 827 |
|
| 828 |
.. rst:directive:option:: lineno-start: number |
| 829 |
:type: number |
| 830 |
|
| 831 |
Set the first line number of the code block. If present, ``linenos`` |
| 832 |
option is also automatically activated:: |
| 833 |
|
| 834 |
.. code-block:: ruby |
| 835 |
:lineno-start: 10 |
| 836 |
|
| 837 |
Some more Ruby code, with line numbering starting at 10. |
| 838 |
|
| 839 |
.. versionadded:: 1.3 |
| 840 |
|
| 841 |
.. rst:directive:option:: emphasize-lines: line numbers |
| 842 |
:type: comma separated numbers |
| 843 |
|
| 844 |
Emphasize particular lines of the code block:: |
| 845 |
|
| 846 |
.. code-block:: python |
| 847 |
:emphasize-lines: 3,5 |
| 848 |
|
| 849 |
def some_function(): |
| 850 |
interesting = False |
| 851 |
print('This line is highlighted.') |
| 852 |
print('This one is not...') |
| 853 |
print('...but this one is.') |
| 854 |
|
| 855 |
.. versionadded:: 1.1 |
| 856 |
.. versionchanged:: 1.6.6 |
| 857 |
LaTeX supports the ``emphasize-lines`` option. |
| 858 |
|
| 859 |
.. rst:directive:option:: force |
| 860 |
:type: no value |
| 861 |
|
| 862 |
Ignore minor errors on highlighting. |
| 863 |
|
| 864 |
.. versionadded:: 2.1 |
| 865 |
|
| 866 |
.. rst:directive:option:: caption: caption of code block |
| 867 |
:type: text |
| 868 |
|
| 869 |
Set a caption to the code block. |
| 870 |
|
| 871 |
.. versionadded:: 1.3 |
| 872 |
|
| 873 |
.. rst:directive:option:: name: a label for hyperlink |
| 874 |
:type: text |
| 875 |
|
| 876 |
Define implicit target name that can be referenced by using |
| 877 |
:rst:role:`ref`. For example:: |
| 878 |
|
| 879 |
.. code-block:: python |
| 880 |
:caption: this.py |
| 881 |
:name: this-py |
| 882 |
|
| 883 |
print('Explicit is better than implicit.') |
| 884 |
|
| 885 |
In order to cross-reference a code-block using either the |
| 886 |
:rst:role:`ref` or the :rst:role:`numref` role, it is necessary |
| 887 |
that both :strong:`name` and :strong:`caption` be defined. The |
| 888 |
argument of :strong:`name` can then be given to :rst:role:`numref` |
| 889 |
to generate the cross-reference. Example:: |
| 890 |
|
| 891 |
See :numref:`this-py` for an example. |
| 892 |
|
| 893 |
When using :rst:role:`ref`, it is possible to generate a cross-reference |
| 894 |
with only :strong:`name` defined, provided an explicit title is |
| 895 |
given. Example:: |
| 896 |
|
| 897 |
See :ref:`this code snippet <this-py>` for an example. |
| 898 |
|
| 899 |
.. versionadded:: 1.3 |
| 900 |
|
| 901 |
.. rst:directive:option:: class: class names |
| 902 |
:type: a list of class names separated by spaces |
| 903 |
|
| 904 |
Assign `class attributes`_. |
| 905 |
This is a :dudir:`common option <common-options>`. |
| 906 |
|
| 907 |
.. versionadded:: 1.4 |
| 908 |
|
| 909 |
.. rst:directive:option:: dedent: number |
| 910 |
:type: number or no value |
| 911 |
|
| 912 |
Strip indentation characters from the code block. When number given, |
| 913 |
leading N characters are removed. When no argument given, leading spaces |
| 914 |
are removed via :func:`textwrap.dedent`. For example:: |
| 915 |
|
| 916 |
.. code-block:: ruby |
| 917 |
:linenos: |
| 918 |
:dedent: 4 |
| 919 |
|
| 920 |
some ruby code |
| 921 |
|
| 922 |
.. versionadded:: 1.3 |
| 923 |
.. versionchanged:: 3.5 |
| 924 |
Support automatic dedent. |
| 925 |
|
| 926 |
.. rst:directive:: .. literalinclude:: filename |
| 927 |
|
| 928 |
Longer displays of verbatim text may be included by storing the example text |
| 929 |
in an external file containing only plain text. The file may be included |
| 930 |
using the ``literalinclude`` directive. [#]_ |
| 931 |
For example, to include the Python source file :file:`example.py`, use: |
| 932 |
|
| 933 |
.. code-block:: rst |
| 934 |
|
| 935 |
.. literalinclude:: example.py |
| 936 |
|
| 937 |
The file name is usually relative to the current file's path. However, if |
| 938 |
it is absolute (starting with ``/``), it is relative to the top source |
| 939 |
directory. |
| 940 |
|
| 941 |
.. rubric:: General options |
| 942 |
|
| 943 |
.. rst:directive:option:: class: class names |
| 944 |
:type: a list of class names, separated by spaces |
| 945 |
|
| 946 |
Assign `class attributes`_. |
| 947 |
This is a :dudir:`common option <common-options>`. |
| 948 |
|
| 949 |
.. _class attributes: https://docutils.sourceforge.io/docs/ref/doctree.html#classes |
| 950 |
|
| 951 |
.. versionadded:: 1.4 |
| 952 |
|
| 953 |
.. rst:directive:option:: name: label |
| 954 |
:type: text |
| 955 |
|
| 956 |
An implicit target name that can be referenced using :rst:role:`ref`. |
| 957 |
This is a :dudir:`common option <common-options>`. |
| 958 |
|
| 959 |
.. versionadded:: 1.3 |
| 960 |
|
| 961 |
.. rst:directive:option:: caption: caption |
| 962 |
:type: text |
| 963 |
|
| 964 |
Add a caption above the included content. |
| 965 |
If no argument is given, the filename is used as the caption. |
| 966 |
|
| 967 |
.. versionadded:: 1.3 |
| 968 |
|
| 969 |
.. rubric:: Options for formatting |
| 970 |
|
| 971 |
.. rst:directive:option:: dedent: number |
| 972 |
:type: number or no value |
| 973 |
|
| 974 |
Strip indentation characters from the included content. |
| 975 |
When a number is given, the leading N characters are removed. |
| 976 |
When no argument given, all common leading indentation is removed |
| 977 |
(using :func:`textwrap.dedent`). |
| 978 |
|
| 979 |
.. versionadded:: 1.3 |
| 980 |
.. versionchanged:: 3.5 |
| 981 |
Support automatic dedent. |
| 982 |
|
| 983 |
.. rst:directive:option:: tab-width: N |
| 984 |
:type: number |
| 985 |
|
| 986 |
Expand tabs to N spaces. |
| 987 |
|
| 988 |
.. versionadded:: 1.0 |
| 989 |
|
| 990 |
.. rst:directive:option:: encoding |
| 991 |
:type: text |
| 992 |
|
| 993 |
Explicitly specify the encoding of the file. |
| 994 |
This overwrites the default encoding (UTF-8). |
| 995 |
For example: |
| 996 |
|
| 997 |
.. code-block:: rst |
| 998 |
|
| 999 |
.. literalinclude:: example.txt |
| 1000 |
:encoding: latin-1 |
| 1001 |
|
| 1002 |
.. versionadded:: 0.4.3 |
| 1003 |
|
| 1004 |
.. rst:directive:option:: linenos |
| 1005 |
:type: no value |
| 1006 |
|
| 1007 |
Show line numbers alongside the included content. |
| 1008 |
By default, line numbers are counted from 1. |
| 1009 |
This can be changed by the options ``:lineno-start:`` and ``:lineno-match:``. |
| 1010 |
|
| 1011 |
.. rst:directive:option:: lineno-start: number |
| 1012 |
:type: number |
| 1013 |
|
| 1014 |
Set line number for the first line to show. |
| 1015 |
If given, this automatically activates ``:linenos:``. |
| 1016 |
|
| 1017 |
.. rst:directive:option:: lineno-match |
| 1018 |
|
| 1019 |
When including only parts of a file and show the original line numbers. |
| 1020 |
This is only allowed only when the selection consists of contiguous lines. |
| 1021 |
|
| 1022 |
.. versionadded:: 1.3 |
| 1023 |
|
| 1024 |
.. rst:directive:option:: emphasize-lines: line numbers |
| 1025 |
:type: comma separated numbers |
| 1026 |
|
| 1027 |
Emphasise particular lines of the included content. |
| 1028 |
For example: |
| 1029 |
|
| 1030 |
.. code-block:: rst |
| 1031 |
|
| 1032 |
.. literalinclude:: example.txt |
| 1033 |
:emphasize-lines: 1,2,4-6 |
| 1034 |
|
| 1035 |
.. rst:directive:option:: language: language |
| 1036 |
:type: text |
| 1037 |
|
| 1038 |
Select a highlighting language (`Pygments lexer`_) different from |
| 1039 |
the current file's standard language |
| 1040 |
(set by :rst:dir:`highlight` or :confval:`highlight_language`). |
| 1041 |
|
| 1042 |
.. _Pygments lexer: https://pygments.org/docs/lexers/ |
| 1043 |
|
| 1044 |
.. rst:directive:option:: force |
| 1045 |
:type: no value |
| 1046 |
|
| 1047 |
Ignore minor errors in highlighting. |
| 1048 |
|
| 1049 |
.. versionadded:: 2.1 |
| 1050 |
|
| 1051 |
.. rubric:: Options for selecting the content to include |
| 1052 |
|
| 1053 |
.. rst:directive:option:: pyobject: object |
| 1054 |
:type: text |
| 1055 |
|
| 1056 |
For Python files, only include the specified class, function or method: |
| 1057 |
|
| 1058 |
.. code-block:: rst |
| 1059 |
|
| 1060 |
.. literalinclude:: example.py |
| 1061 |
:pyobject: Timer.start |
| 1062 |
|
| 1063 |
.. versionadded:: 0.6 |
| 1064 |
|
| 1065 |
.. rst:directive:option:: lines: line numbers |
| 1066 |
:type: comma separated numbers |
| 1067 |
|
| 1068 |
Specify exactly which lines to include:: |
| 1069 |
|
| 1070 |
.. literalinclude:: example.py |
| 1071 |
:lines: 1,3,5-10,20- |
| 1072 |
|
| 1073 |
This includes line 1, line 3, lines 5 to 10, and line 20 to the end. |
| 1074 |
|
| 1075 |
.. versionadded:: 0.6 |
| 1076 |
|
| 1077 |
.. rst:directive:option:: start-at: text |
| 1078 |
start-after: text |
| 1079 |
end-before: text |
| 1080 |
end-at: text |
| 1081 |
|
| 1082 |
Another way to control which part of the file is included is to use |
| 1083 |
the ``start-after`` and ``end-before`` options (or only one of them). |
| 1084 |
If ``start-after`` is given as a string option, |
| 1085 |
only lines that follow the first line containing that string are included. |
| 1086 |
If ``end-before`` is given as a string option, |
| 1087 |
only lines that precede the first lines containing that string are included. |
| 1088 |
The ``start-at`` and ``end-at`` options behave in a similar way, |
| 1089 |
but the lines containing the matched string are included. |
| 1090 |
|
| 1091 |
``start-after``/``start-at`` and ``end-before``/``end-at`` can have same string. |
| 1092 |
``start-after``/``start-at`` filter lines before the line |
| 1093 |
that contains the option string |
| 1094 |
(``start-at`` will keep the line). |
| 1095 |
Then ``end-before``/``end-at`` filter lines after the line |
| 1096 |
that contains the option string |
| 1097 |
(``end-at`` will keep the line and ``end-before`` skip the first line). |
| 1098 |
|
| 1099 |
.. versionadded:: 0.6 |
| 1100 |
Added the ``start-after`` and ``end-before`` options. |
| 1101 |
.. versionadded:: 1.5 |
| 1102 |
Added the ``start-at``, and ``end-at`` options. |
| 1103 |
|
| 1104 |
.. tip:: |
| 1105 |
|
| 1106 |
To only select ``[second-section]`` of an INI file such as the following, |
| 1107 |
use ``:start-at: [second-section]`` and ``:end-before: [third-section]``: |
| 1108 |
|
| 1109 |
.. code-block:: ini |
| 1110 |
|
| 1111 |
[first-section] |
| 1112 |
var_in_first=true |
| 1113 |
|
| 1114 |
[second-section] |
| 1115 |
var_in_second=true |
| 1116 |
|
| 1117 |
[third-section] |
| 1118 |
var_in_third=true |
| 1119 |
|
| 1120 |
These options can be useful when working with tag comments. |
| 1121 |
Using ``:start-after: [initialise]`` and ``:end-before: [initialised]`` |
| 1122 |
keeps the lines between between the two comments below: |
| 1123 |
|
| 1124 |
.. code-block:: py |
| 1125 |
|
| 1126 |
if __name__ == "__main__": |
| 1127 |
# [initialise] |
| 1128 |
app.start(":8000") |
| 1129 |
# [initialised] |
| 1130 |
|
| 1131 |
When lines have been selected in any of the ways described above, |
| 1132 |
the line numbers in ``emphasize-lines`` refer to these selected lines, |
| 1133 |
counted consecutively starting from 1. |
| 1134 |
|
| 1135 |
.. rst:directive:option:: prepend: line |
| 1136 |
:type: text |
| 1137 |
|
| 1138 |
Prepend a line before the included code. |
| 1139 |
This can be useful for example when highlighting |
| 1140 |
PHP code that doesn't include the ``<?php`` or ``?>`` markers. |
| 1141 |
|
| 1142 |
.. versionadded:: 1.0 |
| 1143 |
|
| 1144 |
.. rst:directive:option:: append: line |
| 1145 |
:type: text |
| 1146 |
|
| 1147 |
Append a line after the included code. |
| 1148 |
This can be useful for example when highlighting |
| 1149 |
PHP code that doesn't include the ``<?php`` or ``?>`` markers. |
| 1150 |
|
| 1151 |
.. versionadded:: 1.0 |
| 1152 |
|
| 1153 |
.. rst:directive:option:: diff: filename |
| 1154 |
|
| 1155 |
Show the diff of two files. |
| 1156 |
For example: |
| 1157 |
|
| 1158 |
.. code-block:: rst |
| 1159 |
|
| 1160 |
.. literalinclude:: example.txt |
| 1161 |
:diff: example.txt.orig |
| 1162 |
|
| 1163 |
This shows the diff between ``example.txt`` and ``example.txt.orig`` |
| 1164 |
with unified diff format. |
| 1165 |
|
| 1166 |
.. versionadded:: 1.3 |
| 1167 |
|
| 1168 |
.. versionchanged:: 0.6 |
| 1169 |
Added support for absolute filenames. |
| 1170 |
|
| 1171 |
.. versionchanged:: 1.6 |
| 1172 |
With both ``start-after`` and ``lines`` in use, |
| 1173 |
the first line as per ``start-after`` is considered to be |
| 1174 |
with line number ``1`` for ``lines``. |
| 1175 |
|
| 1176 |
.. _glossary-directive: |
| 1177 |
|
| 1178 |
Glossary |
| 1179 |
-------- |
| 1180 |
|
| 1181 |
.. rst:directive:: .. glossary:: |
| 1182 |
|
| 1183 |
This directive must contain a reStructuredText definition-list-like markup |
| 1184 |
with terms and definitions. The definitions will then be referenceable with the |
| 1185 |
:rst:role:`term` role. Example:: |
| 1186 |
|
| 1187 |
.. glossary:: |
| 1188 |
|
| 1189 |
environment |
| 1190 |
A structure where information about all documents under the root is |
| 1191 |
saved, and used for cross-referencing. The environment is pickled |
| 1192 |
after the parsing stage, so that successive runs only need to read |
| 1193 |
and parse new and changed documents. |
| 1194 |
|
| 1195 |
source directory |
| 1196 |
The directory which, including its subdirectories, contains all |
| 1197 |
source files for one Sphinx project. |
| 1198 |
|
| 1199 |
In contrast to regular definition lists, *multiple* terms per entry are |
| 1200 |
allowed, and inline markup is allowed in terms. You can link to all of the |
| 1201 |
terms. For example:: |
| 1202 |
|
| 1203 |
.. glossary:: |
| 1204 |
|
| 1205 |
term 1 |
| 1206 |
term 2 |
| 1207 |
Definition of both terms. |
| 1208 |
|
| 1209 |
(When the glossary is sorted, the first term determines the sort order.) |
| 1210 |
|
| 1211 |
If you want to specify "grouping key" for general index entries, you can put |
| 1212 |
a "key" as "term : key". For example:: |
| 1213 |
|
| 1214 |
.. glossary:: |
| 1215 |
|
| 1216 |
term 1 : A |
| 1217 |
term 2 : B |
| 1218 |
Definition of both terms. |
| 1219 |
|
| 1220 |
Note that "key" is used for grouping key as is. |
| 1221 |
The "key" isn't normalized; key "A" and "a" become different groups. |
| 1222 |
The whole characters in "key" is used instead of a first character; it is |
| 1223 |
used for "Combining Character Sequence" and "Surrogate Pairs" grouping key. |
| 1224 |
|
| 1225 |
In i18n situation, you can specify "localized term : key" even if original |
| 1226 |
text only have "term" part. In this case, translated "localized term" will be |
| 1227 |
categorized in "key" group. |
| 1228 |
|
| 1229 |
|
| 1230 |
.. versionchanged:: 1.1 |
| 1231 |
Now supports multiple terms and inline markup in terms. |
| 1232 |
|
| 1233 |
.. versionchanged:: 1.4 |
| 1234 |
Index key for glossary term should be considered *experimental*. |
| 1235 |
|
| 1236 |
|
| 1237 |
.. rubric:: Options |
| 1238 |
|
| 1239 |
.. rst:directive:option:: sorted |
| 1240 |
|
| 1241 |
Sort the entries alphabetically. |
| 1242 |
|
| 1243 |
.. versionadded:: 0.6 |
| 1244 |
|
| 1245 |
.. versionchanged:: 4.4 |
| 1246 |
In internationalized documentation, sort according to translated terms. |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
Meta-information markup |
| 1251 |
----------------------- |
| 1252 |
|
| 1253 |
.. rst:directive:: .. sectionauthor:: name <email> |
| 1254 |
|
| 1255 |
Identifies the author of the current section. The argument should include |
| 1256 |
the author's name such that it can be used for presentation and email |
| 1257 |
address. The domain name portion of the address should be lower case. |
| 1258 |
Example:: |
| 1259 |
|
| 1260 |
.. sectionauthor:: Guido van Rossum <[email protected]> |
| 1261 |
|
| 1262 |
By default, this markup isn't reflected in the output in any way (it helps |
| 1263 |
keep track of contributions), but you can set the configuration value |
| 1264 |
:confval:`show_authors` to ``True`` to make them produce a paragraph in the |
| 1265 |
output. |
| 1266 |
|
| 1267 |
|
| 1268 |
.. rst:directive:: .. codeauthor:: name <email> |
| 1269 |
|
| 1270 |
The :rst:dir:`codeauthor` directive, which can appear multiple times, names |
| 1271 |
the authors of the described code, just like :rst:dir:`sectionauthor` names |
| 1272 |
the author(s) of a piece of documentation. It too only produces output if |
| 1273 |
the :confval:`show_authors` configuration value is ``True``. |
| 1274 |
|
| 1275 |
|
| 1276 |
Index-generating markup |
| 1277 |
----------------------- |
| 1278 |
|
| 1279 |
Sphinx automatically creates index entries from all object descriptions (like |
| 1280 |
functions, classes or attributes) like discussed in |
| 1281 |
:doc:`/usage/domains/index`. |
| 1282 |
|
| 1283 |
However, there is also explicit markup available, to make the index more |
| 1284 |
comprehensive and enable index entries in documents where information is not |
| 1285 |
mainly contained in information units, such as the language reference. |
| 1286 |
|
| 1287 |
.. rst:directive:: .. index:: <entries> |
| 1288 |
|
| 1289 |
This directive contains one or more index entries. Each entry consists of a |
| 1290 |
type and a value, separated by a colon. |
| 1291 |
|
| 1292 |
For example:: |
| 1293 |
|
| 1294 |
.. index:: |
| 1295 |
single: execution; context |
| 1296 |
pair: module; __main__ |
| 1297 |
pair: module; sys |
| 1298 |
triple: module; search; path |
| 1299 |
seealso: scope |
| 1300 |
|
| 1301 |
The execution context |
| 1302 |
--------------------- |
| 1303 |
|
| 1304 |
... |
| 1305 |
|
| 1306 |
This directive contains five entries, which will be converted to entries in |
| 1307 |
the generated index which link to the exact location of the index statement |
| 1308 |
(or, in case of offline media, the corresponding page number). |
| 1309 |
|
| 1310 |
Since index directives generate cross-reference targets at their location in |
| 1311 |
the source, it makes sense to put them *before* the thing they refer to -- |
| 1312 |
e.g. a heading, as in the example above. |
| 1313 |
|
| 1314 |
The possible entry types are: |
| 1315 |
|
| 1316 |
single |
| 1317 |
Creates a single index entry. |
| 1318 |
Can be made a sub-entry by separating the sub-entry text with a semicolon |
| 1319 |
(this notation is also used below to describe what entries are created). |
| 1320 |
Examples: |
| 1321 |
|
| 1322 |
.. code-block:: rst |
| 1323 |
|
| 1324 |
.. index:: single: execution |
| 1325 |
single: execution; context |
| 1326 |
|
| 1327 |
- ``single: execution`` creates an index entry labelled ``execution``. |
| 1328 |
- ``single: execution; context`` creates an sub-entry of ``execution`` |
| 1329 |
labelled ``context``. |
| 1330 |
pair |
| 1331 |
A shortcut to create two index entries. |
| 1332 |
The pair of values must be separated by a semicolon. |
| 1333 |
Example: |
| 1334 |
|
| 1335 |
.. code-block:: rst |
| 1336 |
|
| 1337 |
.. index:: pair: loop; statement |
| 1338 |
|
| 1339 |
This would create two index entries; ``loop; statement`` and ``statement; loop``. |
| 1340 |
triple |
| 1341 |
A shortcut to create three index entries. |
| 1342 |
All three values must be separated by a semicolon. |
| 1343 |
Example: |
| 1344 |
|
| 1345 |
.. code-block:: rst |
| 1346 |
|
| 1347 |
.. index:: triple: module; search; path |
| 1348 |
|
| 1349 |
This would create three index entries; ``module; search path``, |
| 1350 |
``search; path, module``, and ``path; module search``. |
| 1351 |
see |
| 1352 |
A shortcut to create an index entry that refers to another entry. |
| 1353 |
Example: |
| 1354 |
|
| 1355 |
.. code-block:: rst |
| 1356 |
|
| 1357 |
.. index:: see: entry; other |
| 1358 |
|
| 1359 |
This would create an index entry referring from ``entry`` to ``other`` |
| 1360 |
(i.e. 'entry': See 'other'). |
| 1361 |
seealso |
| 1362 |
Like ``see``, but inserts 'see also' instead of 'see'. |
| 1363 |
module, keyword, operator, object, exception, statement, builtin |
| 1364 |
These **deprecated** shortcuts all create two index entries. |
| 1365 |
For example, ``module: hashlib`` creates the entries ``module; hashlib`` |
| 1366 |
and ``hashlib; module``. |
| 1367 |
|
| 1368 |
.. deprecated:: 1.0 |
| 1369 |
These Python-specific entry types are deprecated. |
| 1370 |
|
| 1371 |
.. versionchanged:: 7.1 |
| 1372 |
Removal version set to Sphinx 9.0. |
| 1373 |
Using these entry types will now emit warnings with the ``index`` category. |
| 1374 |
|
| 1375 |
You can mark up "main" index entries by prefixing them with an exclamation |
| 1376 |
mark. The references to "main" entries are emphasized in the generated |
| 1377 |
index. For example, if two pages contain :: |
| 1378 |
|
| 1379 |
.. index:: Python |
| 1380 |
|
| 1381 |
and one page contains :: |
| 1382 |
|
| 1383 |
.. index:: ! Python |
| 1384 |
|
| 1385 |
then the backlink to the latter page is emphasized among the three backlinks. |
| 1386 |
|
| 1387 |
For index directives containing only "single" entries, there is a shorthand |
| 1388 |
notation:: |
| 1389 |
|
| 1390 |
.. index:: BNF, grammar, syntax, notation |
| 1391 |
|
| 1392 |
This creates four index entries. |
| 1393 |
|
| 1394 |
.. versionchanged:: 1.1 |
| 1395 |
Added ``see`` and ``seealso`` types, as well as marking main entries. |
| 1396 |
|
| 1397 |
.. rubric:: Options |
| 1398 |
|
| 1399 |
.. rst:directive:option:: name: a label for hyperlink |
| 1400 |
:type: text |
| 1401 |
|
| 1402 |
Define implicit target name that can be referenced by using |
| 1403 |
:rst:role:`ref`. For example:: |
| 1404 |
|
| 1405 |
.. index:: Python |
| 1406 |
:name: py-index |
| 1407 |
|
| 1408 |
.. versionadded:: 3.0 |
| 1409 |
|
| 1410 |
.. rst:role:: index |
| 1411 |
|
| 1412 |
While the :rst:dir:`index` directive is a block-level markup and links to the |
| 1413 |
beginning of the next paragraph, there is also a corresponding role that sets |
| 1414 |
the link target directly where it is used. |
| 1415 |
|
| 1416 |
The content of the role can be a simple phrase, which is then kept in the |
| 1417 |
text and used as an index entry. It can also be a combination of text and |
| 1418 |
index entry, styled like with explicit targets of cross-references. In that |
| 1419 |
case, the "target" part can be a full entry as described for the directive |
| 1420 |
above. For example:: |
| 1421 |
|
| 1422 |
This is a normal reStructuredText :index:`paragraph` that contains several |
| 1423 |
:index:`index entries <pair: index; entry>`. |
| 1424 |
|
| 1425 |
.. versionadded:: 1.1 |
| 1426 |
|
| 1427 |
|
| 1428 |
.. _tags: |
| 1429 |
|
| 1430 |
Including content based on tags |
| 1431 |
------------------------------- |
| 1432 |
|
| 1433 |
.. rst:directive:: .. only:: <expression> |
| 1434 |
|
| 1435 |
Include the content of the directive only if the *expression* is true. The |
| 1436 |
expression should consist of tags, like this:: |
| 1437 |
|
| 1438 |
.. only:: html and draft |
| 1439 |
|
| 1440 |
Undefined tags are false, defined tags are true |
| 1441 |
(tags can be defined via the :option:`--tag <sphinx-build --tag>` |
| 1442 |
command-line option or within :file:`conf.py`, see :ref:`here <conf-tags>`). |
| 1443 |
Boolean expressions (like ``(latex or html) and draft``) are supported |
| 1444 |
and may use parentheses. |
| 1445 |
|
| 1446 |
The *format* and the *name* of the current builder (``html``, ``latex`` or |
| 1447 |
``text``) are always set as a tag [#]_. To make the distinction between |
| 1448 |
format and name explicit, they are also added with the prefix ``format_`` and |
| 1449 |
``builder_``, e.g. the epub builder defines the tags ``html``, ``epub``, |
| 1450 |
``format_html`` and ``builder_epub``. |
| 1451 |
|
| 1452 |
These standard tags are set *after* the configuration file is read, so they |
| 1453 |
are not available there. |
| 1454 |
|
| 1455 |
All tags must follow the standard Python identifier syntax as set out in |
| 1456 |
the `Identifiers and keywords |
| 1457 |
<https://docs.python.org/3/reference/lexical_analysis.html#identifiers>`_ |
| 1458 |
documentation. That is, a tag expression may only consist of tags that |
| 1459 |
conform to the syntax of Python variables. In ASCII, this consists of the |
| 1460 |
uppercase and lowercase letters ``A`` through ``Z``, the underscore ``_`` |
| 1461 |
and, except for the first character, the digits ``0`` through ``9``. |
| 1462 |
|
| 1463 |
.. versionadded:: 0.6 |
| 1464 |
.. versionchanged:: 1.2 |
| 1465 |
Added the name of the builder and the prefixes. |
| 1466 |
|
| 1467 |
.. warning:: |
| 1468 |
|
| 1469 |
This directive is designed to control only content of document. It could |
| 1470 |
not control sections, labels and so on. |
| 1471 |
|
| 1472 |
.. _table-directives: |
| 1473 |
|
| 1474 |
Tables |
| 1475 |
------ |
| 1476 |
|
| 1477 |
Use :ref:`reStructuredText tables <rst-tables>`, i.e. either |
| 1478 |
|
| 1479 |
- grid table syntax (:duref:`ref <grid-tables>`), |
| 1480 |
- simple table syntax (:duref:`ref <simple-tables>`), |
| 1481 |
- :dudir:`csv-table` syntax, |
| 1482 |
- or :dudir:`list-table` syntax. |
| 1483 |
|
| 1484 |
The :dudir:`table` directive serves as optional wrapper of the *grid* and |
| 1485 |
*simple* syntaxes. |
| 1486 |
|
| 1487 |
They work fine in HTML output, but rendering tables to LaTeX is complex. |
| 1488 |
Check the :confval:`latex_table_style`. |
| 1489 |
|
| 1490 |
.. versionchanged:: 1.6 |
| 1491 |
Merged cells (multi-row, multi-column, both) from grid tables containing |
| 1492 |
complex contents such as multiple paragraphs, blockquotes, lists, literal |
| 1493 |
blocks, will render correctly to LaTeX output. |
| 1494 |
|
| 1495 |
.. versionchanged:: 9.0 |
| 1496 |
The partial support of the LaTeX builder for nesting a table in another |
| 1497 |
has been extended. |
| 1498 |
Formerly Sphinx would raise an error if ``longtable`` class was specified |
| 1499 |
for a table containing a nested table, and some cases would not raise an |
| 1500 |
error at Sphinx level but fail at LaTeX level during PDF build. This is a |
| 1501 |
complex topic in LaTeX rendering and the output can sometimes be improved |
| 1502 |
via the :rst:dir:`tabularcolumns` directive. |
| 1503 |
|
| 1504 |
.. rst:directive:: .. tabularcolumns:: column spec |
| 1505 |
|
| 1506 |
This directive influences only the LaTeX output, and only for the next |
| 1507 |
table in source. The mandatory argument is a column specification (known |
| 1508 |
as an "alignment preamble" in LaTeX idiom). Please refer to a LaTeX |
| 1509 |
documentation, such as the `wiki page`_, for basics of such a column |
| 1510 |
specification. |
| 1511 |
|
| 1512 |
.. _wiki page: https://en.wikibooks.org/wiki/LaTeX/Tables |
| 1513 |
|
| 1514 |
.. versionadded:: 0.3 |
| 1515 |
|
| 1516 |
Sphinx renders tables with at most 30 rows using ``tabulary`` (or |
| 1517 |
``tabular`` if at least one cell contains either a code-block or a nested |
| 1518 |
table), and those with more rows with ``longtable``. The advantage of |
| 1519 |
using ``tabulary`` is that it tries to compute automatically (internally to |
| 1520 |
LaTeX) suitable column widths. |
| 1521 |
|
| 1522 |
The ``tabulary`` algorithm often works well, but in some cases when a cell |
| 1523 |
contains long paragraphs, the column will be given a large width and other |
| 1524 |
columns whose cells contain only single words may end up too narrow. The |
| 1525 |
:rst:dir:`tabularcolumns` can help solve this via providing to LaTeX a |
| 1526 |
custom "alignment preamble" (aka "colspec"). For example ``lJJ`` will be |
| 1527 |
suitable for a three-columns table whose first column contains only single |
| 1528 |
words and the other two have cells with long paragraphs. |
| 1529 |
|
| 1530 |
.. note:: |
| 1531 |
|
| 1532 |
Of course, a fully automated solution would be better, and it is still |
| 1533 |
hoped for, but it is an intrinsic aspect of ``tabulary``, and the latter |
| 1534 |
is in use by Sphinx ever since ``0.3``... It looks as if solving the |
| 1535 |
problem of squeezed columns could require substantial changes to that |
| 1536 |
LaTeX package. And no good alternative appears to exist, as of 2025. |
| 1537 |
|
| 1538 |
.. hint:: |
| 1539 |
|
| 1540 |
A way to solve the issue for all tables at once, is to inject in the |
| 1541 |
LaTeX preamble (see :confval:`latex_elements`) a command such as |
| 1542 |
``\setlength{\tymin}{1cm}`` which causes all columns to be at least |
| 1543 |
``1cm`` wide (not counting inter-column whitespace). Currently, Sphinx |
| 1544 |
configures ``\tymin`` to allow room for three characters at least. |
| 1545 |
|
| 1546 |
Here is a more sophisticated "colspec", for a 4-columns table: |
| 1547 |
|
| 1548 |
.. code-block:: latex |
| 1549 |
|
| 1550 |
.. tabularcolumns:: >{\raggedright}\Y{.4}>{\centering}\Y{.1}>{\sphinxcolorblend{!95!red}\centering\noindent\bfseries\color{red}}\Y{.12}>{\raggedright\arraybackslash}\Y{.38} |
| 1551 |
|
| 1552 |
This is used in Sphinx own PDF docs at :ref:`dev-deprecated-apis`. |
| 1553 |
Regarding column widths, this "colspec" achieves the same as would |
| 1554 |
``:widths:`` option set to ``40 10 12 38`` but it injects extra effects. |
| 1555 |
|
| 1556 |
.. note:: |
| 1557 |
|
| 1558 |
In case both :rst:dir:`tabularcolumns` and ``:widths:`` option of table |
| 1559 |
directives are used, ``:widths:`` option will be ignored by the LaTeX |
| 1560 |
builder. Of course it is obeyed by other builders. |
| 1561 |
|
| 1562 |
Literal blocks do not work at all with ``tabulary`` and Sphinx will then |
| 1563 |
fall back to ``tabular`` LaTeX environment. It will employ the |
| 1564 |
:rst:dir:`tabularcolumns` specification in that case only if it contains no |
| 1565 |
usage of the ``tabulary`` specific column types (which are ``L``, ``R``, |
| 1566 |
``C`` and ``J``). |
| 1567 |
|
| 1568 |
Besides the LaTeX ``l``, ``r``, ``c`` and ``p{width}`` column specifiers, |
| 1569 |
and the ``tabulary`` specific ``L``, ``R``, ``C`` and ``J``, one can also |
| 1570 |
use (with all table types) ``\X{a}{b}`` which configures the column width |
| 1571 |
to be a fraction ``a/b`` of the total line width and ``\Y{f}`` where ``f`` |
| 1572 |
is a decimal: for example ``\Y{0.2}`` means that the column will occupy |
| 1573 |
``0.2`` times the line width. |
| 1574 |
|
| 1575 |
.. versionchanged:: 1.6 |
| 1576 |
|
| 1577 |
Sphinx uses ``J`` (justified) by default with ``tabulary``, not ``L`` |
| 1578 |
(flushed-left). To revert, include ``\newcolumntype{T}{L}`` in the LaTeX |
| 1579 |
preamble, as in fact Sphinx uses ``T`` and sets it by default to be an |
| 1580 |
alias of ``J``. |
| 1581 |
|
| 1582 |
.. versionchanged:: 9.0 |
| 1583 |
|
| 1584 |
Formerly, Sphinx did not use ``tabulary`` if the table had at least one |
| 1585 |
cell containing "problematic" elements such as lists, object descriptions, |
| 1586 |
blockquotes (etc...) because such contents are not out-of-the-box |
| 1587 |
compatible with ``tabulary``. At ``9.0`` a technique, which was already |
| 1588 |
in use for merged cells, was extended to such cases, and the sole |
| 1589 |
"problematic" contents are code-blocks and nested tables. So tables |
| 1590 |
containing (only) cells with multiple paragraphs, bullet or enumerated |
| 1591 |
lists, or line blocks, will now better fit to their contents (if not |
| 1592 |
rendered by ``longtable``). Cells with object descriptions or admonitions |
| 1593 |
will still have a tendency to induce the table to fill the full text area |
| 1594 |
width, but columns in that table with no such contents will be tighter. |
| 1595 |
|
| 1596 |
.. hint:: |
| 1597 |
|
| 1598 |
To force usage of the LaTeX ``longtable`` environment pass ``longtable`` as |
| 1599 |
a ``:class:`` option to :dudir:`table`, :dudir:`csv-table`, or |
| 1600 |
:dudir:`list-table`. Use :ref:`rst-class <rstclass>` for other tables. |
| 1601 |
|
| 1602 |
Math |
| 1603 |
---- |
| 1604 |
|
| 1605 |
The input language for mathematics is LaTeX markup. This is the de-facto |
| 1606 |
standard for plain-text math notation and has the added advantage that no |
| 1607 |
further translation is necessary when building LaTeX output. |
| 1608 |
|
| 1609 |
Keep in mind that when you put math markup in **Python docstrings** read by |
| 1610 |
:mod:`autodoc <sphinx.ext.autodoc>`, you either have to double all backslashes, |
| 1611 |
or use Python raw strings (``r"raw"``). |
| 1612 |
|
| 1613 |
.. rst:directive:: math |
| 1614 |
|
| 1615 |
Directive for displayed math (math that takes the whole line for itself). |
| 1616 |
|
| 1617 |
The directive supports multiple equations, which should be separated by a |
| 1618 |
blank line:: |
| 1619 |
|
| 1620 |
.. math:: |
| 1621 |
|
| 1622 |
(a + b)^2 = a^2 + 2ab + b^2 |
| 1623 |
|
| 1624 |
(a - b)^2 = a^2 - 2ab + b^2 |
| 1625 |
|
| 1626 |
In addition, each single equation is set within a ``split`` environment, |
| 1627 |
which means that you can have multiple aligned lines in an equation, |
| 1628 |
aligned at ``&`` and separated by ``\\``:: |
| 1629 |
|
| 1630 |
.. math:: |
| 1631 |
|
| 1632 |
(a + b)^2 &= (a + b)(a + b) \\ |
| 1633 |
&= a^2 + 2ab + b^2 |
| 1634 |
|
| 1635 |
For more details, look into the documentation of the `AmSMath LaTeX |
| 1636 |
package`_. |
| 1637 |
|
| 1638 |
When the math is only one line of text, it can also be given as a directive |
| 1639 |
argument:: |
| 1640 |
|
| 1641 |
.. math:: (a + b)^2 = a^2 + 2ab + b^2 |
| 1642 |
|
| 1643 |
.. rubric:: Options |
| 1644 |
|
| 1645 |
.. rst:directive:option:: class: class names |
| 1646 |
:type: a list of class names, separated by spaces |
| 1647 |
|
| 1648 |
Assign `class attributes`_. |
| 1649 |
This is a :dudir:`common option <common-options>`. |
| 1650 |
|
| 1651 |
.. _class attributes: https://docutils.sourceforge.io/docs/ref/doctree.html#classes |
| 1652 |
|
| 1653 |
.. rst:directive:option:: name: label |
| 1654 |
:type: text |
| 1655 |
|
| 1656 |
An implicit target name that can be referenced using :rst:role:`ref`. |
| 1657 |
This is a :dudir:`common option <common-options>`. |
| 1658 |
|
| 1659 |
.. rst:directive:option:: label: label |
| 1660 |
:type: text |
| 1661 |
|
| 1662 |
Normally, equations are not numbered. If you want your equation to get a |
| 1663 |
number, use the ``label`` option. When given, it selects an internal label |
| 1664 |
for the equation, by which it can be cross-referenced, and causes an equation |
| 1665 |
number to be issued. See :rst:role:`eq` for an example. The numbering |
| 1666 |
style depends on the output format. |
| 1667 |
|
| 1668 |
.. rst:directive:option:: no-wrap |
| 1669 |
|
| 1670 |
Prevent wrapping of the given math in a math environment. |
| 1671 |
When you give this option, you must make sure |
| 1672 |
yourself that the math is properly set up. |
| 1673 |
For example:: |
| 1674 |
|
| 1675 |
.. math:: |
| 1676 |
:no-wrap: |
| 1677 |
|
| 1678 |
\begin{eqnarray} |
| 1679 |
y & = & ax^2 + bx + c \\ |
| 1680 |
f(x) & = & x^2 + 2xy + y^2 |
| 1681 |
\end{eqnarray} |
| 1682 |
|
| 1683 |
.. versionchanged:: 8.2 |
| 1684 |
|
| 1685 |
The directive option ``:nowrap:`` was renamed to ``:no-wrap:``. |
| 1686 |
|
| 1687 |
The previous name has been retained as an alias, |
| 1688 |
but will be deprecated and removed |
| 1689 |
in a future version of Sphinx. |
| 1690 |
|
| 1691 |
.. _AmSMath LaTeX package: https://www.ams.org/publications/authors/tex/amslatex |
| 1692 |
|
| 1693 |
.. seealso:: |
| 1694 |
|
| 1695 |
:ref:`math-support` |
| 1696 |
Rendering options for math with HTML builders. |
| 1697 |
|
| 1698 |
:confval:`latex_engine` |
| 1699 |
Explains how to configure LaTeX builder to support Unicode literals in |
| 1700 |
math mark-up. |
| 1701 |
|
| 1702 |
|
| 1703 |
Grammar production displays |
| 1704 |
--------------------------- |
| 1705 |
|
| 1706 |
Special markup is available for displaying the productions of a formal grammar. |
| 1707 |
The markup is simple and does not attempt to model all aspects of BNF_ |
| 1708 |
(or any derived forms), but provides enough to allow context-free grammars |
| 1709 |
to be displayed in a way that causes uses of a symbol to be rendered |
| 1710 |
as hyperlinks to the definition of the symbol. |
| 1711 |
There is this directive: |
| 1712 |
|
| 1713 |
.. _BNF: https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form |
| 1714 |
|
| 1715 |
.. rst:directive:: .. productionlist:: [production_group] |
| 1716 |
|
| 1717 |
This directive is used to enclose a group of productions. |
| 1718 |
Each production is given on a single line and consists of a name, |
| 1719 |
separated by a colon from the following definition. |
| 1720 |
If the definition spans multiple lines, each continuation line |
| 1721 |
must begin with a colon placed at the same column as in the first line. |
| 1722 |
Blank lines are not allowed within ``productionlist`` directive arguments. |
| 1723 |
|
| 1724 |
The optional *production_group* directive argument serves to distinguish |
| 1725 |
different sets of production lists that belong to different grammars. |
| 1726 |
Multiple production lists with the same *production_group* |
| 1727 |
thus define rules in the same scope. |
| 1728 |
This can also be used to split the description of a long or complex grammar |
| 1729 |
across multiple ``productionlist`` directives with the same *production_group*. |
| 1730 |
|
| 1731 |
The definition can contain token names which are marked as interpreted text, |
| 1732 |
(e.g. "``sum ::= `integer` "+" `integer```"), |
| 1733 |
to generate cross-references to the productions of these tokens. |
| 1734 |
Such cross-references implicitly refer to productions from the current group. |
| 1735 |
To reference a production from another grammar, the token name |
| 1736 |
must be prefixed with the group name and a colon, e.g. "``other-group:sum``". |
| 1737 |
If the group of the token should not be shown in the production, |
| 1738 |
it can be prefixed by a tilde, e.g., "``~other-group:sum``". |
| 1739 |
To refer to a production from an unnamed grammar, |
| 1740 |
the token should be prefixed by a colon, e.g., "``:sum``". |
| 1741 |
No further reStructuredText parsing is done in the production, |
| 1742 |
so that special characters (``*``, ``|``, etc) do not need to be escaped. |
| 1743 |
|
| 1744 |
Token productions can be cross-referenced outwith the production list |
| 1745 |
by using the :rst:role:`token` role. |
| 1746 |
If you have used a *production_group* argument, |
| 1747 |
the token name must be prefixed with the group name and a colon, |
| 1748 |
e.g., "``my_group:sum``" instead of just "``sum``". |
| 1749 |
Standard :ref:`cross-referencing modifiers <xref-modifiers>` |
| 1750 |
may be used with the ``:token:`` role, |
| 1751 |
such as custom link text and suppressing the group name with a tilde (``~``). |
| 1752 |
|
| 1753 |
The following is an example taken from the Python Reference Manual:: |
| 1754 |
|
| 1755 |
.. productionlist:: |
| 1756 |
try_stmt: try1_stmt | try2_stmt |
| 1757 |
try1_stmt: "try" ":" `suite` |
| 1758 |
: ("except" [`expression` ["," `target`]] ":" `suite`)+ |
| 1759 |
: ["else" ":" `suite`] |
| 1760 |
: ["finally" ":" `suite`] |
| 1761 |
try2_stmt: "try" ":" `suite` |
| 1762 |
: "finally" ":" `suite` |
| 1763 |
|
| 1764 |
|
| 1765 |
.. rubric:: Footnotes |
| 1766 |
|
| 1767 |
.. [#] The LaTeX writer only refers the ``maxdepth`` option of first toctree |
| 1768 |
directive in the document. |
| 1769 |
|
| 1770 |
.. [#] A note on available globbing syntax: you can use the standard shell |
| 1771 |
constructs ``*``, ``?``, ``[...]`` and ``[!...]`` with the feature that |
| 1772 |
these all don't match slashes. A double star ``**`` can be used to |
| 1773 |
match any sequence of characters *including* slashes. |
| 1774 |
|
| 1775 |
.. [#] There is a standard ``.. include`` directive, but it raises errors if the |
| 1776 |
file is not found. This one only emits a warning. |
| 1777 |
|
| 1778 |
.. [#] For most builders name and format are the same. At the moment only |
| 1779 |
builders derived from the html builder distinguish between the builder |
| 1780 |
format and the builder name. |
| 1781 |
|
| 1782 |
Note that the current builder tag is not available in ``conf.py``, it is |
| 1783 |
only available after the builder is initialized. |