LaTeX math formulas rendering | Python via .NET
Rendering a LaTeX math formula to PNG
If needed, take a look at the API reference section for this topic. In fact, the best way to demonstrate the LaTeX math formula rendering feature is to start with an example. Here it is:
Set ASPOSE_TEX_OUTPUT_DIR to your output directory, or use the default output folder. All LaTeX input is included in the examples; no external input files are needed. Apply a license first as described in
Licensing.
1# Render a highlighted LaTeX math formula to PNG.
2import os
3from pathlib import Path
4from aspose.pydrawing import Color
5from aspose.tex.features import PngMathRenderer, PngMathRendererOptions
6
7# Set an output directory, or use the default output folder.
8output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
9output_dir.mkdir(parents=True, exist_ok=True)
10
11# Create rendering options setting the image resolution to 150 dpi.
12options = PngMathRendererOptions()
13options.resolution = 150
14# Specify the preamble.
15options.preamble = r"""\usepackage{amsmath}
16\usepackage{amsfonts}
17\usepackage{amssymb}
18\usepackage{color}"""
19# Specify the scaling factor 300%.
20options.scale = 3000
21# Leave space around the rendered fragment.
22options.margin = 5
23# Specify the foreground color.
24options.text_color = Color.black
25# Specify the background color.
26options.background_color = Color.white
27# Specify whether to show the terminal output on the console or not.
28options.show_terminal = False
29
30# Create binary streams for the rendering log and formula image.
31with (output_dir / "math-formula.png.log").open("wb") as log_stream, \
32 (output_dir / "math-formula.png").open("wb") as stream:
33 options.log_stream = log_stream
34 # Run rendering.
35 size = PngMathRenderer().render(r"""\begin{equation*}
36e^x = x^{\color{red}0} + x^{\color{red}1} + \frac{x^{\color{red}2}}{2} + \frac{x^{\color{red}3}}{6} + \cdots = \sum_{n\geq 0} \frac{x^{\color{red}n}}{n!}
37\end{equation*}""", stream, options)
38
39# Show other results.
40print(options.error_report or "No rendering errors.")
41print()
42print(f"Size: {size.width}x{size.height}")Let’s get to the details. Firstly, we initiate a rendering options object, similar to the TeX/LaTeX typesetting, which includes the specification for the desired output image resolution.
Next, we specify the preamble. The default preamble is:
1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}which offers enhanced support for mathematical formulas compared to standard LaTeX. For instance, you can include the color package to customize the highlighting in the formula, as demonstrated in the provided code example.
Then we set scale = 3000 for 300% scaling and margin = 5 to leave space around the rendered fragment. The PNG example uses resolution = 150 DPI.
The next two options determine the foreground and background colors. Any parts of the formula that are not affected by the custom highlighting will be displayed in the text_color color.
The example assigns a binary file stream to log_stream, saving the rendering log as math-formula.png.log beside math-formula.png. Both streams are closed automatically by the with statement.
The show_terminal option controls terminal output in the console. These examples set it to False; the log is still written to the file.
The actual rendering process is carried out by the MathRenderer.render() method, which returns the size of the formula in points.
The second argument is the binary output stream opened for the image. The third argument supplies the rendering options.
And finally, the MathRenderer.render() method is invoked, with the options being passed as the third argument. The LaTeX code of the formula is provided as the first argument.
The final lines print the rendering error report, or No rendering errors. when it is empty, followed by the width and height in points.
Here is the result of the rendering.
This represents the most common and broad application of the LaTeX math formula rendering functionality.
You can also explore the free web app, which utilizes the implemented feature of the Aspose.TeX for .NET API.
Rendering a LaTeX math formula to SVG
Similarly, we can convert a LaTeX math formula into SVG format using the same approach.
1# Render a highlighted LaTeX math formula to SVG.
2import os
3from pathlib import Path
4from aspose.pydrawing import Color
5from aspose.tex.features import SvgMathRenderer, SvgMathRendererOptions
6
7# Set an output directory, or use the default output folder.
8output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
9output_dir.mkdir(parents=True, exist_ok=True)
10
11# Create rendering options.
12options = SvgMathRendererOptions()
13# Specify the preamble.
14options.preamble = r"""\usepackage{amsmath}
15\usepackage{amsfonts}
16\usepackage{amssymb}
17\usepackage{color}"""
18# Specify the scaling factor 300%.
19options.scale = 3000
20# Leave space around the rendered fragment.
21options.margin = 5
22# Specify the foreground color.
23options.text_color = Color.black
24# Specify the background color.
25options.background_color = Color.white
26# Specify whether to show the terminal output on the console or not.
27options.show_terminal = False
28
29# Create binary streams for the rendering log and formula image.
30with (output_dir / "math-formula.svg.log").open("wb") as log_stream, \
31 (output_dir / "math-formula.svg").open("wb") as stream:
32 options.log_stream = log_stream
33 # Run rendering.
34 size = SvgMathRenderer().render(r"""\begin{equation*}
35e^x = x^{\color{red}0} + x^{\color{red}1} + \frac{x^{\color{red}2}}{2} + \frac{x^{\color{red}3}}{6} + \cdots = \sum_{n\geq 0} \frac{x^{\color{red}n}}{n!}
36\end{equation*}""", stream, options)
37
38# Show other results.
39print(options.error_report or "No rendering errors.")
40print()
41print(f"Size: {size.width}x{size.height}")This example creates math-formula.svg and its rendering log, math-formula.svg.log, in the same output directory.
The differences are:
- Instead of using the PngMathRendererOptions class, we utilize the SvgMathRendererOptions class.
- We don’t specify resolution.
- Instead of using the PngMathRenderer class, we use the SvgMathRenderer class.
Here is the result:
You can also explore the free web app, which utilizes the implemented feature of the Aspose.TeX for .NET API.
