summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ximera/src/answer.dtx
blob: c6f5460d054ce85041cc1be09e37422147b03531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
% \subsubsection{Answers}
% \DescribeMacro{\answer}{A math answer}
%    \begin{macrocode}
%<*classXimera>

\ifdefined\HCode
\newcommand{\recordvariable}[1]{}
\else
\newwrite\idfile
\immediate\openout\idfile=\jobname.ids
\newcommand{\recordvariable}[1]{\ifthenelse{\equal{#1}{}}{}{\immediate\write\idfile{var #1;}}}
\fi
%    \end{macrocode}
% Determines if answer is shown in handout mode.  when |given=true|,
% show answer in handout mode, show answer in ``given box'' outside
% handout mode.  When |given=false|, do not show answer in handout
% mode, show answer outside handout mode
%    \begin{macrocode}
\define@key{answer}{given}[true]{\def\ans@given{#1}} 
%    \end{macrocode}
% Used for setting numeric answer tolerance for online student input.
%    \begin{macrocode}
\define@key{answer}{tolerance}{\def\ans@tol{#1}}
%    \end{macrocode}
% Used to run dynamic js code on student provided answers. Note:
% currently pdf outputs the validator code itself.
%    \begin{macrocode}
\define@key{answer}{validator}{}
%    \end{macrocode}
% Used for assigning a js ID to answer for dynamic code (eg validators).
%    \begin{macrocode}
\define@key{answer}{id}{\def\ans@id{#1}}
%    \end{macrocode}
% Used to set anticipated input format; eg "string".
%    \begin{macrocode}
\define@key{answer}{format}{}
%    \end{macrocode}
% Used to hide the answer input box on the web.
%    \begin{macrocode}
\define@key{answer}{onlinenoinput}[false]{}
%    \end{macrocode}
% Used to add a `show answer' button to the answer blank.
%    \begin{macrocode}
\define@key{answer}{onlineshowanswerbutton}[false]{}
%    \end{macrocode}
% Set default values for |\answer| command |key=value| pairs.
% Default values are |given = false|.
%    \begin{macrocode}
\setkeys{answer}{id=,given=false,onlinenoinput=false,onlineshowanswerbutton=false}
%    \end{macrocode}
% Basic code for |\answer|.
%    \begin{macrocode}

% Options for handout
\newcommand{\answerFormatLength}{2cm}

\newcommand{\answerFormatDots}[1]{\ldots\ldots}
\newcommand{\answerFormatLine}[1]{\protect\rule{\answerFormatLength}{0.4pt}}
\newcommand{\answerFormatFlexibleLine}[1]{\protect\rule{\widthof{$#1$}*2}{0.4pt}}
\newcommand{\answerFormatFlexibleBox}[1]{\fbox{\scalebox{2}{\phantom{$#1$}}}}

% options for default (i.e with answers filled in)
\newcommand{\answerFormatPlain}[1]{\ensuremath{#1}}
\newcommand{\answerFormatBlue}[1]{\color{blue}\ensuremath{#1}}
\newcommand{\answerFormatBoxed}[1]{\fbox{\ensuremath{#1}}}
\newcommand{\answerFormatBoxedGiven}[1]{\underset{\scriptstyle\mathrm{given}}{\fbox{\ensuremath{#1}}}}

% defaults for handout and default mode, and for \answer[given]
\let\handoutAnswerFormat\answerFormatDots
\let\defaultAnswerFormat\answerFormatBlue
\let\givenAnswerFormat\answerFormatBoxedGiven

\newcommand{\answer}[2][]{%
	\ifmmode%
		\setkeys{answer}{#1}%
		\recordvariable{\ans@id}
		\ifthenelse{\boolean{\ans@given}}
			{% Start then statement
			\ifhandout 
				#2
			\else
				\givenAnswerFormat{#2} %% in case the argument helps formatting
			\fi
			}% End then statement
			{% Start else statement
			\ifhandout
				\handoutAnswerFormat{#2} %% in case the argument helps formatting
			\else% show answer in box outside handout mode
				\defaultAnswerFormat{#2} %% in case the argument helps formatting
			\fi
			}% End else statement
	\else%
		\GenericError{\space\space\space\space}% Throw an error based on... something? -- Jason
		{Attempt to use \@backslashchar answer outside of math mode}
		{See https://github.com/ximeraProject/ximeraLatex for explanation.}
		{Need to use either inline or display math.}%
	\fi
}
%</classXimera>
%    \end{macrocode}
% On the HTML side, |\answer| emits spans---but it is usually just
% handled directly by MathJax.
%    \begin{macrocode}
%<*htXimera>
\renewcommand{\answer}[2][false]{\HCode{<span class="answer respondable">}#2\HCode{</span>}}

\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>}}
\def\endvalidator{\HCode{</div>}}

%</htXimera>
%    \end{macrocode}