Skip to main content

texext contains a couple of Sphinx extensions for working with LaTeX math.

math_dollar

math_dollar replaces math expressions between dollars in ReST with equivalent inline math.

For example:

Here is some math: $a = 2$

will be replaced by:

Here is some math: :math:`a = 2`

The extension makes some effort not to replace dollars that aren’t meant as math, but please check your output carefully, and submit an issue on the texext issue tracker if we have messed up.

To enable math_dollar, make sure that the texext package is on your Python path, and add texext.math_dollar to your list of extensions in the Sphinx conf.py. If you want math_dollar to process docstrings, you should add sphinx.ext.autodoc higher up your extensions list than math_dollar.

mathcode directive

Users of sympy may want to generate LaTeX expressions dynamically in Sympy, and then render them in LaTeX in the built pages. You can do this with the mathcode directive:

.. mathcode::

    import sympy
    a, b = sympy.symbols('a, b')
    a * 10 + 2 * b

The directive runs sympy.latex() on the return result of the final expression, and embeds it in a .. math:: directive, resulting in equivalent output to sphinx of:

.. math::

    10 a + 2 b

Context (namespace) is preserved by default, so you can use context in subsequent directives, e.g.:

.. mathcode::

    a * 5 + 3 * b

If the last expression in the mathcode block is not an expression, the context gets updated, but the extension generates no math directive to the output. This allows you to have blocks that fill in calculations without rendering to the page. For example, this generates no output:

.. mathcode::

    expr = a * 4

You can use the generated context in a later directive:

.. mathcode::

    expr

To reset the context (namespace), use the newcontext option:

.. mathcode::
    :newcontext:

    import sympy  # again

If you would like mathcode to share a namespace with the matplotlib plot_directive, set the following in your conf.py:

# Config of mathcode directive
mathcode_use_plot_ns = True

If you are using Nb2plots for your plots, and you want Mathcode to share a namespace with the Nb2plots plot directive, you will need to specify the Nb2plots plot context directly:

# Config of mathcode directive
mathcode_use_plot_ns = True
mathcode_plot_context = 'nb2plots.nbplots.plot_context'

More generally if you want to work with a customized version of the plot_directive, you need to supply the name of the plot context dictionary for the plot directive, as a string. For example, if you have a custom plot directive module importable as import my_path.plot_directive, with the plot context in my_path.plot_directive.plot_context, then your conf.py should have lines like these:

# Config of mathcode directive
mathcode_plot_context = "my_path.plot_directive.plot_context"

The plot context is a string rather than the attribute itself in order to let sphinx pickle the configuration between runs. This allows sphinx to avoid building pages that have not changed between calls to sphinx-build.

To enable the mathcode directive, make sure that the texext package is on your Python path, and add textext.mathcode to your list of extensions in the Sphinx conf.py.

Code

See https://github.com/matthew-brett/texext

Released under the BSD two-clause license - see the file LICENSE in the source distribution.

Github Actions kindly tests the code automatically under Python 3.9 through 3.12.

The latest released version is at https://pypi.python.org/pypi/texext

Support

Please put up issues on the texext issue tracker.

Metadata

Release files for texext 0.6.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for texext 0.6.8
File Size Uploaded
texext-0.6.8.tar.gz 41.7 kB Details

Release files / texext-0.6.8.tar.gz

Download URL texext-0.6.8.tar.gz
Size 41.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6c3ccc5578f93a1bd0051fcba25e92d259620d3d9c9031738570e7d78603e7a3
BLAKE2b-256 checksum
How to use checksums
a6735dc7e7955e4c5a45de3ef1467cfe498233cc3aba2a3c50ff41a55d667c42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.6.8 This release

1 release file

0.6.7

1 release file

0.6.6

1 release file

0.6.5

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page