질문과 답변이 포함된 문서를 작성 중입니다. 예를 들어
\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
명령은 이를 외부 프로그램으로 보냅니다.
ㅏ. 답변을 섞고,
비. 답변 섹션에 대한 라텍스를 생성하고
씨. 전방 참조를 보유하기 위해 등을 정의 qa.aux
하는 보조 파일을 생성합니다 .\answernumber1
\answernumber2
이것은 2단계 편집입니다. 파일 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.
다양한 if-then 구성을 사용할 수 있다는 것을 알고 있지만 \providecommand
명령 이름이 \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
다음과 같은 tex를 생성합니다.
\item\label{qanswer123} (text of answer to the 123rd question)