| 1 |
% \subsubsection{Answers} |
| 2 |
% \DescribeMacro{\answer}{A math answer} |
| 3 |
% \begin{macrocode} |
| 4 |
%<*classXimera> |
| 5 |
|
| 6 |
\ifdefined\HCode |
| 7 |
\newcommand{\recordvariable}[1]{} |
| 8 |
\else |
| 9 |
\newwrite\idfile |
| 10 |
\immediate\openout\idfile=\jobname.ids |
| 11 |
\newcommand{\recordvariable}[1]{\ifthenelse{\equal{#1}{}}{}{\immediate\write\idfile{var #1;}}} |
| 12 |
\fi |
| 13 |
% \end{macrocode} |
| 14 |
% Determines if answer is shown in handout mode. when |given=true|, |
| 15 |
% show answer in handout mode, show answer in ``given box'' outside |
| 16 |
% handout mode. When |given=false|, do not show answer in handout |
| 17 |
% mode, show answer outside handout mode |
| 18 |
% \begin{macrocode} |
| 19 |
\define@key{answer}{given}[true]{\def\ans@given{#1}} |
| 20 |
% \end{macrocode} |
| 21 |
% Used for setting numeric answer tolerance for online student input. |
| 22 |
% \begin{macrocode} |
| 23 |
\define@key{answer}{tolerance}{\def\ans@tol{#1}} |
| 24 |
% \end{macrocode} |
| 25 |
% Used to run dynamic js code on student provided answers. Note: |
| 26 |
% currently pdf outputs the validator code itself. |
| 27 |
% \begin{macrocode} |
| 28 |
\define@key{answer}{validator}{} |
| 29 |
% \end{macrocode} |
| 30 |
% Used for assigning a js ID to answer for dynamic code (eg validators). |
| 31 |
% \begin{macrocode} |
| 32 |
\define@key{answer}{id}{\def\ans@id{#1}} |
| 33 |
% \end{macrocode} |
| 34 |
% Used to set anticipated input format; eg "string". |
| 35 |
% \begin{macrocode} |
| 36 |
\define@key{answer}{format}{} |
| 37 |
% \end{macrocode} |
| 38 |
% Used to hide the answer input box on the web. |
| 39 |
% \begin{macrocode} |
| 40 |
\define@key{answer}{onlinenoinput}[false]{} |
| 41 |
% \end{macrocode} |
| 42 |
% Used to add a `show answer' button to the answer blank. |
| 43 |
% \begin{macrocode} |
| 44 |
\define@key{answer}{onlineshowanswerbutton}[false]{} |
| 45 |
% \end{macrocode} |
| 46 |
% Set default values for |\answer| command |key=value| pairs. |
| 47 |
% Default values are |given = false|. |
| 48 |
% \begin{macrocode} |
| 49 |
\setkeys{answer}{id=,given=false,onlinenoinput=false,onlineshowanswerbutton=false} |
| 50 |
% \end{macrocode} |
| 51 |
% Basic code for |\answer|. |
| 52 |
% \begin{macrocode} |
| 53 |
|
| 54 |
% Options for handout |
| 55 |
\newcommand{\answerFormatLength}{2cm} |
| 56 |
|
| 57 |
\newcommand{\answerFormatDots}[1]{\ldots\ldots} |
| 58 |
\newcommand{\answerFormatLine}[1]{\protect\rule{\answerFormatLength}{0.4pt}} |
| 59 |
\newcommand{\answerFormatFlexibleLine}[1]{\protect\rule{\widthof{$#1$}*2}{0.4pt}} |
| 60 |
\newcommand{\answerFormatFlexibleBox}[1]{\fbox{\scalebox{2}{\phantom{$#1$}}}} |
| 61 |
|
| 62 |
% options for default (i.e with answers filled in) |
| 63 |
\newcommand{\answerFormatPlain}[1]{\ensuremath{#1}} |
| 64 |
\newcommand{\answerFormatBlue}[1]{\color{blue}\ensuremath{#1}} |
| 65 |
\newcommand{\answerFormatBoxed}[1]{\fbox{\ensuremath{#1}}} |
| 66 |
\newcommand{\answerFormatBoxedGiven}[1]{\underset{\scriptstyle\mathrm{given}}{\fbox{\ensuremath{#1}}}} |
| 67 |
|
| 68 |
% defaults for handout and default mode, and for \answer[given] |
| 69 |
\let\handoutAnswerFormat\answerFormatDots |
| 70 |
\let\defaultAnswerFormat\answerFormatBlue |
| 71 |
\let\givenAnswerFormat\answerFormatBoxedGiven |
| 72 |
|
| 73 |
\newcommand{\answer}[2][]{% |
| 74 |
\ifmmode% |
| 75 |
\setkeys{answer}{#1}% |
| 76 |
\recordvariable{\ans@id} |
| 77 |
\ifthenelse{\boolean{\ans@given}} |
| 78 |
{% Start then statement |
| 79 |
\ifhandout |
| 80 |
#2 |
| 81 |
\else |
| 82 |
\givenAnswerFormat{#2} %% in case the argument helps formatting |
| 83 |
\fi |
| 84 |
}% End then statement |
| 85 |
{% Start else statement |
| 86 |
\ifhandout |
| 87 |
\handoutAnswerFormat{#2} %% in case the argument helps formatting |
| 88 |
\else% show answer in box outside handout mode |
| 89 |
\defaultAnswerFormat{#2} %% in case the argument helps formatting |
| 90 |
\fi |
| 91 |
}% End else statement |
| 92 |
\else% |
| 93 |
\GenericError{\space\space\space\space}% Throw an error based on... something? -- Jason |
| 94 |
{Attempt to use \@backslashchar answer outside of math mode} |
| 95 |
{See https://github.com/ximeraProject/ximeraLatex for explanation.} |
| 96 |
{Need to use either inline or display math.}% |
| 97 |
\fi |
| 98 |
} |
| 99 |
%</classXimera> |
| 100 |
% \end{macrocode} |
| 101 |
% On the HTML side, |\answer| emits spans---but it is usually just |
| 102 |
% handled directly by MathJax. |
| 103 |
% \begin{macrocode} |
| 104 |
%<*htXimera> |
| 105 |
\renewcommand{\answer}[2][false]{\HCode{<span class="answer respondable">}#2\HCode{</span>}} |
| 106 |
|
| 107 |
\def\validator[#1]{\stepcounter{identification}\HCode{<div class="validator" id="validator\arabic{identification}"><script type="text/javascript">function validator\arabic{identification}() }\{\HCode{ return \detokenize{#1}; }\}\HCode{</script>}} |
| 108 |
\def\endvalidator{\HCode{</div>}} |
| 109 |
|
| 110 |
%</htXimera> |
| 111 |
% \end{macrocode} |