A few months back I posted some code on GitHub to convert Latex markup code into PNG files using Python named it PyTex2png. I know realize that I didn’t explain one bit of the code. I did however write a very clear (to me) README.md file explaining how to use the code if you wanted to download and use it. I even included a couple of cool examples. In this post I want to dive into the code that make it work.

Lets start by explaining exactly what Latex is and what this module does. Latex is a markup language used for scientific notation and papers. I came across it in my second year in college and science then I have never looked back. It is by far my favorite word processor. I even have my resume and letters typed up in Latex. It just make pretty things. Among other things, Latex can create beautiful Mathematical typography like the one below. Latex does have a bit of learning curve, but it shouldn’t be too difficult to master. We are only looking at the math portion of it anyway. Here is some Latex code and the image that got generated from it. Note that this post will not teach you Latex. There are plenty of books and information about Latex online already.

Code:

\cos (2\theta) = \cos^2 \theta - \sin^2 \theta \\
\lim_{x \to \infty} \exp(-x) = 0 \\
a \bmod b \\
x \equiv a \pmod b

[\code]

Pretty Output:

So how does that happen? Well there are a few things to look at. The first is a C++ code that was written by Bruno Bachelet. It is available here (and here as backup). There is also and online version of the code running here. This code is the main power behind the software. Everything else is just a Python wrapper to add some fancy functions like multiple files, remove background and invert colors.

Within pytex2png.py you will see a function name covert(). This function calls the C++ module to convert images, creates the PNG, removes any background and finally converts the math to white. The last portion is not really required. I was coding this up for a larger project and in that one the background is not white, so I need white fonts.


def convert(source,output,display=False):
if(display): print &amp;amp;quot;Coverting LaTeX from &amp;amp;quot; + source + &amp;amp;quot; to &amp;amp;quot; + output
data = get_file(source).replace(&amp;amp;quot;\\&amp;amp;quot;,&amp;amp;quot;\\\\&amp;amp;quot;)
make_png(data,output,display)
make_transparent_bg(output,display)
make_white_text(output,display)



This doesn’t tell us anything about how this program works. It is just a plain function that makes calls to other functions. We will have to look at the other functions to understand how this one ties everything together. Lets take a look at the code:


# read file content as a single variable
def get_file(filename):
file = open(filename, "r")
file.close()
return lines

# create png from latex
def make_png(data,image,display):
if(display): print "Making image..."
command_line = "./tex2png -r* -lq \""+data+"\" " + image
exe_command(command_line,display)

# remove white background
def make_transparent_bg(image,display):
if(display): print "Removing white background..."
command_line = "convert "+image+" -channel rgba -fill \"rgba(255,255,255,0)\" -opaque \"rgb(255,255,255)\" " + image
exe_command(command_line)

# convert output text to white
def make_white_text(image,display):
if(display): print "Converting output image text to white..."
command_line = "convert "+image+" -channel rgb -fill \"rgba(255,255,255)\" -opaque \"rgb(0,0,0)\" " + image
exe_command(command_line)



After a careful inspection of these functions, you should see a pattern. The first one is a get_file function. All it does is open a file and reads it’s content. Then returns the data from the file as a single object. You can read more about reading a writing from files in the Python Documentation. The other 3 functions, make_png, make_tansparent_bg, and make_white_text follow a templet:

display output
build command line command
execute command

That’s all folks! it is pretty straight forward. The commands each one execute are based off of what we need done. The first one, make_png calls the executable made from the C++ module. The remanning 2 functions, make_transparent_bg and make_white_text use a command line tool called convert. This is a Unix utility and you can read all about it in the convert manual file or on this site.

You will notice that there is one function we haven’t mentioned yet, exe_command. Let’s look at that function:


# runs a command line command and waits for it to complete
# by default it will not display any output
def exe_command(cmd, display=False):
args = shlex.split(cmd)
if(display):
p = subprocess.Popen(args)
else:
p = subprocess.Popen(args,stdout=subprocess.PIPE)
p.wait()



This one should be taken as is. It takes in a command line command and excites it using a Python module called subprocess. It can be used to execute and command in the command line and can retune the results if needed. It is a good little function to keep handy for other programs in the future...

That is all the magic that ties this thing together. There are some more files and notable mentions.

• Examples folder holds text files with Latex markup to be converted.
• Output folder will contain PNG’s that have been converted already.
• Source has the C++ file need to make the PNG files in the first place.
• Makefile is used to compile the C++ file and create and executable for your computer architecture.
• examples.py is a simple Python module to load the example text files from the folder and run them through PyTex2png converter.
• gpl-3.0.txt is a GPL License under which the code is distributed.

Here is a PyTex2png zip file with all the files together. It is also available via GitHub (pytex2png).

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.