(How to extract elements from a loop in an array?)

I'm trying to create "n" versions of a homework and their answers in the last page changing or increasing each variable every loop.

So far I can create "n" versions of a set of problems, but I want every problem to be different in each iteration by increasing \x, \y, \z by one.

My code is:





Find the solution to the following operations:

\item $\x+\y\z$
\item $\x-\y$
\item $\frac{\x+\y}{\z}$





\item \pgfmathparse {int(\x+\y+\z)}\pgfmathresult
\item \pgfmathparse {int(\x-\y)}\pgfmathresult
\item \pgfmathparse {(\x+\y)/\z}\pgfmathresult




  • I do something similar, but I use a random number generator and C code to generate the questions/answers and LaTeX to format them. I could never get any feedback, so lost interest. See elfsoft2000.com/worksheets/index.htm May 10, 2015 at 17:41
  • Thanks a lot!!, I have a little more experience working in C that with Latex, this will give me an advantage in my goals. I appreciate your time and your answer. May 11, 2015 at 13:22
  • I can show you my source for a sample. I generate separate files for the questions and answers, consisting of macro calls with as little TeX formatting as possible. May 11, 2015 at 14:09
  • Be my guest!!, It would be awesome !! May 11, 2015 at 14:35

2 Answers 2


Here is the main file:




\newcommand{\qhead}[2]% #1 = batch, #2 = version
{\framebox[3in][l]{\rule{0in}{0.25in}\tiny name}\hfill
\framebox[1.5in][l]{\rule{0in}{0.25in}\tiny date}\hfill
\framebox[0.4in][l]{\rule{0in}{0.25in}\tiny period}
\rlap{Batch #1}\hfill{\huge Evaluate Expressions}\hfill\llap{Version #2}
Answers should be rounded to 2 decimal places.

\newcommand{\question}[3]% #1 = problem number, #2 = expression, #3 = value
(#1) \framebox[0.75in]{\rule{0in}{.2in}}
\parbox[t]{2in}{Compute \[#2\] for $x = #3$.}



\newcommand{\abanner}[1]% #1 = batch
\markright{\rlap{Batch #1}\hfil Evaluate Expressions}}

\newcommand{\ahead}[1]%#1 = version
{\begin{tabular}{|r@{ }lr@{ }l|}
\multicolumn{4}{l}{ }\\
\multicolumn{4}{l}{Version #1}\\

\newcommand{\afoot}[0]{\hline\end{tabular} \hfil}




This is question.tex which is generated by the C program (2 versions):


This is answer.tex also produced by the C program:


The batch number is actually the unix time/date rendered in hex. For this worksheet I use the random number generator to produce 2 digit coefficients and fixed digit answers, then solved for x (so that the last decimal of the answer should always be zero).

  • SWEEEEEET!!!, Yesterday I followed your advice and programmed my iterated latex code in C (in Xcode) and it worked!! I'll try your code too!! THANKS AGAIN !!! May 13, 2015 at 12:52

There is a package called PythonTeX by Geoff Poore which integrates Python code in a .tex file and allows you to typeset the output of the Python code. While I haven't written your problem specifically, here's a script which generates 10 random quadratic equations and typesets them, along with the answers. One could store the answer strings in a list and print them on a separate page.

PythonTeX is distributed with TeX live and MikTeX. It requires a TeX compilation, running pythontex on the output, then re-TeXing. Documentation is installed with texlive and miktex along with sample files in the documentation folder.

If you are familiar with Python strings, you should be able to modify this to meet your needs.



import numpy.random as rn
from numpy import sqrt

print(r'Equation & Answers \\')
for i in range(0,10):
    string = r'{0}$x^2$'.format(a[i])
    if b[i]<0:
    if c[i]<0:
        string=string+r'${0} = 0$ &'.format(c[i])
        string=string+r'$+{0} = 0$ &'.format(c[i])
    if radical<0:
        anstring=r'$x = {0}\pm{1} i$ \\ '.format(round(base,3),round(rootn,3))
        anstring=r'$x = {0}$ and ${1}$   \\'.format(round(base+rootp,3),round(base-rootp,3))

  • Great, my students will have to work by themselves individually!!, I hate when they give me 20 versions of the same homework XD Thanks a lot !! May 13, 2015 at 14:20

You must log in to answer this question.

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