Skip to main content

Draw LaTeX equations

When a LaTeX\LaTeX distribution is installed, this package can render equations. We recommend TeXLive\TeX\textrm{Live} as the runtime.

1. Draw a simple equation​

In the first example, we show the rendering of the following simple equation:

E=mc2.\begin{align} E = m c^2. \end{align}
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.png

example-eq1.png

2. Use Chinese characaters​

When writing Chinese articles, users may need to put CJK characters. For example,

{σ|σ是A→B的双射}.\begin{align} \left\{ \sigma \middle| \sigma \text{是} \mathbb{A} \rightarrow \mathbb{B} \text{的双射} \right\}. \end{align}

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.png

example-eq2.png

3. Draw complicated equations​

The following example renders the Maxwell equations:

{∇×E=−μ∂H∂t,∇×H=ε∂E∂t+σE+J.\begin{equation} \left\{\begin{aligned} \nabla \times \mathbf{E} &= - \mu \frac{\partial \mathbf{H}}{\partial t}, \\ \nabla \times \mathbf{H} &= \varepsilon\frac{\partial \mathbf{E}}{\partial t} + \sigma \mathbf{E} + \mathbf{J}. \end{aligned}\right. \end{equation}

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

example-eq3.png

This example can be found here:

render_latex_equations.py

4. Templates​

By default, we allow the following templates:

  • "default": The default template used for rendering simple equations. It has enabled equation and aligned wrappers.
  • "multilines": The template automatically add the equation environment 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}
""",
)