| 1 |
.. highlight:: rst |
| 2 |
|
| 3 |
.. _math-support: |
| 4 |
|
| 5 |
Math support for HTML outputs in Sphinx |
| 6 |
======================================= |
| 7 |
|
| 8 |
.. module:: sphinx.ext.mathbase |
| 9 |
:synopsis: Common math support for imgmath and mathjax / jsmath. |
| 10 |
|
| 11 |
.. versionadded:: 0.5 |
| 12 |
.. versionchanged:: 1.8 |
| 13 |
|
| 14 |
Math support for non-HTML builders is integrated to sphinx-core. |
| 15 |
So mathbase extension is no longer needed. |
| 16 |
|
| 17 |
.. role:: code-py(code) |
| 18 |
:language: Python |
| 19 |
|
| 20 |
Since mathematical notation isn't natively supported by HTML in any way, Sphinx |
| 21 |
gives a math support to HTML document with several extensions. These use the |
| 22 |
reStructuredText math :rst:dir:`directive <math>` and :rst:role:`role <math>`. |
| 23 |
|
| 24 |
:mod:`sphinx.ext.imgmath` -- Render math as images |
| 25 |
-------------------------------------------------- |
| 26 |
|
| 27 |
.. module:: sphinx.ext.imgmath |
| 28 |
:synopsis: Render math as PNG or SVG images. |
| 29 |
|
| 30 |
.. versionadded:: 1.4 |
| 31 |
|
| 32 |
This extension renders math via LaTeX and dvipng_ or dvisvgm_ into PNG or SVG |
| 33 |
images. This of course means that the computer where the docs are built must |
| 34 |
have both programs available. |
| 35 |
|
| 36 |
There are various configuration values you can set to influence how the images |
| 37 |
are built: |
| 38 |
|
| 39 |
.. confval:: imgmath_image_format |
| 40 |
:type: :code-py:`'png' | 'svg'` |
| 41 |
:default: :code-py:`'png'` |
| 42 |
|
| 43 |
The output image format. It should be either |
| 44 |
``'png'`` or ``'svg'``. The image is produced by first executing ``latex`` |
| 45 |
on the TeX mathematical mark-up then (depending on the requested format) |
| 46 |
either `dvipng`_ or `dvisvgm`_. |
| 47 |
|
| 48 |
.. confval:: imgmath_use_preview |
| 49 |
:type: :code-py:`bool` |
| 50 |
:default: :code-py:`False` |
| 51 |
|
| 52 |
``dvipng`` and ``dvisvgm`` both have the ability to collect from LaTeX the |
| 53 |
"depth" of the rendered math: an inline image should use this "depth" in a |
| 54 |
``vertical-align`` style to get correctly aligned with surrounding text. |
| 55 |
|
| 56 |
This mechanism requires the `LaTeX preview package`_ (available as |
| 57 |
``preview-latex-style`` on Ubuntu xenial). Therefore, the default for this |
| 58 |
option is ``False`` but it is strongly recommended to set it to ``True``. |
| 59 |
|
| 60 |
.. versionchanged:: 2.2 |
| 61 |
|
| 62 |
This option can be used with the ``'svg'`` :confval:`imgmath_image_format`. |
| 63 |
|
| 64 |
.. confval:: imgmath_add_tooltips |
| 65 |
:type: :code-py:`bool` |
| 66 |
:default: :code-py:`True` |
| 67 |
|
| 68 |
If false, do not add the LaTeX code as an "alt" attribute for math images. |
| 69 |
|
| 70 |
.. confval:: imgmath_font_size |
| 71 |
:type: :code-py:`int` |
| 72 |
:default: :code-py:`12` |
| 73 |
|
| 74 |
The font size (in ``pt``) of the displayed math. |
| 75 |
This must be a positive integer. |
| 76 |
|
| 77 |
.. confval:: imgmath_latex |
| 78 |
:type: :code-py:`str` |
| 79 |
:default: :code-py:`'latex'` |
| 80 |
|
| 81 |
The command name with which to invoke LaTeX. |
| 82 |
You may need to set this to a full path if ``latex`` is not in the executable |
| 83 |
search path. |
| 84 |
|
| 85 |
Since this setting is not portable from system to system, it is normally not |
| 86 |
useful to set it in ``conf.py``; rather, giving it on the |
| 87 |
:program:`sphinx-build` command line via the :option:`-D <sphinx-build -D>` |
| 88 |
option should be preferable, like this:: |
| 89 |
|
| 90 |
sphinx-build -M html -D imgmath_latex=C:\tex\latex.exe . _build |
| 91 |
|
| 92 |
This value should only contain the path to the latex executable, not further |
| 93 |
arguments; use :confval:`imgmath_latex_args` for that purpose. |
| 94 |
|
| 95 |
.. hint:: |
| 96 |
|
| 97 |
To use `OpenType Math fonts`__ with ``unicode-math``, via a custom |
| 98 |
:confval:`imgmath_latex_preamble`, you can set :confval:`imgmath_latex` |
| 99 |
to ``'dvilualatex'``, but must then set :confval:`imgmath_image_format` |
| 100 |
to ``'svg'``. Note: this has only been tested with ``dvisvgm 3.0.3``. |
| 101 |
It significantly increases image production duration compared to using |
| 102 |
standard ``'latex'`` with traditional TeX math fonts. |
| 103 |
|
| 104 |
__ https://tex.stackexchange.com/questions/425098/which-opentype-math-fonts-are-available |
| 105 |
|
| 106 |
|
| 107 |
.. hint:: |
| 108 |
|
| 109 |
Some fancy LaTeX mark-up (an example was reported which used TikZ to add |
| 110 |
various decorations to the equation) require multiple runs of the LaTeX |
| 111 |
executable. To handle this, set this configuration setting to |
| 112 |
``'latexmk'`` (or a full path to it) as this Perl script reliably |
| 113 |
chooses dynamically how many latex runs are needed. |
| 114 |
|
| 115 |
.. versionchanged:: 6.2.0 |
| 116 |
|
| 117 |
Using ``'xelatex'`` (or a full path to it) is now supported. But you |
| 118 |
must then add ``'-no-pdf'`` to the :confval:`imgmath_latex_args` list of |
| 119 |
the command options. The ``'svg'`` :confval:`imgmath_image_format` is |
| 120 |
required. Also, you may need the ``dvisvgm`` binary to be relatively |
| 121 |
recent (testing was done only with its ``3.0.3`` release). |
| 122 |
|
| 123 |
.. note:: |
| 124 |
|
| 125 |
Regarding the previous note, it is currently not supported to use |
| 126 |
``latexmk`` with option ``-xelatex``. |
| 127 |
|
| 128 |
.. confval:: imgmath_latex_args |
| 129 |
:type: :code-py:`Sequence[str]` |
| 130 |
:default: :code-py:`()` |
| 131 |
|
| 132 |
Additional arguments to give to latex, as a list. |
| 133 |
|
| 134 |
.. confval:: imgmath_latex_preamble |
| 135 |
:type: :code-py:`str` |
| 136 |
:default: :code-py:`''` |
| 137 |
|
| 138 |
Additional LaTeX code to put into the preamble of the LaTeX files used to |
| 139 |
translate the math snippets. Use it |
| 140 |
e.g. to add packages which modify the fonts used for math, such as |
| 141 |
``'\\usepackage{newtxsf}'`` for sans-serif fonts, or |
| 142 |
``'\\usepackage{fouriernc}'`` for serif fonts. Indeed, the default LaTeX |
| 143 |
math fonts have rather thin glyphs which (in HTML output) often do not |
| 144 |
match well with the font for text. |
| 145 |
|
| 146 |
.. confval:: imgmath_dvipng |
| 147 |
:type: :code-py:`str` |
| 148 |
:default: :code-py:`'dvipng'` |
| 149 |
|
| 150 |
The command name to invoke ``dvipng``. |
| 151 |
You may need to set this to a full path if ``dvipng`` is not in |
| 152 |
the executable search path. This option is only used when |
| 153 |
``imgmath_image_format`` is set to ``'png'``. |
| 154 |
|
| 155 |
.. confval:: imgmath_dvipng_args |
| 156 |
:type: :code-py:`Sequence[str]` |
| 157 |
:default: :code-py:`('-gamma', '1.5', '-D', '110', '-bg', 'Transparent')` |
| 158 |
|
| 159 |
Additional arguments to give to dvipng, as a list. |
| 160 |
The default value makes the image a bit darker and larger |
| 161 |
than it is by default (this compensates |
| 162 |
somewhat for the thinness of default LaTeX math fonts), and produces PNGs with a |
| 163 |
transparent background. This option is used only when |
| 164 |
``imgmath_image_format`` is ``'png'``. |
| 165 |
|
| 166 |
.. confval:: imgmath_dvisvgm |
| 167 |
:type: :code-py:`str` |
| 168 |
:default: :code-py:`'dvisvgm'` |
| 169 |
|
| 170 |
The command name to invoke ``dvisvgm``. |
| 171 |
You may need to set this to a full path if ``dvisvgm`` is not |
| 172 |
in the executable search path. This option is only used when |
| 173 |
``imgmath_image_format`` is ``'svg'``. |
| 174 |
|
| 175 |
.. confval:: imgmath_dvisvgm_args |
| 176 |
:type: :code-py:`Sequence[str]` |
| 177 |
:default: :code-py:`('--no-fonts',)` |
| 178 |
|
| 179 |
Additional arguments to give to dvisvgm, as a list. |
| 180 |
The default value means that ``dvisvgm`` will render glyphs as path |
| 181 |
elements (cf the `dvisvgm FAQ`_). This option is used only when |
| 182 |
``imgmath_image_format`` is ``'svg'``. |
| 183 |
|
| 184 |
.. confval:: imgmath_embed |
| 185 |
:type: :code-py:`bool` |
| 186 |
:default: :code-py:`False` |
| 187 |
|
| 188 |
If true, encode LaTeX output images within HTML files |
| 189 |
(base64 encoded) and do not save separate png/svg files to disk. |
| 190 |
|
| 191 |
.. versionadded:: 5.2 |
| 192 |
|
| 193 |
:mod:`sphinx.ext.mathjax` -- Render math via JavaScript |
| 194 |
------------------------------------------------------- |
| 195 |
|
| 196 |
.. module:: sphinx.ext.mathjax |
| 197 |
:synopsis: Render math using JavaScript via MathJax. |
| 198 |
|
| 199 |
.. attention:: |
| 200 |
|
| 201 |
The default version of MathJax changed |
| 202 |
from version 2 to version 3 in Sphinx 4.0, and |
| 203 |
from version 3 to version 4 in Sphinx 9.0. |
| 204 |
|
| 205 |
You may need to override :confval:`mathjax_path` to load an older version, |
| 206 |
or update your :confval:`configuration options <mathjax4_config>`. |
| 207 |
In general, MathJax v3 and v4 options are compatible. |
| 208 |
|
| 209 |
.. versionadded:: 1.1 |
| 210 |
|
| 211 |
This extension puts math as-is into the HTML files. The JavaScript package |
| 212 |
MathJax_ is then loaded and transforms the LaTeX markup to readable math live in |
| 213 |
the browser. |
| 214 |
|
| 215 |
Because MathJax (and the necessary fonts) is very large, it is not included in |
| 216 |
Sphinx but is set to automatically include it from a third-party site. |
| 217 |
|
| 218 |
.. attention:: |
| 219 |
|
| 220 |
You should use the math :rst:dir:`directive <math>` and |
| 221 |
:rst:role:`role <math>`, not the native MathJax ``$$``, ``\(``, etc. |
| 222 |
|
| 223 |
.. tip:: |
| 224 |
|
| 225 |
MathJax configuration can be supplied in a JavaScript file |
| 226 |
by using the :confval:`mathjax_config_path` option. |
| 227 |
This is useful for more complex configurations that are hard to express |
| 228 |
only using a Python dictionary, for example JavaScript functions. |
| 229 |
|
| 230 |
|
| 231 |
.. confval:: mathjax_path |
| 232 |
:type: :code-py:`str` |
| 233 |
:default: :code-py:`'https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js'` |
| 234 |
|
| 235 |
The path to the JavaScript file to include in the HTML files in order to load |
| 236 |
MathJax. |
| 237 |
|
| 238 |
The default is the ``https://`` URL that loads the JS files from the |
| 239 |
`jsdelivr`__ Content Delivery Network. See the `MathJax Getting Started |
| 240 |
page`__ for details. If you want MathJax to be available offline or |
| 241 |
without including resources from a third-party site, you have to |
| 242 |
download it and set this value to a different path. |
| 243 |
|
| 244 |
__ https://www.jsdelivr.com/ |
| 245 |
|
| 246 |
__ https://www.mathjax.org/#gettingstarted |
| 247 |
|
| 248 |
The path can be absolute or relative; if it is relative, it is relative to |
| 249 |
the ``_static`` directory of the built docs. |
| 250 |
|
| 251 |
For example, if you put MathJax into the static path of the Sphinx docs, this |
| 252 |
value would be ``MathJax/MathJax.js``. If you host more than one Sphinx |
| 253 |
documentation set on one server, it is advisable to install MathJax in a |
| 254 |
shared location. |
| 255 |
|
| 256 |
You can also give a full ``https://`` URL different from the CDN URL. |
| 257 |
|
| 258 |
.. versionchanged:: 4.0 |
| 259 |
The default version of MathJax is now version 3. |
| 260 |
To keep using MathJax v2, you must explicitly load it via this option. |
| 261 |
For example: |
| 262 |
|
| 263 |
.. code-block:: python |
| 264 |
|
| 265 |
mathjax_path = 'https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML' |
| 266 |
|
| 267 |
.. versionchanged:: 9.0 |
| 268 |
The default version of MathJax is now version 4. |
| 269 |
To keep using MathJax v3, you must explicitly load it via this option. |
| 270 |
For example: |
| 271 |
|
| 272 |
.. code-block:: python |
| 273 |
|
| 274 |
mathjax_path = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js' |
| 275 |
|
| 276 |
.. confval:: mathjax_options |
| 277 |
:type: :code-py:`dict[str, Any]` |
| 278 |
:default: :code-py:`{}` |
| 279 |
|
| 280 |
The options to script tag for mathjax. For example, you can set integrity |
| 281 |
option with following setting:: |
| 282 |
|
| 283 |
mathjax_options = { |
| 284 |
'integrity': 'sha384-......', |
| 285 |
} |
| 286 |
|
| 287 |
.. versionadded:: 1.8 |
| 288 |
|
| 289 |
.. versionchanged:: 4.4.1 |
| 290 |
|
| 291 |
Allow to change the loading method (async or defer) of MathJax if "async" |
| 292 |
or "defer" key is set. |
| 293 |
|
| 294 |
.. confval:: mathjax4_config |
| 295 |
:type: :code-py:`dict[str, Any] | None` |
| 296 |
:default: :code-py:`None` |
| 297 |
|
| 298 |
The configuration options for MathJax v4 (which is used by default). |
| 299 |
The given dictionary is assigned to the JavaScript variable |
| 300 |
``window.MathJax``. |
| 301 |
For more information, please read `Configuring MathJax`__. |
| 302 |
|
| 303 |
__ https://docs.mathjax.org/en/stable/web/configuration.html#configuration |
| 304 |
|
| 305 |
|
| 306 |
For help converting your old MathJax configuration to to the new |
| 307 |
:confval:`mathjax4_config`, see `Converting your old Configuration to v4`__. |
| 308 |
|
| 309 |
__ https://docs.mathjax.org/en/stable/web/ |
| 310 |
configuration.html#converting-your-old-configuration-to-v4 |
| 311 |
|
| 312 |
.. versionadded:: 9.0 |
| 313 |
|
| 314 |
.. confval:: mathjax3_config |
| 315 |
:type: :code-py:`dict[str, Any] | None` |
| 316 |
:default: :code-py:`None` |
| 317 |
|
| 318 |
The configuration options for MathJax v3 (which can be loaded via |
| 319 |
:confval:`mathjax_path`). |
| 320 |
If given, the dictionary is converted to a JSON object |
| 321 |
and assigned to the JavaScript variable ``window.MathJax``. |
| 322 |
|
| 323 |
For more information, please read `Configuring MathJax`__. |
| 324 |
|
| 325 |
__ https://docs.mathjax.org/en/v3.2/web/configuration.html#configuration |
| 326 |
|
| 327 |
.. versionadded:: 4.0 |
| 328 |
|
| 329 |
.. confval:: mathjax2_config |
| 330 |
:type: :code-py:`dict[str, Any] | None` |
| 331 |
:default: :code-py:`None` |
| 332 |
|
| 333 |
The configuration options for MathJax v2 (which can be loaded via |
| 334 |
:confval:`mathjax_path`). |
| 335 |
The value is used as a parameter of ``MathJax.Hub.Config()``. |
| 336 |
For more information, please read `Using in-line configuration options`__. |
| 337 |
|
| 338 |
__ https://docs.mathjax.org/en/v2.7/ |
| 339 |
configuration.html#using-in-line-configuration-options |
| 340 |
|
| 341 |
For example:: |
| 342 |
|
| 343 |
mathjax2_config = { |
| 344 |
'extensions': ['tex2jax.js'], |
| 345 |
'jax': ['input/TeX', 'output/HTML-CSS'], |
| 346 |
} |
| 347 |
|
| 348 |
For help converting your old MathJax configuration to to the new |
| 349 |
:confval:`mathjax3_config`, see `Converting Your v2 Configuration to v3`__. |
| 350 |
|
| 351 |
__ https://docs.mathjax.org/en/v3.2/web/ |
| 352 |
configuration.html#converting-your-v2-configuration-to-v3 |
| 353 |
|
| 354 |
.. versionadded:: 4.0 |
| 355 |
|
| 356 |
:confval:`mathjax_config` has been renamed to :confval:`mathjax2_config`. |
| 357 |
|
| 358 |
.. confval:: mathjax_config |
| 359 |
:type: :code-py:`dict[str, Any] | None` |
| 360 |
:default: :code-py:`None` |
| 361 |
|
| 362 |
Former name of :confval:`mathjax2_config`. |
| 363 |
|
| 364 |
.. versionadded:: 1.8 |
| 365 |
|
| 366 |
.. versionchanged:: 4.0 |
| 367 |
|
| 368 |
This has been renamed to :confval:`mathjax2_config`. |
| 369 |
:confval:`mathjax_config` is still supported for backwards compatibility. |
| 370 |
|
| 371 |
.. confval:: mathjax_config_path |
| 372 |
:type: :code-py:`str` |
| 373 |
:default: :code-py:`''` |
| 374 |
|
| 375 |
If given, this must be the path of a JavaScript (:file:`.js`) file |
| 376 |
(path relative to the :term:`configuration directory`) |
| 377 |
that contains the configuration options for MathJax. |
| 378 |
Example: |
| 379 |
|
| 380 |
.. code-block:: python |
| 381 |
|
| 382 |
mathjax_config_path = 'mathjax-config.js' |
| 383 |
|
| 384 |
.. important:: The user is responsible for ensuring that the given file |
| 385 |
is compatible with the version of MathJax being used. |
| 386 |
|
| 387 |
For more information, please read `Configuring MathJax`__. |
| 388 |
|
| 389 |
__ https://docs.mathjax.org/en/latest/web/configuration.html#configuration |
| 390 |
|
| 391 |
.. versionadded:: 9.0 |
| 392 |
|
| 393 |
:mod:`sphinxcontrib.jsmath` -- Render math via JavaScript |
| 394 |
--------------------------------------------------------- |
| 395 |
|
| 396 |
.. module:: sphinxcontrib.jsmath |
| 397 |
:synopsis: Render math using JavaScript via JSMath. |
| 398 |
|
| 399 |
This extension works just as the MathJax extension does, but uses the older |
| 400 |
package jsMath_. jsMath is no longer actively developed, but it has the |
| 401 |
advantage that the size of the JavaScript package is much smaller than |
| 402 |
MathJax. |
| 403 |
|
| 404 |
.. versionadded:: 0.5 |
| 405 |
The :mod:`!sphinx.ext.jsmath` extension. |
| 406 |
.. versionchanged:: 2.0 |
| 407 |
:mod:`!sphinx.ext.jsmath` was moved to :mod:`sphinxcontrib.jsmath`. |
| 408 |
.. versionremoved:: 4.0 |
| 409 |
The alias from :mod:`!sphinx.ext.jsmath` to :mod:`sphinxcontrib.jsmath`. |
| 410 |
|
| 411 |
Config value: |
| 412 |
|
| 413 |
.. confval:: jsmath_path |
| 414 |
:type: :code-py:`str` |
| 415 |
:default: :code-py:`''` |
| 416 |
|
| 417 |
The path to the JavaScript file to include in the HTML files in order to load |
| 418 |
JSMath. |
| 419 |
|
| 420 |
The path can be absolute or relative; if it is relative, it is relative to |
| 421 |
the ``_static`` directory of the built docs. |
| 422 |
|
| 423 |
For example, if you put jsMath into the static path of the Sphinx docs, this |
| 424 |
value would be ``jsMath/easy/load.js``. If you host more than one |
| 425 |
Sphinx documentation set on one server, it is advisable to install jsMath in |
| 426 |
a shared location. |
| 427 |
|
| 428 |
|
| 429 |
.. _dvipng: https://savannah.nongnu.org/projects/dvipng/ |
| 430 |
.. _dvisvgm: https://dvisvgm.de/ |
| 431 |
.. _dvisvgm FAQ: https://dvisvgm.de/FAQ |
| 432 |
.. _MathJax: https://www.mathjax.org/ |
| 433 |
.. _jsMath: https://www.math.union.edu/~dpvc/jsMath/ |
| 434 |
.. _LaTeX preview package: https://www.gnu.org/software/auctex/preview-latex.html |