pelican-plugins / render-math

Pelican plugin that renders mathematics via the MathJax JavaScript engine
Other
30 stars 7 forks source link

Render Math Plugin for Pelican

Build Status PyPI Version Downloads License

This plugin gives Pelican the ability to render mathematics inside post content. It accomplishes this by using the MathJax JavaScript engine.

The plugin also makes Typogrify and recognized math play nicely together by ensuring Typogrify does not alter math content.

Both Markdown and reStructuredText are supported.

Requirements

Installation

This plugin can be installed via:

python -m pip install pelican-render-math

As long as you have not explicitly added a PLUGINS setting to your Pelican settings file, then the newly-installed plugin should be automatically detected and enabled. Otherwise, you must add render_math to your existing PLUGINS list. For more information, please see the How to Use Plugins documentation.

Once the plugin has been installed according to those instructions, your site should be capable of rendering math using the MathJax JavaScript engine. No alterations to the template are needed — just use and enjoy!

However, if you wish, you can set the auto_insert setting to False which will disable the MathJax script from being automatically inserted into the content. You would only want to do this if you have control over the template and want to insert the script manually.

Typogrify

In the past, using Typogrify would alter math content, resulting in math that could not be rendered by MathJax. The only option was to ensure that Typogrify was disabled in the settings.

The problem has been rectified in this plugin, but it requires at a minimum Typogrify version 2.0.7 or higher. If this version is not present, the plugin will disable Typogrify for the entire site.

BeautifulSoup

Pelican creates summaries by truncating the post content to a specified user length. The truncation process is oblivious to any math and can therefore destroy the math output in the summary.

To restore math, BeautifulSoup is used. If it is not installed, no summary processing will happen.

Usage

Templates

No template alteration is needed for this plugin to work. Just install the plugin and start writing your math.

Settings

Certain MathJax rendering options can be set. These options are in a dictionary variable called MATH_JAX in the Pelican settings file.

The dictionary can be set with the following keys:

Settings Examples

Make math render in blue and display math aligned to the left:

MATH_JAX = {'color':'blue','align':left}

Use the color and mhchem extensions:

MATH_JAX = {'tex_extensions': ['color.js','mhchem.js']}

Resulting HTML

Inline math is wrapped in span tags, while displayed math is wrapped in div tags. These tags will have a class attribute that is set to math which can be used by template designers to alter the display of the math.

Markdown

This plugin implements a custom extension for Markdown resulting in math being a "first class citizen" for Pelican.

Inline Math

Math between $..$, for example, $x^2$, will be rendered inline with respect to the current HTML block. Note: To use inline math, there must not be any whitespace before the ending $. So for example:

Displayed Math

Math between $$..$$ will be rendered "block style", for example, $$x^2$$, will be rendered centered in a new paragraph.

Other Latex Display Math Commands

The other LaTeX commands which usually invoke display math mode from text mode are supported, and are automatically treated like $$-style displayed math in that they are rendered "block" style on their own lines. For example, \begin{equation} x^2 \end{equation}, will be rendered in its own block with a right justified equation number at the top of the block. This equation number can be referenced in the document. To do this, use a label inside of the equation format and then refer to that label using ref. For example: \begin{equation} \label{eq} X^2 \end{equation}. Now refer to that equation number by $\ref{eq}$.

reStructuredText

If there is math detected in reStructuredText content, the plugin will automatically set the math_output configuration setting to mathjax.

Inline Math

Inline math needs to use the math role:

The area of a circle is :math:`A_\text{c} = (\pi/4) d^2`.

Displayed Math

Displayed math uses the math block:

.. math::

   α_t(i) = P(O_1, O_2, … O_t, q_t = S_i λ)

Enabling Additional Features

In order for \frac, equation numbering, and other features to work, the MathJAX JavaScript file must be added to the HTML file, either via your theme's template configuration (if supported) or by editing the relevant template file directly. The current MathJAX CDN is hosted at http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML, and the code to add it to the HTML is provided below.

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

As long as this script is loaded in either the head or body of the HTML output, the complex expressions are also converted.

Contributing

Contributions are welcome and much appreciated. Every little bit helps. You can contribute by improving the documentation, adding missing features, and fixing bugs. You can also help out by reviewing and commenting on existing issues.

To start contributing to this plugin, review the Contributing to Pelican documentation, beginning with the Contributing Code section.

License

This project is licensed under the AGPL-3.0 license.