Draw LaTeX equations
When a distribution is installed, this package can render equations. We recommend as the runtime.
1. Draw a simple equation
In the first example, we show the rendering of the following simple equation:
stet.ImageMultiLayer((2700, 150), fmt="png").add_latex(
equation="E = m c^2.", name="eq1", font_size="h1", color="#ffffff"
).add_background().convert("RGB")
example-eq1.png2. Use Chinese characaters
When writing Chinese articles, users may need to put CJK characters. For example,
We offer a template that can be used in this case:
stet.ImageMultiLayer((2700, 200), fmt="png").add_latex(
equation=(
R"\left\{ \sigma \middle| \sigma \textrm{是} \mathbb{A} "
R"\rightarrow \mathbb{B} \textrm{的双射} \right\}."
),
name="eq1",
template="chinese",
font_size="h1",
color="#ffffff",
).add_background().convert("RGB")
example-eq2.png3. Draw complicated equations
The following example renders the Maxwell equations:
In previous examples, we only rendered one-line equations. This complicated example contains multi-line equations, so we need to use a different template. We also add more styles that make the equations prettier:
equation = (
R"\left\{\begin{aligned}"
R"\nabla \times \mathbf{E} &= - \mu \frac{\partial \mathbf{H}}{\partial t}, \\"
R"\nabla \times \mathbf{H} &= \varepsilon "
R"\frac{\partial \mathbf{E}}{\partial t} + \sigma \mathbf{E} + \mathbf{J}."
R"\end{aligned}\right."
)
stet.ImageMultiLayer((2700, 500), fmt="png").add_latex(
equation=equation,
name="eq1",
template="multilines",
font_size="h1",
color="#ffffff",
stroke_color="#000000",
shadow_color="#000000",
).add_background().convert("RGB").save("alpha-latex.png", quality="medium")
example-eq3.png
This example can be found here:
4. Templates
By default, we allow the following templates:
"default": The default template used for rendering simple equations. It has enabledequationandalignedwrappers."multilines": The template automatically add theequationenvironment allowing compliated line structures."chinese": The default template with CJK features enabled."multilines_chinese": The"multilines"template with CJK features enabled.
We allow users to create their own templates. For example, the "default" template can be implemented by:
from steam_editor_tools.improc import TeXTemplate
TeXTemplate(
prep=R"""
\documentclass[preview,border=5pt]{standalone}
\usepackage{amsmath, amssymb}
\begin{document}
\begin{equation*}
\begin{aligned}
""",
end=R"""
\end{aligned}
\end{equation*}
\end{document}
""",
)