3

(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:

\documentclass{article}
\usepackage{pgf}

\newcommand\x{5}
\newcommand\y{6}
\newcommand\z{-4}

\newcommand*\myarray[1]
{%
  \ifnum#1>0

\section{Problems}

Find the solution to the following operations:

\begin{enumerate}
\item $\x+\y\z$
\item $\x-\y$
\item $\frac{\x+\y}{\z}$
\end{enumerate}

\newpage

    \myarray{\numexpr#1-1}%

\else

\section{Answers}

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

  \fi
}

\begin{document}

\myarray{10}

\end{document}
4
  • 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

2

Here is the main file:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools}

\setlength{\parindent}{0in}
\setlength{\parskip}{0.1in}

\newlength{\qwidth}
\setlength{\qwidth}{0.5\textwidth}

\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}
\par\noindent
\rlap{Batch #1}\hfill{\huge Evaluate Expressions}\hfill\llap{Version #2}
\par
Answers should be rounded to 2 decimal places.
\par\vspace*{0.1in}}

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

\newcommand{\qline}[2]{\mbox{#1#2}\par\vfill}

\newcommand{\qfoot}[0]{\newpage}

\newcommand{\abanner}[1]% #1 = batch
{\setlength{\headheight}{0.1in}
\setlength{\textheight}{8.9in}
\setcounter{page}{1}
\pagestyle{myheadings}
\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}\\
\hline}

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

\newcommand{\aline}[4]{(#1)&#2&(#3)&#4\\}

\newcommand{\adone}{\par\vfill}

\begin{document}
\pagestyle{empty}
\large
\include{question}
\raggedright
\include{answer}
\end{document}

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

\qhead{55510af5}{1}
\qline
{\question{1}{-5.6x-0.1}{-1.51786}}
{\question{2}{-6.7x^2-5x+4.9}{0.484912}}
\qline
{\question{3}{(6.3x-3.2)x-3.1}{0}}
{\question{4}{(0.1x-3.8)(-3.8x-9.3)}{21.6217}}
\qline
{\question{5}{\frac{1.8x+3.4}{x-7.1}}{3.864}}
{\question{6}{\frac{6.3x^2+8.8x-472.7}{x-8}}{8.06626}}
\qline
{\question{7}{\sqrt{-7x+4.9}}{-2.87143}}
{\question{8}{\sqrt{x^2-29.9}}{-8.04425}}
\qline
{\question{9}{10^{2.6x+8.8}}{-2.71494}}
{\question{10}{10^{x^2-3.5}}{-2.32619}}
\qfoot
\qhead{55510af5}{2}
\qline
{\question{1}{-5.6x-0.1}{1.71429}}
{\question{2}{-6.7x^2-5x+4.9}{-1.25689}}
\qline
{\question{3}{(6.3x-3.2)x-3.1}{1.23848}}
{\question{4}{(0.1x-3.8)(-3.8x-9.3)}{21.5176}}
\qline
{\question{5}{\frac{1.8x+3.4}{x-7.1}}{4.40333}}
{\question{6}{\frac{6.3x^2+8.8x-472.7}{x-8}}{9.42345}}
\qline
{\question{7}{\sqrt{-7x+4.9}}{-12.4657}}
{\question{8}{\sqrt{x^2-29.9}}{-8.34206}}
\qline
{\question{9}{10^{2.6x+8.8}}{-2.93227}}
{\question{10}{10^{x^2-3.5}}{1.94962}}
\qfoot

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

\abanner{55510af5}
\ahead{1}
\aline{1}{8.4}{2}{0.9}
\aline{3}{-3.1}{4}{149.8}
\aline{5}{-3.2}{6}{123.6}
\aline{7}{5}{8}{5.9}
\aline{9}{55.1}{10}{81.5}
\afoot
\ahead{2}
\aline{1}{-9.7}{2}{0.6}
\aline{3}{2.6}{4}{150.1}
\aline{5}{-4.2}{6}{119.2}
\aline{7}{9.6}{8}{6.3}
\aline{9}{15}{10}{2}
\afoot
\adone

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

1
  • 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
2

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.

\documentclass{article}
\usepackage{pythontex}
\usepackage{geometry}
\geometry{margin=0.75in}

\begin{document}

\begin{pycode}
import numpy.random as rn
from numpy import sqrt

a=[]
b=[]
c=[]
print(r'\begin{tabular}{cc}')
print(r'Equation & Answers \\')
for i in range(0,10):
    a.append(rn.randint(-10,10))
    b.append(rn.randint(-15,15))
    c.append(rn.randint(-40,26))
    string = r'{0}$x^2$'.format(a[i])
    if b[i]<0:
        string=string+r'${0}x$'.format(b[i])
    else:
        string=string+r'$+{0}x$'.format(b[i])
    if c[i]<0:
        string=string+r'${0} = 0$ &'.format(c[i])
    else:
        string=string+r'$+{0} = 0$ &'.format(c[i])
    print(string)
    base=-0.5*b[i]/a[i]
    radical=(b[i]*b[i]-4.0*a[i]*c[i])*0.25/(a[i]*a[i])
    if radical<0:
        rootn=sqrt(-radical)
        anstring=r'$x = {0}\pm{1} i$ \\ '.format(round(base,3),round(rootn,3))
    else:
        rootp=sqrt(radical)
        anstring=r'$x = {0}$ and ${1}$   \\'.format(round(base+rootp,3),round(base-rootp,3))
    print(anstring) 
print(r'\end{tabular}')
\end{pycode}


\end{document}
1
  • 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 .