(如何从数组循环中提取元素?)
我正在尝试创建“n”个版本的家庭作业,并在最后一页中更改或增加每个变量的答案。
到目前为止,我可以创建一组问题的“n”个版本,但我希望每个问题在每次迭代中都有所不同,方法是将\x
、\y
、\z
增加一。
我的代码是:
\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}
答案1
这是主要文件:
\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)&(#3)\\}
\newcommand{\adone}{\par\vfill}
\begin{document}
\pagestyle{empty}
\large
\include{question}
\raggedright
\include{answer}
\end{document}
这是由 C 程序生成的question.tex(2个版本):
\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
这也是由 C 程序生成的 answer.tex:
\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
批号实际上是以十六进制呈现的 unix 时间/日期。对于此工作表,我使用随机数生成器生成 2 位系数和固定数字答案,然后求解 x(以便答案的最后一位小数应始终为零)。
答案2
Geoff Poore 开发了一个名为 PythonTeX 的软件包,它将 Python 代码集成到 .tex 文件中,并允许您排版 Python 代码的输出。虽然我没有专门写过你的问题,但这里有一个脚本,它可以生成 10 个随机二次方程并排版它们以及答案。可以将答案字符串存储在列表中,并将它们打印在单独的页面上。
PythonTeX 随 TeX live 和 MikTeX 一起发布。它需要 TeX 编译,在输出上运行 pythontex,然后重新 TeXing。文档与 texlive 和 miktex 一起安装,并在文档文件夹中安装示例文件。
如果您熟悉 Python 字符串,您应该能够修改它以满足您的需要。
\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}