avatarMartin McBride

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1691

Abstract

"https://pythoninformer.com/generative-art/generativepy/">generativepy reference</a> for details on how to install the library. You will also need NumPy installed, and the command line programs <code>latex</code> and <code>dvipng</code>.</p><h1 id="ca1d">Converting Latex to PNG</h1><p id="b635">Here is the Python code to convert a couple of equations to images:</p><div id="321f"><pre><span class="hljs-keyword">from</span> generativepy.color <span class="hljs-keyword">import</span> Color <span class="hljs-keyword">from</span> generativepy.formulas <span class="hljs-keyword">import</span> rasterise_formula</pre></div><div id="d125"><pre>rasterise_formula(<span class="hljs-string">"create-png-1"</span>, <span class="hljs-string">r"e^{i \pi}+1 = 0"</span>, Color(<span class="hljs-string">"red"</span>), dpi=<span class="hljs-number">800</span>) rasterise_formula(<span class="hljs-string">"create-png-2"</span>, <span class="hljs-string">r"\frac{\cancel{a}b}{\cancel{a}c} = 0"</span>, Color(<span class="hljs-string">"green"</span>), packages=[<span class="hljs-string">"cancel"</span>])</pre></div><p id="53c7">After importing a couple of required modules from <code>generativepy</code>, we just need to call <code>rasterise_formula</code>, passing in the three required arguments:</p><ul><li><code>name</code> - the base name that will be used to create the PNG file. This file will always be created in the working folder.</li><li><code>formula</code> - the Latex formula string. This is just the formula definition, it should not be enclosed in $ symbols, and you do not need <code>begin</code> or <code>end</code> text.</li><li>The colour of the text, as a <cod

Options

e>Color</code> object (defined in <code>generativepy.color</code>). You can use any CSS named colours, and there are also lots of other ways to define a colour.</li></ul><p id="66d0">The first call above adds an optional <code>dpi</code> parameter that controls the size of the image. The actual size of the image is determined by the formula itself, the <code>dpi</code> parameter acts as a scale factor. The default is 600, but you can make the image bigger or smaller by using a larger or smaller dpi value. Here is the result:</p><figure id="d2cd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*6tvFvlHxfs2-y_nt.png"><figcaption></figcaption></figure><p id="d50e">Latex contains many optional packages that include extra features. The second call to <code>rasterise_formula</code> shows how we can use packages when rendering Latex. We add a <code>packages</code> argument consisting of a list of package names (in this case just on, the <code>cancel</code> package). We can then use the Latex <code>\cancel</code> operator in our function, which draws diagonal lines through the <i>a</i> characters to cancel them out. Here is the result:</p><figure id="cb9e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*BOIq2kvu9f-zBG3T.png"><figcaption></figcaption></figure><h1 id="ceba">Summary</h1><p id="0c6b">It is very easy to render Latex formulas in Python with the <code>generativepy</code> library. In a future article, we will see how to add formulas to graphs and videos.</p><p id="d80f">This article first appeared on <a href="https://pythoninformer.com/generative-art/generativepy-tutorial/formula-png/">PythonInformer.com</a></p></article></body>

Rendering Latex formulas in Python

It’s easy with generativepy.

In this article we will look at the simplest way to render a Latex formula as a PNG image using generativepy.

We will use the rasterise_formula function of the formula module. First, we will look at how a formula is defined.

Latex

Latex provides a way to define mathematical formulas in plain text. For example, this Latex formula:

x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}

Defines the well-known formula for the solutions to a quadratic equation. When this definition is rendered, it looks something like this:

We won’t go into details about how Latex works here, there are many examples online. This article just looks at how to render Latex as an image.

generativepy

generativepy is a Python library for creating generative art and mathematical illustrations in code. It has many features, but the one we will look at here is how to render Latex as a PNG image.

See the generativepy reference for details on how to install the library. You will also need NumPy installed, and the command line programs latex and dvipng.

Converting Latex to PNG

Here is the Python code to convert a couple of equations to images:

from generativepy.color import Color
from generativepy.formulas import rasterise_formula
rasterise_formula("create-png-1", r"e^{i \pi}+1 = 0", Color("red"), dpi=800)
rasterise_formula("create-png-2", r"\frac{\cancel{a}b}{\cancel{a}c} = 0",
                  Color("green"), packages=["cancel"])

After importing a couple of required modules from generativepy, we just need to call rasterise_formula, passing in the three required arguments:

  • name - the base name that will be used to create the PNG file. This file will always be created in the working folder.
  • formula - the Latex formula string. This is just the formula definition, it should not be enclosed in $ symbols, and you do not need begin or end text.
  • The colour of the text, as a Color object (defined in generativepy.color). You can use any CSS named colours, and there are also lots of other ways to define a colour.

The first call above adds an optional dpi parameter that controls the size of the image. The actual size of the image is determined by the formula itself, the dpi parameter acts as a scale factor. The default is 600, but you can make the image bigger or smaller by using a larger or smaller dpi value. Here is the result:

Latex contains many optional packages that include extra features. The second call to rasterise_formula shows how we can use packages when rendering Latex. We add a packages argument consisting of a list of package names (in this case just on, the cancel package). We can then use the Latex \cancel operator in our function, which draws diagonal lines through the a characters to cancel them out. Here is the result:

Summary

It is very easy to render Latex formulas in Python with the generativepy library. In a future article, we will see how to add formulas to graphs and videos.

This article first appeared on PythonInformer.com

Latex
Python
Generative Art
Mathematics
Formula
Recommended from ReadMedium