LaTeX Figure rendering | Python via .NET

In certain situations, you may need to extract specific content from a LaTeX file as a standalone rendered piece, commonly referred to as a figure, without any connection to the page layout. This could be useful, for example, when creating illustrations for an online publication. Our API allows you to accomplish this. The target formats for rendering figures are PNG and SVG, just like with the LaTeX math formula rendering feature. It is worth noting that LaTeX figure rendering is a more general feature compared to LaTeX math formula rendering.

Rendering a LaTeX figure to PNG

If needed, first take a look at the API reference section for this topic. Similar to formula rendering, we will begin 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 LaTeX figure with a triangle and formulas to PNG.
 2import os
 3from pathlib import Path
 4from aspose.pydrawing import Color
 5from aspose.tex.features import PngFigureRenderer, PngFigureRendererOptions
 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 = PngFigureRendererOptions()
13options.resolution = 150
14# Specify the preamble.
15options.preamble = r"\usepackage{pict2e}"
16# Specify the scaling factor 300%.
17options.scale = 3000
18# Leave space around the rendered fragment.
19options.margin = 5
20# Specify the background color.
21options.background_color = Color.white
22# Specify whether to show the terminal output on the console or not.
23options.show_terminal = False
24
25# Create binary streams for the rendering log and figure image.
26with (output_dir / "text-and-formula.png.log").open("wb") as log_stream, \
27        (output_dir / "text-and-formula.png").open("wb") as stream:
28    options.log_stream = log_stream
29    # Run rendering.
30    size = PngFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
31\begin{picture}(6,5)
32\thicklines
33\put(1,0.5){\line(2,1){3}} \put(4,2){\line(-2,1){2}} \put(2,3){\line(-2,-5){1}} \put(0.7,0.3){$A$} \put(4.05,1.9){$B$} \put(1.7,2.95){$C$}
34\put(3.1,2.5){$a$} \put(1.3,1.7){$b$} \put(2.5,1.05){$c$} \put(0.3,4){$F=\sqrt{s(s-a)(s-b)(s-c)}$} \put(3.5,0.4){$\displaystyle s:=\frac{a+b+c}{2}$}
35\end{picture}""", stream, options)
36
37# Show other results.
38print(options.error_report or "No rendering errors.")
39print()
40print(f"Size: {size.width}x{size.height}")

To begin, we create an instance of the LaTeX figure renderer options class, where we simultaneously specify the output image resolution.

Next, we need to specify the preamble. Unlike LaTeX math formula rendering, there is no default preamble for LaTeX figure rendering. Therefore, if you are planning to render graphics created using the pict2e LaTeX package, for example, you will need to specify it in the preamble:

1\usepackage{pict2e}

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 option determines the background color. Unlike with math formula rendering, we do not need to specify a foreground color, as we assume that the colors are entirely controlled by the LaTeX code. Similarly, the background color is also controlled by the LaTeX code, so this option is simply provided for convenience.

The example assigns a binary file stream to log_stream, saving the rendering log as text-and-formula.png.log beside text-and-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 method responsible for rendering the figure is FigureRenderer.render() . It returns the size of the figure in points.

The second argument is the binary output stream opened for the image. The third argument supplies the rendering options.

Finally, we invoke the FigureRenderer.render() method itself, passing the options as the third argument. The LaTeX code of the figure 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 rendering.

LaTeX Figure rendering to PNG

This represents the most general use case for the LaTeX figure rendering feature.

Rendering a LaTeX figure to SVG

Similarly, we can also render a LaTeX figure in SVG format.

 1# Render a LaTeX figure with a triangle and formulas to SVG.
 2import os
 3from pathlib import Path
 4from aspose.pydrawing import Color
 5from aspose.tex.features import SvgFigureRenderer, SvgFigureRendererOptions
 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 = SvgFigureRendererOptions()
13# Specify the preamble.
14options.preamble = r"\usepackage{pict2e}"
15# Specify the scaling factor 300%.
16options.scale = 3000
17# Leave space around the rendered fragment.
18options.margin = 5
19# Specify the background color.
20options.background_color = Color.white
21# Specify whether to show the terminal output on the console or not.
22options.show_terminal = False
23
24# Create binary streams for the rendering log and figure image.
25with (output_dir / "text-and-formula.svg.log").open("wb") as log_stream, \
26        (output_dir / "text-and-formula.svg").open("wb") as stream:
27    options.log_stream = log_stream
28    # Run rendering.
29    size = SvgFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
30\begin{picture}(6,5)
31\thicklines
32\put(1,0.5){\line(2,1){3}} \put(4,2){\line(-2,1){2}} \put(2,3){\line(-2,-5){1}} \put(0.7,0.3){$A$} \put(4.05,1.9){$B$} \put(1.7,2.95){$C$}
33\put(3.1,2.5){$a$} \put(1.3,1.7){$b$} \put(2.5,1.05){$c$} \put(0.3,4){$F=\sqrt{s(s-a)(s-b)(s-c)}$} \put(3.5,0.4){$\displaystyle s:=\frac{a+b+c}{2}$}
34\end{picture}""", stream, options)
35
36# Show other results.
37print(options.error_report or "No rendering errors.")
38print()
39print(f"Size: {size.width}x{size.height}")

This example creates text-and-formula.svg and its rendering log, text-and-formula.svg.log, in the same output directory.

The differences are:

Here is the result:

LaTeX Figure rendering to SVG