我正在写一份包含问题和答案的文档。例如
\question{What is 2+3?}{2+3=5}
\question{What is cheese?}{Food made from milk.}
答案会被保存起来,以便在后面的部分收集,但答案会被打乱顺序,以防止被偷看。这意味着问题必须包含对答案编号的前向引用。例如:
Q2.1 2+3 等于多少?(见答案 104)
Q2.2 什么是奶酪?(见答案 57)
我有这个几乎在职的。
该\question
命令将所有答案写入临时文件,稍后的\printtheanswers
命令会将其发送到外部程序,该程序
a. 打乱答案顺序,
b. 为答案部分生成 Latex,并且
c. 生成一个辅助文件,qa.aux
该文件定义等来保存前向引用。\answernumber1
\answernumber2
这是一个两遍编译。qa.aux
如果文件存在,则在开始时加载。命令\question
必须\answernumberN
为每个不存在的文件定义一个占位符。
目前,\question
是部分定义的——它总是定义\answernumberN
为一个占位符:
\newcommand{\question}[2]{
\stepcounter{questionnumber}
\stepcounter{questionnumberinchapter}
% Define the placeholder for \answernumberN
\expandafter\edef\expandafter\answernumber\roman{questionnumber}{PLACEHOLDER:\arabic{questionnumber}}
% FOR DEBUGGING
DEFINING \texttt{\textbackslash answernumber\roman{questionnumber} as \{PLACEHOLDER:\arabic{questionnumber}\}}
% Write "x.y #2" to \qainpfilename
\immediate\write\qafile{\arabic{chapter}.\arabic{questionnumberinchapter} \unexpanded{#2}}
% Print the qustion to the document.
Q\arabic{chapter}.\arabic{questionnumberinchapter} #1 \emph{(See answer \expandafter\answernumber\roman{questionnumber}.)}
}
以下是我们感兴趣的一条线:
\expandafter\edef\expandafter\answernumber\roman{questionnumber}{PLACEHOLDER:\arabic{questionnumber}}
问题是\answernumberN
无论是否在辅助文件中定义,这始终定义。
这是一个 MWE,尽管如果没有外部程序你实际上无法编译它。
\documentclass[a4paper,10pt,openany]{scrbook}
\usepackage{verbatim}
% Define \bashline command
% Taken from https://gist.github.com/w495/7328b76e76aee49657e0bd7a3b46c870
\makeatletter
\newcommand{\bashline@file@name}[1]{%
/tmp/${USER}-${HOSTNAME}-\jobname-#1.tex%
}
\newread\bashline@file
\newcommand{\bashline@command@one}[2][tmp]{%
\immediate\write18{#2 > \bashline@file@name{#1}}
\openin\bashline@file=\bashline@file@name{#1}
% The group localizes the change to \endlinechar
\bgroup
\endlinechar=-1
\read\bashline@file to \localline
% Since everything in the group is local,
% we have to explicitly make the assignment global
\global\let\bashline@result\localline
\egroup
\closein\bashline@file
% Clean up after ourselves
\immediate\write18{rm \bashline@file@name{#1}}
\bashline@result
}
\newcommand{\bashline@command@many}[2][tmp]{%
\immediate\write18{#2 > \bashline@file@name{#1}}
\openin\bashline@file=\bashline@file@name{#1}
% The group localizes the change to \endlinechar
\newcount\linecnt
\bgroup
\endlinechar=-1
\loop\unless\ifeof\bashline@file
\read\bashline@file to \localline%
\localline
\newline
\repeat
\egroup
\closein\bashline@file
% Clean up after ourselves
\immediate\write18{rm \bashline@file@name{#1}}
}
\newcommand{\bashline}[2][tmp]{%
\bashline@command@one[#1]{#2}%
}
\newcommand{\bashlines}[2][tmp]{%
\bashline@command@many[#1]{#2}%
}
\makeatother
% Question-answer stuff.
\def\qainpfilename{qa.inp}
\def\qaauxfilename{qa.aux}
\def\qatexfilename{qa.tex}
\newwrite\qafile
\immediate\openout\qafile=\qainpfilename
\newcounter{questionnumber}
\newcounter{questionnumberinchapter}[chapter]
\newcommand{\question}[2]{
\stepcounter{questionnumber}
\stepcounter{questionnumberinchapter}
% Define the placeholder for \answernumberN
\expandafter\edef\expandafter\answernumber\roman{questionnumber}{PLACEHOLDER:\arabic{questionnumber}}
DEFINING \texttt{\textbackslash answernumber\roman{questionnumber} as \{PLACEHOLDER:\arabic{questionnumber}\}}
% Write "x.y #2" to \qainpfilename
\immediate\write\qafile{\arabic{chapter}.\arabic{questionnumberinchapter} \unexpanded{#2}}
% Print the qustion to the document.
Q\arabic{chapter}.\arabic{questionnumberinchapter} #1 \emph{(See answer \expandafter\answernumber\roman{questionnumber}.)}
}
\newcommand{\printtheanswers}{
\immediate\closeout\qafile
\bashline{bash bin/generate-answers.sh '\qainpfilename' '\qatexfilename' '\qaauxfilename'}
\input{\qatexfilename}
}
% Demonstrate that this will not be expanded until after qa.tex has been
% generated loaded into the document.
\def\examplething{this is expanded later}
\begin{document}
TEST
\IfFileExists{\qaauxfilename}{\input{\qaauxfilename}}{AUX DOES NOT EXIST}
\chapter{Maths}
\question{What is $1+2$?}{$1+2=3$ \examplething}
\question{What is $2\times 3$?}{$2\times 3=6$}
\chapter{Science}
\question{Name the six quarks.}{Up down, charm, strange, truth, and beauty.}
\question{How many planets orbit the Sun?}{Eight. Deal with it.}
\chapter{Answers}
\printtheanswers
\end{document}
qa.aux
为该文档生成的文件是
\def\answernumberi{3}% (Q1.1): $1+2=3$ \examplething
\def\answernumberii{1}% (Q1.2): $2\times 3=6$
\def\answernumberiii{4}% (Q2.1): Up down, charm, strange, truth, and beauty.
\def\answernumberiv{2}% (Q2.2): Eight. Deal with it.
我知道您可以使用\providecommand
各种 if-then 结构,但当命令名称由 构造时,我无法让它们工作\expandafter\answernumber\roman{questionnumber}
。如何有条件地定义一个名称由数字构成的命令,就像这样?
答案1
感谢 cfr,我现在使用\label
和\ref
该\question
命令现在包括
Q\arabic{chapter}.\arabic{questionnumberinchapter} #1 \emph{(See answer \expandafter\ref{\expandafter qanswer\arabic{questionnumber}}.)}
该\printtheanswers
命令生成如下文本:
\item\label{qanswer123} (text of answer to the 123rd question)