LaTeX math formulas rendering | C++

The API reference section related to this topic is here. In fact, the easiest way to demonstrate the LaTeX math formula rendering feature is to start with the example. Here it is:

 1    // Create rendering options 
 2    System::SharedPtr<PngMathRendererOptions> options = System::MakeObject<PngMathRendererOptions>();
 3    // Sspecify the image resolution 150 dpi
 4    options->set_Resolution(150);
 5    // Specify the preamble.
 6    options->set_Preamble(u"\\usepackage{amsmath}\r\n\\usepackage{amsfonts}\r\n\\usepackage{amssymb}\r\n\\usepackage{color}");
 7    // Specify the scaling factor 300%.
 8    options->set_Scale(3000);
 9    // Specify the foreground color.
10    options->set_TextColor(System::Drawing::Color::get_Black());
11    // Specify the background color.
12    options->set_BackgroundColor(System::Drawing::Color::get_White());
13    // Specify the output stream for the log file.
14    options->set_LogStream(System::MakeObject<System::IO::MemoryStream>());
15    // Specify whether to show the terminal output on the console or not.
16    options->set_ShowTerminal(true);
17    
18    // Create the output stream for the formula image.
19    {
20        System::SharedPtr<System::IO::Stream> stream = System::IO::File::Open(System::IO::Path::Combine(RunExamples::OutputDirectory, u"math-formula.png"), System::IO::FileMode::Create);
21        // Clearing resources under 'using' statement
22        System::Details::DisposeGuard<1> __dispose_guard_0({ stream});
23        // ------------------------------------------
24        try
25        {
26            System::Drawing::SizeF size = System::MakeObject<Features::PngMathRenderer>()->Render(u"\\begin{equation*}\r\ne^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!}\r\n\\end{equation*}", stream, options, size);
27            
28            // Show other results.
29            System::Console::get_Out()->WriteLine(options->get_ErrorReport());
30            System::Console::get_Out()->WriteLine();
31            System::Console::get_Out()->WriteLine(System::String(u"Size: ") + size);
32        }
33        catch(...)
34        {
35            __dispose_guard_0.SetCurrentException(std::current_exception());
36        }
37    }

Let’s get to the details. First of all, we create a rendering options instance, similar to the TeX/LaTeX typesetting. We do it here simultaneously specifying the output image resolution.

Next, we specify the preamble. The default preamble is:

1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}

which provides slightly more advanced math formula support than basic LaTeX. You can, for example, add the color package if you want to use your own highlighting in the formula, as we showed in the code example.

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

Next two options define the foreground and background colors. Those parts of the formula that are not covered (“colored”) by the custom highlighting will be displayed in the TextColor color.

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 MathRenderer.Render(). It returns the size of the formula in points.

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

And finally, we call the MathRenderer.Render() method itself, passing options as the third argument. The LaTeX code of the formula is passed as a string through the first argument.

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

This is the most general use case for LaTeX math formula rendering.

You may also check out the free web app built based on the feature implemented within Aspose.TeX for C++ API.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.