Browsing Tag


Latex, Mathematics, Python

PyTex2png Make Pretty Math PNG Files With Latex, Python and C++

October 9, 2015

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) 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.


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


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 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 "Coverting LaTeX from " + source + " to " + output
	data = get_file(source).replace("\\","\\\\")

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")
	lines =
	return lines

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

# 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

# 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

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)
		p = subprocess.Popen(args)
		p = subprocess.Popen(args,stdout=subprocess.PIPE)

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.
  • 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).


Bringing Colors to TeXShop

January 4, 2013

In an effort to continue posts with colors in common I want to bring up TeXShop. TeXShop is a latex editor of Mac that comes with MacTex distributions. In my mind it is the only Latex editor you will need, but what do I now. For those of you who do not know about Latex, it is a markup typeset base on Donald Knuth’s TeX typesettings. Latex has the ability to handle just about anything: math symbols, algorithms, flow charts, you name it. I have learned how to use Latex about 3 years ago, and I cannot recall a document I wrote that has not been typed in Latex. That is a whle other story for another time.

Here is a Simple Latex document I found online. I had made just available here just in case you could not find it. I also had to add comments to show you how annoying the color scheme can get. Here is a screen shot of what the file would look like with the default TexShop color scheme:


I think this is just awful. So, lets change it. In order to change the colors you will have to run some terminal command. I know, one would think that by now you could change it through preferences. Nope. There are 7 items you could change the colors to. For each you would have to run 3 commands, one for each RGB value. The RGB values have to be in a float RGB scale. You can consult this float RGB color chart if you need. The 7 items you could color are:


For example, say we want to change the comments from that horrible red to something more easy on the eye. Lets go with some light purple, like <0.8, 0.0, 1.0>. The commands will be:

defaults write TeXShop commentred 0.8
defaults write TeXShop commentgreen 0.0
defaults write TeXShop commentblue 1.0

You will have to restart TeXShop and now comments will look like this:


In a similar way you can change the rest of the colors to match you style. Here is a bash script to change all the colors on TexShop. The colors are set for the default colors. You can change any of the colors to match you style. If you want to go back to the original simply download the file and run it. I would suggest you keep a local copy of your settings just in case. In order to run the code you will have to change the permissions to 700 by running this command:

sudo chmod 700

Now you can change any of the colors at by running the file instead of running multiple commands.