Adding Math and Graphviz to Pelican
In the AI era, I felt that math was too important for my next studies and work, so I decided to give Pelican the ability to display mathematical expressions.
MathJax Mathematical Formula
Option selection
Pelican shows mathematical formulas, which can be achieved in three ways:
| Programme | Plugin | Modalities |
|---|---|---|
| KaTeX Rendering | pelican-katex | Convert formulae to HTML/MathML on generation |
| MathJax Browser render | pelican-render-math | Browser loads MathJax.js dynamic rendering |
| LaTeX to SVG | pelican-math-svg | Call LaTeX to compile the formula into a SVG image embedded page |
I compared it to the second.pelican-render-mathIt is easier to do so, using the original operating environment of Pelican.
Install Configuration
UsepipInstallation:
pip install pelican-render-math
If it's working,uvUse:
uv add pelican-render-math
And then..pelicanconf.py, and add:
PLUGINS = ["render_math"]
MATH_JAX = {
"align": "center",
"auto_insert": True,
"process_escapes": True,
"latex_preview": "TeX",
"color": "inherit",
"linebreak_automatic": False,
"responsive": True,
"responsive_break": 768,
"process_summary": True,
}
Comments on parameters
-
"align": "center"- Corresponds to MathJax
displayAlignSettings. - Role: Control block formulae ()
$$...$$) Horizontal alignment. - Optional value:
"left","center","right". Set here to center display.
- Corresponds to MathJax
-
"auto_insert": True- Effect: Whether or not to automatically render mathematical formulae on pages that require plugins
<head>. Inserts JavaScript references and configurations for MathJax. - Effects:
- Set as
True: You do not need to change the theme template manually, the plugin will be smart to judge whether the article contains a formula and load MathJax only on a page with a formula. - Set as
False: You need to add the MathJax load code to the template, which is suitable for a scenario that wants to fully manually control the resource load.
- Set as
- Effect: Whether or not to automatically render mathematical formulae on pages that require plugins
-
"process_escapes": True- Effect: MathJax will first process the conversion character from Markdown in HTML. For example, Markdown will
\$Convert to$Literally, but actually you might want to write LaTeX commands. - Typical impact:
- When written in Markdown
\$x$, the option will be opened first\$Revert$, so that you can correct the formula in the line. - On close,
$Could be invalidated by the Markdown conversion, making the formula unrenderable.
- When written in Markdown
- Recommendation: Maintain
TrueEspecially when you need to use the dollar symbol itself in the article instead of the formula.\$Distinguishing.
- Effect: MathJax will first process the conversion character from Markdown in HTML. For example, Markdown will
-
"latex_preview": "TeX"- Activation: The placeholder content style that is displayed on the page before MathJax is fully retrofitting the fine formula.
- Optional value:
"TeX": Show original LaTeX source code (e. g.)\frac{a}{b}) is not replaced by a beautiful formula until the rendering is complete. This avoids the page layout and lets the reader know that there are formulas in the process."none": without any preview, the formula area may be briefly whited or displayed as placeholders.- Other values may not be valid, and plugins usually deal only with these two options.
- Effects: Set to
"TeX"It provides a better visual experience.
-
"color": "inherit"- Activation: Sets the overall colour of the mathematical formula after rendering.
- Explanation:
"inherit"is the foreground colour of the element(s) of the formula that inherits its HTML element. This means that the formula automatically corresponds to the font colour of the web page, such as when the text is dark gray, the formula is dark gray, and can automatically change colour in dark colour mode. - If set to specific colours:
"black"or"#333", the formula colour is fixed and no longer follows the text colour.
-
"linebreak_automatic": False- Automatic line breaks for MathJax.
- Effect: Controls whether excessively long block formulas allow automatic line breaks within rows.
- Effects:
False: A horizontal scrollbar will occur when the long formula exceeds the width of the container, and the formula will remain single-line.True: Mathematical formulas will be suitable for an operator (e. g.+,=) is automatically replaced to avoid undermining the page layout, but may affect the symmetry of the formula. Here you choose to disable, usually hoping for a full presentation of the formula.
-
"responsive": True- Active: Enables the response rendering of MathJax. When open, the formula will adjust the display policy (e.g., zoom, rearrange) to the width of the screen or container to prevent the formula from being cut off on the small screen mover.
- Dependency: usually with
responsive_breakWhen used in conjunction, triggers special treatment when the width of the view is less than a value.
-
"responsive_break": 768- Activation: Sets the breakpoint width (in pixels) that is triggered by a response.
- Effect: MathJax tries to adapt the formula to a narrow screen (e.g., using scaling or allowing internal line breaks, etc.) when the browser views the width of the mouth. Values can be adjusted according to your theme-based mobile end-of-adaptation scheme.
- Note: This parameter is only available
"responsive": Trueeffective on 31 December 2001.
-
"process_summary": True- Effect: Is the mathematical formula also addressed in the summary of the article (the first rows shown on the article list page)?
- Effects:
- Set as
True: Plugin resolves the summary$...$or$$...$$, and also insert MathJax render them on the list page. This ensures that the original LaTeX code is not shown in the head page or in the summary of the classification page. - Set as
False: The formula in the summary will not be processed and may directly display unrendered codes, affecting reading experience. Recommendation maintainedTrue.
- Set as
Examples of effects
Now we'll try the effect:
A mathematical formula for the same line:\(\dfrac{1}{1+x^2}\)
The code is:
同一行数学公式:$\dfrac{1}{1+x^2}$
Independent line complex mathematical formula:
The code is:
独立行复杂数学公式:
$$
\begin{pmatrix}
a_{11} & a_{12} & \cdots & a_{1n} \\
a_{21} & a_{22} & \cdots & a_{2n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{n1} & a_{n2} & \cdots & a_{nn}
\end{pmatrix}
\begin{pmatrix}
x_1 \\ x_2 \\ \vdots \\ x_n
\end{pmatrix}
=
\lambda
\begin{pmatrix}
x_1 \\ x_2 \\ \vdots \\ x_n
\end{pmatrix}
$$
Graphviz Chart
Besides the mathematical formula, I added the function of the Graphviz diagram, because I felt it was important.
Install a Grampviz program
First of all, you need to install the Graphviz application itself.
- Windows
scoop install graphviz
- macOS
brew install graphviz
- Linux
apt install graphviz
Install Pelican-graphviz plugin
If usedpipInstallation:
pip install pelican-graphviz
If it's working,uvUse:
uv add pelican-graphviz
Yes.pelicanconf.py, and amend:
PLUGINS = ["graphviz", "render_math"]
Examples of effects
A more complex example:
Look at the effect. It's code:
一个比较复杂的例子:
..graphviz dot
digraph finite_state_machine {
rankdir=LR;
size="8,5";
fontsize=10;
ranksep=1.5;
nodesep=1.0;
overlap=false;
splines=true;
node [fontsize=14];
edge [fontsize=12];
node [shape = doublecircle]; S3 [color=red, style=filled, fillcolor=lightpink];
node [shape = circle];
S1 [color=blue, style=filled, fillcolor=lightblue];
S2 [color=darkgreen, style=filled, fillcolor=lightgreen];
subgraph cluster_guard {
label = "守护条件:计数 < 3";
style=dashed;
fontsize=10;
color=purple;
C1 [shape=box, label="错误计数器", style=filled, fillcolor=gold, fontsize=8];
}
S1 -> S2 [ label = "开始", xlabel="动作:重置计数器", fontsize=10, color=blue ];
S2 -> S1 [ label = "停止", fontsize=10, color=darkgreen ];
S2 -> S3 [ label = "错误", xlabel="条件:增加计数器", fontsize=10, color=red ];
S3 -> S2 [ label = "恢复", fontsize=10, color=red ];
S2 -> C1 [style=dotted, arrowhead=none, color=purple];
}
Attention.:..graphviz dotYou can't be free in the code block.