Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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? –  Juan A. Navarro Aug 31 '10 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 '10 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? –  Loop Space Aug 31 '10 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? –  dmckee Aug 31 '10 at 16:49
add comment

3 Answers

up vote 8 down vote accepted

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.

\newcommand{\myeq}[2]{$\begin{array}{@{}r@{}}#1\\#2\\\hline\end{array}$}

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

\begin{tabular}{c@{\qquad}c}
\myeq{4}{+2} & \myeq{3}{\times 2} \\[2em]
\myeq{4}{+2} & \myeq{3}{\times 2}
\end{tabular}

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.

share|improve this answer
add comment

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:

\documentclass{article}
\usepackage{pgfmath}

% Juan's macro:
\newcommand{\myeq}[2]{$\begin{array}{@{}r@{}}#1\\#2\\\hline\end{array}$}

% Generate random integer between 1 and 9:
\newcommand{\rand}{\pgfmathrandominteger{\rndint}{1}{9}\rndint}

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

\end{document}

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.

share|improve this answer
add comment

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.

#!usr/bin/python
import random

#cfg
numRows = 6
numColumns = 5
numPages = 1
equation_top_range=[1,30]
equation_bottom_range=[1,20]


# text blocks
multiply = r'\myeq{%s}{\times %s}'
add = r'\myeq{%s}{+%s}'
subtract = r'\myeq{%s}{-%s}'
page = r'''\begin{center}
\begin{tabular}{%s}
%s
\end{tabular}
\end{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)

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


body = r'''\documentclass[10pt,letterpaper]{report}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[margin=2.5cm]{geometry}

\newcommand{\myeq}[2]{$\begin{array}{@{}r@{}}#1\\#2\\\hline\end{array}$}

\DeclareMathSizes{10}{18}{12}{8}
\begin{document}
%s
\end{document}
'''%('\pagebreak'.join(pages))

print body

produces alt text

share|improve this answer
    
I tried compiling this and got an error: –  DJP Jul 9 '11 at 3:45
    
Changing 'column_number' to 'numColumns' solves the problem. –  DJP Jul 9 '11 at 3:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.