基于我所做的事情此主题,为什么我的作业没有做完\xdef
?
练习页显示得很好。理想情况下,答案页看起来应该完全相同,当然,答案除外。
但答案页面却很乱。有时可以编译,有时不能。
如果我注释掉\AnswerList
底部附近的代码,代码就可以完美运行。所以问题与此有关。
是什么赋予了?
这是代码。
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{ifthen}
\usepackage{pgf}
\usepackage{pgffor}
\usepackage{tikz}
\setlength{\parindent}{0pt}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand{\InitVariables}
{
\pgfmathsetmacro{\FactorA}{int(random(1,9))}
\pgfmathsetmacro{\FactorB}{int(random(1,9))}
\pgfmathsetmacro{\Product}{int(\FactorA*\FactorB)}
\pgfmathsetmacro{\Structure}{int(random(0,2))}
}
\newcommand{\AnswerSpace}
{%
\tikz[baseline=6pt]{\draw (0,0)rectangle(0.9,0.9);}
}
\newcommand{\ASF}[1] % Answer Space Filled
{%
\tikz[baseline=6pt]{\draw (0,0)rectangle(0.9,0.9); \node[center] at (0.45,0.35) {\color{blue}#1};}
}
\newcommand{\OneEquation}
{%
\large
\InitVariables
\ifcase\Structure\relax
\def\Exercise{\(\FactorA \times \FactorB = \AnswerSpace\)}
\def\Answer{\(\FactorA \times \FactorB =\ASF{\Product}\)}
\or
\def\Exercise{\(\FactorA \times \AnswerSpace = \Product\)}
\def\Answer{{\(\FactorA \times \ASF{\FactorB} = \Product\)}}
\or
\def\Exercise{\(\AnswerSpace \times \FactorB = \Product\)}
\def\Answer{\(\ASF{\FactorA} \times \FactorB = \Product\)}
\fi
}
\newcommand{\AnswerList}{}
\newcommand{\ExerciseList}[1]
{%
\foreach \x in {1,2,3,...,{#1}}
{%
\OneEquation \Exercise \par \vspace{0.5cm}
\xdef\AnswerList{\AnswerList \Answer \par \vspace{0.5cm}}
}
}
\begin{document}
\section{Exercises}
\ExerciseList{10}
\pagebreak
\section{Answers}
\AnswerList
\end{document}
答案1
您不能在 xdef/edef 中放置任意 tex 命令,如果它们不能通过扩展起作用,它们将会失败,您不想完全扩展它,只需扩展数字部分,一步。
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{ifthen}
\usepackage{pgf}
\usepackage{pgffor}
\usepackage{tikz}
\setlength{\parindent}{0pt}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand{\InitVariables}
{
\pgfmathsetmacro{\FactorA}{int(random(1,9))}
\pgfmathsetmacro{\FactorB}{int(random(1,9))}
\pgfmathsetmacro{\Product}{int(\FactorA*\FactorB)}
\pgfmathsetmacro{\Structure}{int(random(0,2))}
}
\newcommand{\AnswerSpace}
{%
\tikz[baseline=6pt]{\draw (0,0)rectangle(0.9,0.9);}
}
\newcommand{\ASF}[1] % Answer Space Filled
{%
\tikz[baseline=6pt]{\draw (0,0)rectangle(0.9,0.9); \node at (0.45,0.35) {\color{blue}#1};}
}
\newcommand{\OneEquation}
{%
\large
\InitVariables
\ifcase\Structure\relax
\def\Exercise{\(\FactorA \times \FactorB = \AnswerSpace\)}
\edef\Answer{\noexpand\(\FactorA \times \FactorB =\noexpand\ASF{\Product}\noexpand\)}
\or
\def\Exercise{\(\FactorA \times \AnswerSpace = \Product\)}
\edef\Answer{{\noexpand\(\FactorA \times\noexpand \ASF{\FactorB} = \Product\noexpand\)}}
\or
\def\Exercise{\(\AnswerSpace \times \FactorB = \Product\)}
\edef\Answer{\noexpand\(\noexpand\ASF{\FactorA} \times \FactorB = \Product\noexpand\)}
\fi
}
\newcommand{\AnswerList}{}
\makeatletter
\newcommand{\ExerciseList}[1]
{%
\foreach \x in {1,2,3,...,{#1}}
{%
\OneEquation \Exercise \par \vspace{0.5cm}
\expandafter\g@addto@macro\expandafter\AnswerList\expandafter
{\Answer \par \vspace{0.5cm}}
}
}
\makeatother
\begin{document}
\section{Exercises}
\ExerciseList{10}
\pagebreak
\section{Answers}
\AnswerList
\end{document}
答案2
您正在使用\ASF
:\xdef
这是行不通的,因为\tikz
不是可扩展的命令。它不是该上下文中唯一危险的宏。
这是使用 的不同实现expl3
。有一些代码重复,但您可能希望对练习和解决方案使用不同的渲染,因此我定义了不同的函数来执行它们。
\documentclass[twocolumn]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{tikz}
\usepackage{xparse}
\setlength{\parindent}{0pt}
\pgfmathsetseed{\number\pdfrandomseed}
\NewDocumentCommand{\InitVariables}{}{%
\pgfmathsetmacro{\FactorA}{int(random(1,9))}%
\pgfmathsetmacro{\FactorB}{int(random(1,9))}%
\pgfmathsetmacro{\Product}{int(\FactorA*\FactorB)}%
\pgfmathsetmacro{\Structure}{int(random(0,2))}%
}
\NewDocumentCommand{\AnswerSpace}{}{%
\tikz[baseline=6pt]{\draw (0,0)rectangle(0.9,0.9);}%
}
\NewDocumentCommand{\ASF}{m}{% Answer Space Filled
\tikz[baseline=6pt]{%
\draw (0,0)rectangle(0.9,0.9);
\node[blue] at (0.45,0.35) {#1};
}%
}
\ExplSyntaxOn
\NewDocumentCommand{\OneEquation}{}
{
\InitVariables
\int_case:nn { \Structure }
{
{0}{
\wcla_make_exercise:xxx {\FactorA}{\FactorB}{\AnswerSpace}
\wcla_make_answer:xxx {\FactorA}{\FactorB}{\ASF{\Product}}
}
{1}{
\wcla_make_exercise:xxx {\FactorA}{\AnswerSpace}{\Product}
\wcla_make_answer:xxx {\FactorA}{\ASF{\FactorB}}{\Product}
}
{2}{
\wcla_make_exercise:xxx {\AnswerSpace}{\FactorB}{\Product}
\wcla_make_answer:xxx {\ASF{\FactorA}}{\FactorB}{\Product}
}
}
}
\seq_new:N \l_wcla_exercise_list_seq
\seq_new:N \l_wcla_answer_list_seq
\cs_new_protected:Nn \wcla_make_exercise:nnn
{
\seq_put_right:Nn \l_wcla_exercise_list_seq { \( #1 \times #2 = #3 \) }
}
\cs_generate_variant:Nn \wcla_make_exercise:nnn { xxx }
\cs_new_protected:Nn \wcla_make_answer:nnn
{
\seq_put_right:Nn \l_wcla_answer_list_seq { \( #1 \times #2 = #3 \) }
}
\cs_generate_variant:Nn \wcla_make_answer:nnn { xxx }
\NewDocumentCommand{\ExerciseList}{m}
{
\prg_replicate:nn { #1 } { \OneEquation }
\seq_use:Nn \l_wcla_exercise_list_seq { \par \vspace{0.5cm} }
}
\NewDocumentCommand{\PrintAnswers}{}
{
\seq_use:Nn \l_wcla_answer_list_seq { \par \vspace{0.5cm} }
}
\ExplSyntaxOff
\begin{document}
\section{Exercises}
\ExerciseList{10}
\newpage
\section{Answers}
\PrintAnswers
\end{document}
我twocolumn
只是为了把所有东西都放在同一张图片中,以便更好地检查。
最值得注意的是如何\wcla_make_exercise:xxx
定义。首先我定义\wcla_make_exercise:nnn
将公式添加到包含练习列表的序列中\(#1\times#2=#3\)
。然后定义完全展开其三个参数的变体,这被称为,因此的值\FactorA
和\FactorB
是\Product
当前的值。在这里完全展开没有问题\ASF
,因为\NewDocumentCommand
这使得它很健壮。