I am looking to create a template where I can quickly generate pages of basic math equations. LaTeX seems like a good place to start typesetting math equations but I am completely stumped with the syntax.

My thoughts are to first build a template, use python to generate the template code with random numbers, convert to PDF, then print. here is an example of what I have come up with so far, but trying to nest this within another table created issues.

\halign{\hfil # & # \hfil \cr 4\cr \underline{+2}\cr}

This produces rendered example.

Are there any other languages or tools that I could use to meet my goals? I'm all ears.

  • 1
    What is exactly the problem that you are having? How to typeset some specific equation? How to layout a number of equations in a single page? Aug 31, 2010 at 11:02
  • Both, I made headway typesetting the equation itself as documented by the code I provided, although I could very well be on the wrong track with that. I also would like to know the most effective way to layout a grid of equations.
    – ebt
    Aug 31, 2010 at 11:12
  • 1
    You want them laid out like that - vertically? And are they all to be of that sort: "x + y = [blank]" (only vertically)? How would you like them laid out on the page? In a grid? Or can they be out of alignment? Aug 31, 2010 at 11:53
  • 1
    Generally, when asking a question like this it helps to say what the goal is. Are you, perhaps, writing a page of exercises in arithmetic for school kids? Or something else? Aug 31, 2010 at 16:49

3 Answers 3


Not so hackish, but still a bit, here are some ideas.

First define a new command to produce each of your equations. I used the array environment to do this.


Then arrange them on a page in a grid using a tabular environment.

\myeq{4}{+2} & \myeq{3}{\times 2} \\[2em]
\myeq{4}{+2} & \myeq{3}{\times 2}

rendered example

Note that you can give an optional argument to the \\[..] command in order to control the spacing between rows. And in the @{..} specifiers passed to tabular you can also use commands like \qquad or \hspace{..} to control the spacing between columns.


You can even use LaTeX to generate your random numbers for you, using the pgfmath package, which is a part of pgf/tikz. Here is minimal example, using Juan's macros from his answer:


% Juan's macro:

% Generate random integer between 1 and 9:

% The rest is from Juan's answer again, with numbers replaced by \rand:
\myeq{\rand}{+\rand} & \myeq{\rand}{\times \rand} \\[2em]
\myeq{\rand}{+\rand} & \myeq{\rand}{\times \rand}


Then all you need to do is process the file by pdflatex to get the pdf.

Another option would be using lua to generate the numbers, and process the file by luatex. That will give you more flexibility, you can generate the numbers exactly the way you want.


based upon the accepted answer I was able to put together a quick script that does exactly what I set out to do. I added logic to avoid carry-overs. Thanks for all your input.

import random

numRows = 6
numColumns = 5
numPages = 1

# text blocks
multiply = r'\myeq{%s}{\times %s}'
add = r'\myeq{%s}{+%s}'
subtract = r'\myeq{%s}{-%s}'
page = r'''\begin{center}
end_row = r'\\[6em]'
column = r'c@{\hspace{%sin}} '%(5.0/numColumns)

def hasCarryOver(top,bottom):
    a = b = c = d = 0
    if len(str(top))==2:a,b = int(str(top)[0]),int(str(top)[1])
    else:b = top

    if len(str(bottom))==2:c,d = int(str(bottom)[0]),int(str(bottom)[1])
    else:d = bottom

    if a+c > 9 or b+d > 9:return True
    else:return False

pages = []
for p in range(numPages):
    lines = []
    for x in range(numRows):
        line = []
        for y in range(numColumns):
            reject = True
            while reject:
                top = random.randrange(*equation_top_range)
                bottom = random.randrange(*equation_bottom_range)
                reject = hasCarryOver(top,bottom)

        lines.append(' & '.join(line)+end_row)
    pages.append(page%("".join(column * column_number),'\n'.join(lines)))

body = r'''\documentclass[10pt,letterpaper]{report}



print body

produces alt text

  • I tried compiling this and got an error:
    – DJP
    Jul 9, 2011 at 3:45
  • Changing 'column_number' to 'numColumns' solves the problem.
    – DJP
    Jul 9, 2011 at 3:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .