好的,我基本上是通过阅读一些软件包手册和在这个网站上寻找特定问题的答案来学习 LaTex 的。我一直在使用 eqexam 软件包和 newcommand 来定义问题,从而创建随机版本的书面数学考试。例如,一个问题的设置如下:
\newcommand{\CreditPayoffQuestion}[1]{%
\foreach \i in {1,...,#1}{%
\def\ops{{"1225","1475","1175","1350","1250"}}
\def\opsa{{"14.99","15.99","16.99","17.99"}}
\def\opsb{{"60","65","70","75","80","85"}}
\newcommand\randomopone{\bgroup\pgfmathsetmacro\op{\ops[int(rnd*5)]}\op\egroup}
\newcommand\randomoptwo{\bgroup\pgfmathsetmacro\op{\opsa[int(rnd*4)]}\op\egroup}
\newcommand\randomopthree{\bgroup\pgfmathsetmacro\op{\opsb[int(rnd*6)]}\op\egroup}
$\text{Assume you want to buy a new phone that costs }\$\randomopone{}\text{ using a credit card that has an APR}\\ \text{of }\randomoptwo{}\%\text{. How long will it take you to pay off the phone if you make regular monthly}\\ \text{payments of }\$\randomopthree{}\text{. Round your answer to the nearest tenth of a year.}$%
}%
}%
当我编译:5.\CreditPayoffQuestion{1} 我得到了我期望的结果:
现在问题来了。我定义了一个表的六个不同版本,例如:
\newcommand{\TableA}{\begin{tabular}{|llr|}
\hline \multicolumn{3}{|c|}{Statement for May 15-June 14}\\
\hline Previous Balance:&&\$180\\
\hline May 20&Payment&\$225\\
\hline Charges:&&\\
&May 16&\$70\\
&May 25&\$156\\
&June 5&\$85\\
\hline Days in Billing Cycle&&31\\
\hline
\end{tabular}}
我已经使用过以下代码:
\newcommand*{\CreditTableQuestion}[1]{%
\foreach \i in {#1,...,##1}{%
\def\opsb{{"\TableA","\TableB","\TableC","\TableD","\TableE","\TableF"}}
\newcommand\randomopone{\bgroup\pgfmathsetmacro\op{\opsb[int(rnd*4)]}\op\egroup}
$\randomopone{}$%
}%
}%
但不断出现错误:
\pgffor@values 定义中的参数编号非法。\CreditTableQuestion{1}
和
不完整 \iffalse;第 316 行之后的所有文本均被忽略
任何能够解决此问题的帮助都将不胜感激。
答案1
您可以使用随机数生成器,expl3
它还提供更简单的语法。
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\optionlists}{m}
{% each set of options is of the form {label}{item1,item2,...}
\bowman_random_optionlists:n { #1 }
}
\NewExpandableDocumentCommand{\randopt}{m}
{% #1 is one of the previously set up labels
\bowman_random_getrandom:n { #1 }
}
\tl_new:N \l__bowman_random_options_tl
\cs_new_protected:Nn \bowman_random_optionlists:n
{
\tl_set:Nn \l__bowman_random_options_tl { #1 }
}
\cs_new:Nn \bowman_random_getrandom:n
{
\bowman_random_get:e
{
\str_case:nV { #1 } \l__bowman_random_options_tl
}
}
\cs_generate_variant:Nn \str_case:nn { nV }
\cs_new:Nn \bowman_random_get:n
{
\clist_item:nn { #1 } { \int_rand:n { \clist_count:n { #1 } } }
}
\cs_generate_variant:Nn \bowman_random_get:n { e }
\ExplSyntaxOff
\optionlists{
{}{1225,1475,1175,1350,1250}
{a}{14.99,15.99,16.99,17.99}
{b}{60,65,70,75,80,85}
{c}{TableA,TableB,TableC}
}
\newcommand{\CreditPayoffQuestion}[1]{%
Assume you want to buy a new phone that costs \$\randopt{} using a credit card
that has an APR of \randopt{a}. How long will it take you to pay off the phone
if you make regular monthly payments of \$\randopt{b}? Round your answer to
the nearest tenth of a year.
Comment the following table
\UseName{\randopt{c}}
}
\newcommand{\TableA}{%
\begin{tabular}{cc}Table & A \\ 1 & 2 \end{tabular}
}
\newcommand{\TableB}{%
\begin{tabular}{cc}Table & B \\ 3 & 4 \end{tabular}
}
\newcommand{\TableC}{%
\begin{tabular}{cc}Table & C \\ 5 & 6 \end{tabular}
}
\begin{document}
\CreditPayoffQuestion
\bigskip
\CreditPayoffQuestion
\end{document}
您可以定义任意数量的列表以供选择,并以集中的方式执行,而不是在每个问题生成命令中执行。
答案2
您只想根据某个随机变量来设定使用\TableA
...的条件\TableF
。下面使用以下函数来实现这一点:xfp
并rand()
通过以下方式将其转换为字母字符\@Alph
:
\documentclass{article}
\usepackage{xfp}
\newcommand{\TableA}{This is Table~A}
\newcommand{\TableB}{This is Table~B}
\newcommand{\TableC}{This is Table~C}
\newcommand{\TableD}{This is Table~D}
\newcommand{\TableE}{This is Table~E}
\newcommand{\TableF}{This is Table~F}
\begin{document}
\csname Table\csname @Alph\endcsname{\fpeval{ceil(6*rand())}}\endcsname
\end{document}