LaTeX Figure rendering | Aspose.TeX for .NET

Sometimes you may want to extract some content from a LaTeX file as a separately rendered piece, or a “figure”, without any connection to the page layout. It can be an illustration for your publication on the Internet, for example. You can do it with our API. As for the target formats, there are two options - PNG and SVG. As is in the case with the LaTeX math formula rendering feature. It’s also worth noting that LaTeX figure rendering is a generalization of LaTeX math formula rendering.

How to render a LaTeX figure to PNG

The API reference section related to this topic is here. And as with formula rendering, we will start with an example. Here it is:

 1// Create rendering options setting the image resolution to 150 dpi.
 2PngFigureRendererOptions options = new PngFigureRendererOptions();
 3options.Resolution = 150;
 4// Specify the preamble.
 5options.Preamble = "\\usepackage{pict2e}";
 6// Specify the scaling factor 300%.
 7options.Scale = 3000;
 8// Specify the background color.
 9options.BackgroundColor = System.Drawing.Color.White;
10// Specify the output stream for the log file.
11options.LogStream = new System.IO.MemoryStream();
12// Specify whether to show the terminal output on the console or not.
13options.ShowTerminal = true;
14
15// Create the output stream for the figure image.
16using (System.IO.Stream stream = System.IO.File.Open(
17    System.IO.Path.Combine(RunExamples.OutputDirectory, "text-and-formula.png"), System.IO.FileMode.Create))
18{
19    // Run rendering.
20    System.Drawing.SizeF size = new PngFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
21\begin{picture}(6,5)
22\thicklines
23\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$}
24\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}$}
25\end{picture}", stream, options);
26    
27    // Show other results.
28    System.Console.Out.WriteLine(options.ErrorReport);
29    System.Console.Out.WriteLine();
30    System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
31}

First of all, we create a LaTeX figure options instance. We do it here simultaneously specifying the output image resolution.

Next, we specify the preamble. There’s no default preamble for LaTeX figure rendering, so if you are, for instance, going to render some graphics plotted using the pict2e LaTeX package, you need to specify it in the preamble:

1\usepackage{pict2e}

Then we instruct the renderer to scale the output by 300%.

The next option defines the background color. Unlike with math formula rendering, we don’t specify a foreground color since we assume that the colors are entirely under the control of the LaTeX code. In fact, so is the background color, so this is just a convenience option.

The next line of the example doesn’t make much sense. It just demonstrates that you can direct the log output to some stream.

And the last option ShowTerminal allows you to toggle writing the terminal output to the console.

The method that actually performs the rendering is FigureRenderer.Render(). It returns the size of the figure in points.

The stream where the image is to be written is accepted by the method as the second argument. We create the stream next.

And finally, we call the FigureRenderer.Render() method itself, passing options as the third argument. The LaTeX code of the figure is passed as the first argument.

The last lines of the example print two artifacts of figure rendering - the size of the figure and the brief error report (in case there are errors).

Here is the result of rendering.

LaTeX Figure rendering to PNG

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

How to render a LaTeX figure to SVG

In much the same way, we can render a LaTeX figure in SVG format.

 1// Create rendering options.
 2FigureRendererOptions options = new SvgFigureRendererOptions();
 3// Specify the preamble.
 4options.Preamble = "\\usepackage{pict2e}";
 5// Specify the scaling factor 300%.
 6options.Scale = 3000;
 7// Specify the background color.
 8options.BackgroundColor = System.Drawing.Color.White;
 9// Specify the output stream for the log file.
10options.LogStream = new System.IO.MemoryStream();
11// Specify whether to show the terminal output on the console or not.
12options.ShowTerminal = true;
13
14// Create the output stream for the figure image.
15using (System.IO.Stream stream = System.IO.File.Open(
16    System.IO.Path.Combine(RunExamples.OutputDirectory, "text-and-formula.svg"), System.IO.FileMode.Create))
17{
18    // Run rendering.
19    System.Drawing.SizeF size = new SvgFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
20\begin{picture}(6,5)
21\thicklines
22\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$}
23\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}$}
24\end{picture}", stream, options);
25    
26    // Show other results.
27    System.Console.Out.WriteLine(options.ErrorReport);
28    System.Console.Out.WriteLine();
29    System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
30}

The differences are:

Here is the result:

LaTeX Figure rendering to SVG

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.