[system] / trunk / pg / macros / PGbasicmacros.pl Repository:
ViewVC logotype

Annotation of /trunk/pg/macros/PGbasicmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1462 - (view) (download) (as text)

1 : sh002i 1050
2 : apizer 1080
3 : sh002i 1050 ####################################################################
4 :     # Copyright @ 1995-1998 University of Rochester
5 :     # All Rights Reserved
6 :     ####################################################################
7 :    
8 :     =head1 NAME
9 :    
10 :     PGbasicmacros.pl --- located in the courseScripts directory
11 :    
12 :     =head1 SYNPOSIS
13 :    
14 :    
15 :    
16 :     =head1 DESCRIPTION
17 :    
18 :    
19 :    
20 :     =cut
21 :    
22 :     # this is equivalent to use strict, but can be used within the Safe compartment.
23 :     BEGIN{
24 : gage 1267 be_strict;
25 : sh002i 1050 }
26 :    
27 :    
28 : gage 1251 my $displayMode;
29 : sh002i 1050
30 :     my ($PAR,
31 :     $BR,
32 :     $LQ,
33 :     $RQ,
34 :     $BM,
35 :     $EM,
36 :     $BDM,
37 :     $EDM,
38 :     $LTS,
39 :     $GTS,
40 :     $LTE,
41 :     $GTE,
42 :     $BEGIN_ONE_COLUMN,
43 :     $END_ONE_COLUMN,
44 :     $SOL,
45 :     $SOLUTION,
46 :     $HINT,
47 :     $US,
48 :     $SPACE,
49 :     $BBOLD,
50 : apizer 1080 $EBOLD,
51 : sh002i 1050 $BITALIC,
52 :     $EITALIC,
53 :     $BCENTER,
54 :     $ECENTER,
55 :     $HR,
56 :     $LBRACE,
57 :     $RBRACE,
58 :     $LB,
59 :     $RB,
60 :     $DOLLAR,
61 :     $PERCENT,
62 :     $CARET,
63 :     $PI,
64 :     $E,
65 :     @ALPHABET,
66 : gage 1251 $envir,
67 : gage 1253 $PG_random_generator,
68 : gage 1267 $inputs_ref,
69 : gage 1462 $rh_sticky_answers,
70 :     $r_ans_rule_count,
71 : sh002i 1050 );
72 :    
73 :     sub _PGbasicmacros_init {
74 : gage 1251
75 :     # The big problem is that at compile time in the cached Safe compartment
76 :     # main:: has one definition, probably Safe::Root1::
77 :     # At runtime main has another definition Safe::Rootx:: where x is > 1
78 : apizer 1314
79 :     # It is important to
80 : gage 1251 # initialize the my variable version of $displayMode from the "runtime" version
81 :     # of main::displayMode
82 : apizer 1314
83 : gage 1251 $displayMode = main::PG_restricted_eval(q!$main::displayMode!);
84 :    
85 :     # This is initializes the remaining variables in the runtime main:: compartment.
86 : apizer 1314
87 : gage 1251 main::PG_restricted_eval( <<'EndOfFile');
88 : gage 1267 $displayMode = $displayMode;
89 : gage 1251
90 : gage 1286 $main::PAR = PAR();
91 :     $main::BR = BR();
92 : sh002i 1050 $main::LQ = LQ();
93 :     $main::RQ = RQ();
94 : gage 1286 $main::BM = BM();
95 :     $main::EM = EM();
96 : sh002i 1050 $main::BDM = BDM();
97 :     $main::EDM = EDM();
98 :     $main::LTS = LTS();
99 :     $main::GTS = GTS();
100 :     $main::LTE = LTE();
101 :     $main::GTE = GTE();
102 :     $main::BEGIN_ONE_COLUMN = BEGIN_ONE_COLUMN();
103 :     $main::END_ONE_COLUMN = END_ONE_COLUMN();
104 :     $main::SOL = SOLUTION_HEADING();
105 :     $main::SOLUTION = SOLUTION_HEADING();
106 :     $main::HINT = HINT_HEADING();
107 :     $main::US = US();
108 :     $main::SPACE = SPACE();
109 : gage 1286 $main::BBOLD = BBOLD();
110 :     $main::EBOLD = EBOLD();
111 : sh002i 1050 $main::BITALIC = BITALIC();
112 :     $main::EITALIC = EITALIC();
113 :     $main::BCENTER = BCENTER();
114 :     $main::ECENTER = ECENTER();
115 :     $main::HR = HR();
116 :     $main::LBRACE = LBRACE();
117 :     $main::RBRACE = RBRACE();
118 :     $main::LB = LB();
119 :     $main::RB = RB();
120 :     $main::DOLLAR = DOLLAR();
121 :     $main::PERCENT = PERCENT();
122 :     $main::CARET = CARET();
123 :     $main::PI = PI();
124 :     $main::E = E();
125 :     @main::ALPHABET = ('A'..'ZZ');
126 : gage 1462 %main::STICKY_ANSWERS = ();
127 : apizer 1080
128 : gage 1251
129 :     EndOfFile
130 :    
131 :     # Next we transfer the correct definitions in the main:: compartment to the local my variables
132 :     # This can't be done inside the eval above because my variables seem to be invisible inside the eval
133 :    
134 :    
135 : gage 1286 $PAR = PAR();
136 :     $BR = BR();
137 :     $LQ = LQ();
138 :     $RQ = RQ();
139 :     $BM = BM();
140 :     $EM = EM();
141 :     $BDM = BDM();
142 :     $EDM = EDM();
143 :     $LTS = LTS();
144 :     $GTS = GTS();
145 :     $LTE = LTE();
146 :     $GTE = GTE();
147 :     $BEGIN_ONE_COLUMN = BEGIN_ONE_COLUMN();
148 :     $END_ONE_COLUMN = END_ONE_COLUMN();
149 :     $SOL = SOLUTION_HEADING();
150 :     $SOLUTION = SOLUTION_HEADING();
151 :     $HINT = HINT_HEADING();
152 :     $US = US();
153 :     $SPACE = SPACE();
154 :     $BBOLD = BBOLD();
155 :     $EBOLD = EBOLD();
156 :     $BITALIC = BITALIC();
157 :     $EITALIC = EITALIC();
158 :     $BCENTER = BCENTER();
159 :     $ECENTER = ECENTER();
160 :     $HR = HR();
161 :     $LBRACE = LBRACE();
162 :     $RBRACE = RBRACE();
163 :     $LB = LB();
164 :     $RB = RB();
165 :     $DOLLAR = DOLLAR();
166 :     $PERCENT = PERCENT();
167 :     $CARET = CARET();
168 :     $PI = PI();
169 :     $E = E();
170 :     @ALPHABET = ('A'..'ZZ');
171 : sh002i 1050
172 : gage 1253 $envir = PG_restricted_eval(q!\%main::envir!);
173 :     $PG_random_generator = PG_restricted_eval(q!$main::PG_random_generator!);
174 : gage 1267 $inputs_ref = $envir{inputs_ref};
175 : gage 1462 $rh_sticky_answers = PG_restricted_eval(q!\%main::STICKY_ANSWERS!);
176 :     $r_ans_rule_count = PG_restricted_eval(q!\$ans_rule_count!);
177 : sh002i 1050 }
178 :    
179 :     =head2 Answer blank macros:
180 :    
181 :     These produce answer blanks of various sizes or pop up lists or radio answer buttons.
182 :     The names for the answer blanks are
183 :     generated implicitly.
184 :    
185 :     ans_rule( width )
186 :     tex_ans_rule( width )
187 :     ans_radio_buttons(value1=>label1, value2,label2 => value3,label3=>...)
188 :     pop_up_list(@list) # list consists of (value => label, PR => "Product rule",...)
189 :    
190 :     To indicate the checked position of radio buttons put a '%' in front of the value: C<ans_radio_buttons(1, 'Yes','%2','No')>
191 :     will have 'No' checked. C<tex_ans_rule> works inside math equations in C<HTML_tth> mode. It does not work in C<Latex2HTML> mode
192 :     since this mode produces gif pictures.
193 :    
194 :    
195 :     The following method is defined in F<PG.pl> for entering the answer evaluators corresponding
196 :     to answer rules with automatically generated names. The answer evaluators are matched with the
197 :     answer rules in the order in which they appear on the page.
198 :    
199 :     ANS(ans_evaluator1, ans_evaluator2,...);
200 :    
201 :     These are more primitive macros which produce answer blanks for specialized cases when complete
202 :     control over the matching of answers blanks and answer evaluators is desired.
203 :     The names of the answer blanks must be generated manually, and it is best if they do NOT begin
204 :     with the default answer prefix (currently AnSwEr).
205 :    
206 :     labeled_ans_rule(name, width) # an alias for NAMED_ANS_RULE where width defaults to 20 if omitted.
207 :    
208 :     NAMED_ANS_RULE(name, width)
209 :     NAMED_ANS_BOX(name, rows, cols)
210 :     NAMED_ANS_RADIO(name, value,label,)
211 :     NAMED_ANS_RADIO_EXTENSION(name, value,label)
212 :     NAMED_ANS_RADIO_BUTTONS(name,value1,label1,value2,label2,...)
213 :     check_box('-name' =>answer5,'-value' =>'statement3','-label' =>'I loved this course!' )
214 :     NAMED_POP_UP_LIST($name, @list) # list consists of (value => tag, PR => "Product rule",...)
215 :    
216 :     (Name is the name of the variable, value is the value given to the variable when this option is selected,
217 :     and label is the text printed next to the button or check box. Check box variables can have multiple values.)
218 :    
219 :     NAMED_ANS_RADIO_BUTTONS creates a sequence of NAMED_ANS_RADIO and NAMED_ANS_RADIO_EXTENSION items which
220 :     are output either as an array or, in scalar context, as the array glued together with spaces. It is
221 :     usually easier to use this than to manually construct the radio buttons by hand. However, sometimes
222 :     extra flexibility is desiredin which case:
223 :    
224 :     When entering radio buttons using the "NAMED" format, you should use NAMED_ANS_RADIO button for the first button
225 :     and then use NAMED_ANS_RADIO_EXTENSION for the remaining buttons. NAMED_ANS_RADIO requires a matching answer evalutor,
226 :     while NAMED_ANS_RADIO_EXTENSION does not. The name used for NAMED_ANS_RADIO_EXTENSION should match the name
227 :     used for NAMED_ANS_RADIO (and the associated answer evaluator).
228 :    
229 :    
230 :     The following method is defined in F<PG.pl> for entering the answer evaluators corresponding
231 :     to answer rules with automatically generated names. The answer evaluators are matched with the
232 :     answer rules in the order in which they appear on the page.
233 :    
234 :     NAMED_ANS(name1 => ans_evaluator1, name2 => ans_evaluator2,...);
235 :    
236 :     These auxiliary macros are defined in PG.pl
237 :    
238 :    
239 :     NEW_ANS_NAME( number ); # produces a new answer blank name from a number by adding a prefix (AnSwEr)
240 :     # and registers this name as an implicitly labeled answer
241 :     # Its use is paired with each answer evaluator being entered using ANS()
242 :    
243 :     ANS_NUM_TO_NAME(number); # adds the prefix (AnSwEr) to the number, but does nothing else.
244 :    
245 :     RECORD_ANS_NAME( name ); # records the order in which the answer blank is rendered
246 :     # This is called by all of the constructs above, but must
247 :     # be called explicitly if an input blank is constructed explictly
248 :     # using HTML code.
249 :    
250 :     These are legacy macros:
251 :    
252 :     ANS_RULE( number, width ); # equivalent to NAMED_ANS_RULE( NEW_ANS_NAME(number), width)
253 :     ANS_BOX( question_number,height, width ); # equivalent to NAMED_ANS_BOX( NEW_ANS_NAME(number), height, width)
254 :     ANS_RADIO( question_number, value,tag ); # equivalent to NAMED_ANS_RADIO( NEW_ANS_NAME(number), value,tag)
255 :     ANS_RADIO_OPTION( question_number, value,tag ); # equivalent to NAMED_ANS_RADIO_EXTENSION( ANS_NUM_TO_NAME(number), value,tag)
256 :    
257 :    
258 :     =cut
259 :    
260 : gage 1267
261 :    
262 : sh002i 1050 sub labeled_ans_rule { # syntactic sugar for NAMED_ANS_RULE
263 :     my($name,$col) = @_;
264 :     $col = 20 unless defined($col);
265 :     NAMED_ANS_RULE($name,$col);
266 :     }
267 :    
268 :     sub NAMED_ANS_RULE {
269 :     my($name,$col) = @_;
270 :     my $len = 0.07*$col;
271 :     my $answer_value = '';
272 : gage 1267 $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
273 : sh002i 1050 if ($answer_value =~ /\0/ ) {
274 :     my @answers = split("\0", $answer_value);
275 :     $answer_value = shift(@answers); # use up the first answer
276 : gage 1462 $rh_sticky_answers->{$name}=\@answers;
277 : apizer 1314 # store the rest -- beacuse this stores to a main:; variable
278 :     # it must be evaluated at run time
279 : sh002i 1050 $answer_value= '' unless defined($answer_value);
280 :     } elsif (ref($answer_value) eq 'ARRAY') {
281 :     my @answers = @{ $answer_value};
282 :     $answer_value = shift(@answers); # use up the first answer
283 : gage 1462 $rh_sticky_answers->{$name}=\@answers;
284 : apizer 1314 # store the rest -- beacuse this stores to a main:; variable
285 :     # it must be evaluated at run time
286 : sh002i 1050 $answer_value= '' unless defined($answer_value);
287 : apizer 1080 }
288 :    
289 : apizer 1314 $answer_value =~ tr/\\$@`//d; ## make sure student answers can not be interpolated by e.g. EV3
290 : sh002i 1050 $name = RECORD_ANS_NAME($name);
291 : apizer 1379
292 : apizer 1385 # incorporated Davide Cervone's changes
293 : apizer 1379 # removed newlines from around <INPUT> tags
294 :     # made TeX rule be based on specified width rather than varying size.
295 : apizer 1385 my $tcol = $col/2 > 3 ? $col/2 : 3; ## get max
296 :     $tcol = $tcol < 40 ? $tcol : 40; ## get min
297 : apizer 1379
298 : sh002i 1050 MODES(
299 : apizer 1379 TeX => "\\mbox{\\parbox[t]{${tcol}ex}{\\hrulefill}}",
300 :     Latex2HTML => qq!\\begin{rawhtml}<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"\">\\end{rawhtml}!,
301 :     HTML => "<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"$answer_value\">"
302 : sh002i 1050 );
303 :     }
304 :    
305 :     sub NAMED_ANS_RULE_OPTION { # deprecated
306 :     &NAMED_ANS_RULE_EXTENSION;
307 :     }
308 :    
309 :     sub NAMED_ANS_RULE_EXTENSION {
310 :     my($name,$col) = @_;
311 :     my $len = 0.07*$col;
312 :     my $answer_value = '';
313 : gage 1267 $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
314 : gage 1462 if ( defined( $rh_sticky_answers->{$name} ) ) {
315 :     $answer_value = shift( @{ $rh_sticky_answers->{$name} });
316 : sh002i 1050 $answer_value = '' unless defined($answer_value);
317 :     }
318 : apizer 1314 $answer_value =~ tr/\\$@`//d; ## make sure student answers can not be interpolated by e.g. EV3
319 : sh002i 1050 MODES(
320 :     TeX => '\\hrulefill\\quad ',
321 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"\">\n\\end{rawhtml}\n!,
322 :     HTML => qq!<INPUT TYPE=TEXT SIZE=$col NAME = "$name" VALUE = "$answer_value">\n!
323 :     );
324 :     }
325 :    
326 :     sub ANS_RULE { #deprecated
327 :     my($number,$col) = @_;
328 :     my $name = NEW_ANS_NAME($number);
329 :     NAMED_ANS_RULE($name,$col);
330 :     }
331 :    
332 :    
333 :     sub NAMED_ANS_BOX {
334 :     my($name,$row,$col) = @_;
335 :     $row = 10 unless defined($row);
336 :     $col = 80 unless defined($col);
337 :     $name = RECORD_ANS_NAME($name);
338 :     my $len = 0.07*$col;
339 :     my $height = .07*$row;
340 :     my $answer_value = '';
341 : gage 1267 $answer_value = $inputs_ref->{$name} if defined( $inputs_ref->{$name} );
342 : apizer 1314 $answer_value =~ tr/\\$@`//d; ## make sure student answers can not be interpolated by e.g. EV3
343 : sh002i 1050 my $out = M3(
344 :     qq!\\vskip $height in \\hrulefill\\quad !,
345 :     qq!\\begin{rawhtml}<TEXTAREA NAME="$name" ROWS="$row" COLS="$col"
346 :     WRAP="VIRTUAL">$answer_value</TEXTAREA>\\end{rawhtml}!,
347 :     qq!<TEXTAREA NAME="$name" ROWS="$row" COLS="$col"
348 :     WRAP="VIRTUAL">$answer_value</TEXTAREA>!
349 :     );
350 :     $out;
351 :     }
352 :    
353 :     sub ANS_BOX { #deprecated
354 :     my($number,$row,$col) = @_;
355 :     my $name = NEW_ANS_NAME($number);
356 :     NAMED_ANS_BOX($name,$row,$col);
357 :     }
358 :    
359 :     sub NAMED_ANS_RADIO {
360 :     my $name = shift;
361 :     my $value = shift;
362 :     my $tag =shift;
363 :     $name = RECORD_ANS_NAME($name);
364 :     my $checked = '';
365 :     if ($value =~/^\%/) {
366 :     $value =~ s/^\%//;
367 :     $checked = 'CHECKED'
368 :     }
369 : gage 1267 if (defined($inputs_ref->{$name}) ) {
370 :     if ($inputs_ref->{$name} eq $value) {
371 : sh002i 1050 $checked = 'CHECKED'
372 :     } else {
373 :     $checked = '';
374 :     }
375 :    
376 :     }
377 :    
378 :     MODES(
379 :     TeX => qq!\\item{$tag}\n!,
380 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=RADIO NAME="$name" VALUE="$value" $checked>\\end{rawhtml}$tag!,
381 :     HTML => qq!<INPUT TYPE=RADIO NAME="$name" VALUE="$value" $checked>$tag!
382 :     );
383 :    
384 :     }
385 :    
386 :     sub NAMED_ANS_RADIO_OPTION { #deprecated
387 :     &NAMED_ANS_RADIO_EXTENSION;
388 :     }
389 :    
390 :     sub NAMED_ANS_RADIO_EXTENSION {
391 :     my $name = shift;
392 :     my $value = shift;
393 :     my $tag =shift;
394 :    
395 :    
396 :     my $checked = '';
397 :     if ($value =~/^\%/) {
398 :     $value =~ s/^\%//;
399 :     $checked = 'CHECKED'
400 :     }
401 : gage 1267 if (defined($inputs_ref->{$name}) ) {
402 :     if ($inputs_ref->{$name} eq $value) {
403 : sh002i 1050 $checked = 'CHECKED'
404 :     } else {
405 :     $checked = '';
406 :     }
407 :    
408 :     }
409 :    
410 :     MODES(
411 :     TeX => qq!\\item{$tag}\n!,
412 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=RADIO NAME="$name" VALUE="$value" $checked>\\end{rawhtml}$tag!,
413 :     HTML => qq!<INPUT TYPE=RADIO NAME="$name" VALUE="$value" $checked>$tag!
414 :     );
415 :    
416 :     }
417 :    
418 :     sub NAMED_ANS_RADIO_BUTTONS {
419 :     my $name =shift;
420 :     my $value = shift;
421 :     my $tag = shift;
422 :    
423 :    
424 :     my @out = ();
425 :     push(@out, NAMED_ANS_RADIO($name, $value,$tag));
426 :     my @buttons = @_;
427 :     while (@buttons) {
428 :     $value = shift @buttons; $tag = shift @buttons;
429 :     push(@out, NAMED_ANS_RADIO_OPTION($name, $value,$tag));
430 :     }
431 :     (wantarray) ? @out : join(" ",@out);
432 :     }
433 :     sub ANS_RADIO {
434 :     my $number = shift;
435 :     my $value = shift;
436 :     my $tag =shift;
437 :     my $name = NEW_ANS_NAME($number);
438 :     NAMED_ANS_RADIO($name,$value,$tag);
439 :     }
440 :    
441 :     sub ANS_RADIO_OPTION {
442 :     my $number = shift;
443 :     my $value = shift;
444 :     my $tag =shift;
445 :    
446 :    
447 :     my $name = ANS_NUM_TO_NAME($number);
448 :     NAMED_ANS_RADIO_OPTION($name,$value,$tag);
449 :     }
450 :     sub ANS_RADIO_BUTTONS {
451 :     my $number =shift;
452 :     my $value = shift;
453 :     my $tag = shift;
454 :    
455 :    
456 :     my @out = ();
457 :     push(@out, ANS_RADIO($number, $value,$tag));
458 :     my @buttons = @_;
459 :     while (@buttons) {
460 :     $value = shift @buttons; $tag = shift @buttons;
461 :     push(@out, ANS_RADIO_OPTION($number, $value,$tag));
462 :     }
463 :     (wantarray) ? @out : join(" ",@out);
464 :     }
465 :    
466 :     sub NAMED_ANS_CHECKBOX {
467 :     my $name = shift;
468 :     my $value = shift;
469 :     my $tag =shift;
470 :     $name = RECORD_ANS_NAME($name);
471 :    
472 :     my $checked = '';
473 :     if ($value =~/^\%/) {
474 :     $value =~ s/^\%//;
475 :     $checked = 'CHECKED'
476 :     }
477 :    
478 : gage 1267 if (defined($inputs_ref->{$name}) ) {
479 :     if ($inputs_ref->{$name} eq $value) {
480 : sh002i 1050 $checked = 'CHECKED'
481 :     }
482 :     else {
483 :     $checked = '';
484 :     }
485 :    
486 :     }
487 :    
488 :     MODES(
489 :     TeX => qq!\\item{$tag}\n!,
490 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=CHECKBOX NAME="$name" VALUE="$value" $checked>\\end{rawhtml}$tag!,
491 :     HTML => qq!<INPUT TYPE=CHECKBOX NAME="$name" VALUE="$value" $checked>$tag!
492 :     );
493 :    
494 :     }
495 :    
496 :     sub NAMED_ANS_CHECKBOX_OPTION {
497 :     my $name = shift;
498 :     my $value = shift;
499 :     my $tag =shift;
500 :    
501 :     my $checked = '';
502 :     if ($value =~/^\%/) {
503 :     $value =~ s/^\%//;
504 :     $checked = 'CHECKED'
505 :     }
506 :    
507 : gage 1267 if (defined($inputs_ref->{$name}) ) {
508 :     if ($inputs_ref->{$name} eq $value) {
509 : sh002i 1050 $checked = 'CHECKED'
510 :     }
511 :     else {
512 :     $checked = '';
513 :     }
514 :    
515 :     }
516 :    
517 :     MODES(
518 :     TeX => qq!\\item{$tag}\n!,
519 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=CHECKBOX NAME="$name" VALUE="$value" $checked>\\end{rawhtml}$tag!,
520 :     HTML => qq!<INPUT TYPE=CHECKBOX NAME="$name" VALUE="$value" $checked>$tag!
521 :     );
522 :    
523 :     }
524 :    
525 :     sub NAMED_ANS_CHECKBOX_BUTTONS {
526 :     my $name =shift;
527 :     my $value = shift;
528 :     my $tag = shift;
529 :    
530 :     my @out = ();
531 :     push(@out, NAMED_ANS_CHECKBOX($name, $value,$tag));
532 :    
533 :     my @buttons = @_;
534 :     while (@buttons) {
535 :     $value = shift @buttons; $tag = shift @buttons;
536 :     push(@out, NAMED_ANS_CHECKBOX_OPTION($name, $value,$tag));
537 :     }
538 :    
539 :     (wantarray) ? @out : join(" ",@out);
540 :     }
541 :    
542 :     sub ANS_CHECKBOX {
543 :     my $number = shift;
544 :     my $value = shift;
545 :     my $tag =shift;
546 :     my $name = NEW_ANS_NAME($number);
547 :    
548 :     NAMED_ANS_CHECKBOX($name,$value,$tag);
549 :     }
550 :    
551 :     sub ANS_CHECKBOX_OPTION {
552 :     my $number = shift;
553 :     my $value = shift;
554 :     my $tag =shift;
555 :     my $name = ANS_NUM_TO_NAME($number);
556 :    
557 :     NAMED_ANS_CHECKBOX_OPTION($name,$value,$tag);
558 :     }
559 :    
560 :     sub ANS_CHECKBOX_BUTTONS {
561 :     my $number =shift;
562 :     my $value = shift;
563 :     my $tag = shift;
564 :    
565 :     my @out = ();
566 :     push(@out, ANS_CHECKBOX($number, $value, $tag));
567 :    
568 :     my @buttons = @_;
569 :     while (@buttons) {
570 :     $value = shift @buttons; $tag = shift @buttons;
571 :     push(@out, ANS_CHECKBOX_OPTION($number, $value,$tag));
572 :     }
573 :    
574 :     (wantarray) ? @out : join(" ",@out);
575 :     }
576 :    
577 :     sub ans_rule {
578 :     my $len = shift; # gives the optional length of the answer blank
579 :     $len = 20 unless $len ;
580 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count());
581 : sh002i 1050 NAMED_ANS_RULE($name ,$len);
582 :     }
583 :     sub ans_rule_extension {
584 :     my $len = shift;
585 :     $len = 20 unless $len ;
586 : gage 1462 my $name = NEW_ANS_NAME($$r_ans_rule_count); # don't update the answer name
587 : sh002i 1050 NAMED_ANS_RULE($name ,$len);
588 :     }
589 :     sub ans_radio_buttons {
590 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count());
591 : sh002i 1050 my @radio_buttons = NAMED_ANS_RADIO_BUTTONS($name, @_);
592 :    
593 :     if ($displayMode eq 'TeX') {
594 :     $radio_buttons[0] = "\n\\begin{itemize}\n" . $radio_buttons[0];
595 :     $radio_buttons[$#radio_buttons] .= "\n\\end{itemize}\n";
596 :     }
597 :    
598 :     (wantarray) ? @radio_buttons: join(" ", @radio_buttons);
599 :     }
600 :    
601 :     #added 6/14/2000 by David Etlinger
602 :     sub ans_checkbox {
603 : gage 1267 my $name = NEW_ANS_NAME( inc_ans_rule_count() );
604 : sh002i 1050 my @checkboxes = NAMED_ANS_CHECKBOX_BUTTONS( $name, @_ );
605 :    
606 :     if ($displayMode eq 'TeX') {
607 :     $checkboxes[0] = "\n\\begin{itemize}\n" . $checkboxes[0];
608 :     $checkboxes[$#checkboxes] .= "\n\\end{itemize}\n";
609 :     }
610 :    
611 :     (wantarray) ? @checkboxes: join(" ", @checkboxes);
612 :     }
613 :    
614 :    
615 :     ## define a version of ans_rule which will work inside TeX math mode or display math mode -- at least for tth mode.
616 :     ## This is great for displayed fractions.
617 :     ## This will not work with latex2HTML mode since it creates gif equations.
618 :    
619 :     sub tex_ans_rule {
620 :     my $len = shift;
621 :     $len = 20 unless $len ;
622 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count());
623 : sh002i 1050 my $answer_rule = NAMED_ANS_RULE($name ,$len); # we don't want to create three answer rules in different modes.
624 :     my $out = MODES(
625 :     'TeX' => $answer_rule,
626 :     'Latex2HTML' => '\\fbox{Answer boxes cannot be placed inside typeset equations}',
627 :     'HTML_tth' => '\\begin{rawhtml} '. $answer_rule.'\\end{rawhtml}',
628 :     'HTML_dpng' => '\\fbox{Answer boxes cannot be placed inside typeset equations}',
629 :     'HTML' => $answer_rule
630 :     );
631 : apizer 1080
632 : sh002i 1050 $out;
633 :     }
634 :     sub tex_ans_rule_extension {
635 :     my $len = shift;
636 :     $len = 20 unless $len ;
637 : gage 1462 my $name = NEW_ANS_NAME($$r_ans_rule_count);
638 : sh002i 1050 my $answer_rule = NAMED_ANS_RULE($name ,$len); # we don't want to create three answer rules in different modes.
639 :     my $out = MODES(
640 :     'TeX' => $answer_rule,
641 :     'Latex2HTML' => '\fbox{Answer boxes cannot be placed inside typeset equations}',
642 :     'HTML_tth' => '\\begin{rawhtml} '. $answer_rule.'\\end{rawhtml}',
643 :     'HTML_dpng' => '\fbox{Answer boxes cannot be placed inside typeset equations}',
644 :     'HTML' => $answer_rule
645 :     );
646 : apizer 1080
647 : sh002i 1050 $out;
648 :     }
649 :     # still needs some cleanup.
650 :     sub NAMED_TEX_ANS_RULE {
651 :     my $name = shift;
652 :     my $len = shift;
653 :     $len = 20 unless $len ;
654 :     my $answer_rule = NAMED_ANS_RULE($name ,$len); # we don't want to create three answer rules in different modes.
655 :     my $out = MODES(
656 :     'TeX' => $answer_rule,
657 :     'Latex2HTML' => '\\fbox{Answer boxes cannot be placed inside typeset equations}',
658 :     'HTML_tth' => '\\begin{rawhtml} '. $answer_rule.'\\end{rawhtml}',
659 :     'HTML_dpng' => '\\fbox{Answer boxes cannot be placed inside typeset equations}',
660 :     'HTML' => $answer_rule
661 :     );
662 : apizer 1080
663 : sh002i 1050 $out;
664 :     }
665 :     sub NAMED_TEX_ANS_RULE_EXTENSION {
666 :     my $name = shift;
667 :     my $len = shift;
668 :     $len = 20 unless $len ;
669 :     my $answer_rule = NAMED_ANS_RULE_EXTENSION($name ,$len); # we don't want to create three answer rules in different modes.
670 :     my $out = MODES(
671 :     'TeX' => $answer_rule,
672 :     'Latex2HTML' => '\fbox{Answer boxes cannot be placed inside typeset equations}',
673 :     'HTML_tth' => '\\begin{rawhtml} '. $answer_rule.'\\end{rawhtml}',
674 :     'HTML_dpng' => '\fbox{Answer boxes cannot be placed inside typeset equations}',
675 :     'HTML' => $answer_rule
676 :     );
677 : apizer 1080
678 : sh002i 1050 $out;
679 :     }
680 :     sub ans_box {
681 :     my $row = shift;
682 :     my $col =shift;
683 :     $row = 5 unless $row;
684 :     $col = 80 unless $col;
685 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count());
686 : sh002i 1050 NAMED_ANS_BOX($name ,$row,$col);
687 :     }
688 :    
689 :     #this is legacy code; use ans_checkbox instead
690 :     sub checkbox {
691 :     my %options = @_;
692 :     qq!<INPUT TYPE="checkbox" NAME="$options{'-name'}" VALUE="$options{'-value'}">$options{'-label'}!
693 :     }
694 :    
695 :    
696 :     sub NAMED_POP_UP_LIST {
697 :     my $name = shift;
698 :     my @list = @_;
699 :     $name = RECORD_ANS_NAME($name); # record answer name
700 :     my $answer_value = '';
701 : gage 1267 $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
702 : sh002i 1050 my $out = "";
703 :     if ($displayMode eq 'HTML' or $displayMode eq 'HTML_tth' or
704 :     $displayMode eq 'HTML_dpng' or $displayMode eq 'HTML_img') {
705 :     $out = qq!<SELECT NAME = "$name" SIZE=1> \n!;
706 :     my $i;
707 :     foreach ($i=0; $i< @list; $i=$i+2) {
708 :     my $select_flag = ($list[$i] eq $answer_value) ? "SELECTED" : "";
709 :     $out .= qq!<OPTION $select_flag VALUE ="$list[$i]" > $list[$i+1] </OPTION>\n!;
710 :     };
711 :     $out .= " </SELECT>\n";
712 :     } elsif ( $displayMode eq "Latex2HTML") {
713 :     $out = qq! \\begin{rawhtml}<SELECT NAME = "$name" SIZE=1> \\end{rawhtml} \n !;
714 :     my $i;
715 :     foreach ($i=0; $i< @list; $i=$i+2) {
716 :     my $select_flag = ($list[$i] eq $answer_value) ? "SELECTED" : "";
717 :     $out .= qq!\\begin{rawhtml}<OPTION $select_flag VALUE ="$list[$i]" > $list[$i+1] </OPTION>\\end{rawhtml}\n!;
718 :     };
719 :     $out .= " \\begin{rawhtml}</SELECT>\\end{rawhtml}\n";
720 :     } elsif ( $displayMode eq "TeX") {
721 :     $out .= "\\fbox{?}";
722 :     }
723 :    
724 :     }
725 :    
726 :     sub pop_up_list {
727 :     my @list = @_;
728 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count()); # get new answer name
729 : sh002i 1050 NAMED_POP_UP_LIST($name, @list);
730 :     }
731 :    
732 : lr003k 1120
733 :    
734 :     =head5 answer_matrix
735 :    
736 :     Usage \[ \{ answer_matrix(rows,columns,width_of_ans_rule, @options) \} \]
737 : apizer 1314
738 : lr003k 1120 Creates an array of answer blanks and passes it to display_matrix which returns
739 :     text which represents the matrix in TeX format used in math display mode. Answers
740 :     are then passed back to whatever answer evaluators you write at the end of the problem.
741 :     (note, if you have an m x n matrix, you will need mn answer evaluators, and they will be
742 : apizer 1314 returned to the evaluaters starting in the top left hand corner and proceed to the left
743 : lr003k 1120 and then at the end moving down one row, just as you would read them.)
744 : apizer 1314
745 : lr003k 1120 The options are passed on to display_matrix.
746 :    
747 :    
748 :     =cut
749 :    
750 :    
751 :     sub answer_matrix{
752 :     my $m = shift;
753 :     my $n = shift;
754 :     my $width = shift;
755 :     my @options = @_;
756 :     my @array=();
757 :     for( my $i = 0; $i < $m; $i+=1)
758 :     {
759 :     my @row_array = ();
760 : apizer 1314
761 : lr003k 1120 for( my $i = 0; $i < $n; $i+=1)
762 :     {
763 :     push @row_array, ans_rule($width);
764 : apizer 1314 }
765 : lr003k 1120 my $r_row_array = \@row_array;
766 :     push @array, $r_row_array;
767 :     }
768 : gage 1251 # display_matrix hasn't been loaded into the cache safe compartment
769 :     # so we need to refer to the subroutine in this way to make
770 :     # sure that main is defined correctly.
771 :     my $ra_local_display_matrix=PG_restricted_eval(q!\&main::display_matrix!);
772 :     &$ra_local_display_matrix( \@array, @options );
773 : apizer 1314
774 : lr003k 1120 }
775 :    
776 :     sub NAMED_ANS_ARRAY_EXTENSION{
777 : apizer 1314
778 : lr003k 1120 my $name = shift;
779 :     my $col = shift;
780 :     $col = 20 unless $col;
781 :     my $answer_value = '';
782 : apizer 1314
783 : gage 1267 $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
784 : lr003k 1120 if ($answer_value =~ /\0/ ) {
785 :     my @answers = split("\0", $answer_value);
786 : apizer 1314 $answer_value = shift(@answers);
787 : lr003k 1120 $answer_value= '' unless defined($answer_value);
788 :     } elsif (ref($answer_value) eq 'ARRAY') {
789 :     my @answers = @{ $answer_value};
790 : apizer 1314 $answer_value = shift(@answers);
791 : lr003k 1120 $answer_value= '' unless defined($answer_value);
792 :     }
793 : apizer 1314
794 :     $answer_value =~ tr/\\$@`//d; ## make sure student answers can not be interpolated by e.g. EV3
795 : lr003k 1120 MODES(
796 :     TeX => "\\mbox{\\parbox[t]{10pt}{\\hrulefill}}\\hrulefill\\quad ",
797 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"\">\n\\end{rawhtml}\n!,
798 :     HTML => "<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"$answer_value\">\n"
799 :     );
800 :     }
801 :    
802 :     sub ans_array{
803 :     my $m = shift;
804 :     my $n = shift;
805 :     my $col = shift;
806 :     $col = 20 unless $col;
807 : gage 1267 my $num = inc_ans_rule_count() ;
808 : lr003k 1120 my $name = NEW_ANS_ARRAY_NAME($num,0,0);
809 :     my @options = @_;
810 :     my @array=();
811 :     my $string;
812 :     my $answer_value = "";
813 : apizer 1314
814 : lr003k 1120 $array[0][0] = NAMED_ANS_RULE($name,$col);
815 : apizer 1314
816 : lr003k 1120 for( my $i = 1; $i < $n; $i+=1)
817 :     {
818 :     $name = NEW_ANS_ARRAY_NAME_EXTENSION($num,0,$i);
819 :     $array[0][$i] = NAMED_ANS_ARRAY_EXTENSION($name,$col);
820 : apizer 1314
821 : lr003k 1120 }
822 : apizer 1314
823 : lr003k 1120 for( my $j = 1; $j < $m; $j+=1 ){
824 : apizer 1314
825 : lr003k 1120 for( my $i = 0; $i < $n; $i+=1)
826 :     {
827 :     $name = NEW_ANS_ARRAY_NAME_EXTENSION($num,$j,$i);
828 :     $array[$j][$i] = NAMED_ANS_ARRAY_EXTENSION($name,$col);
829 : apizer 1314
830 : lr003k 1120 }
831 : apizer 1314
832 : lr003k 1120 }
833 : gage 1251 my $ra_local_display_matrix=PG_restricted_eval(q!\&main::display_matrix!);
834 :     &$ra_local_display_matrix( \@array, @options );
835 : apizer 1314
836 : lr003k 1120 }
837 :    
838 :     sub ans_array_extension{
839 :     my $m = shift;
840 :     my $n = shift;
841 :     my $col = shift;
842 :     $col = 20 unless $col;
843 : gage 1267 my $num = PG_restricted_eval(q!$main::ans_rule_count!);
844 : lr003k 1120 my @options = @_;
845 :     my $name;
846 :     my @array=();
847 :     my $string;
848 :     my $answer_value = "";
849 : apizer 1314
850 : lr003k 1120 for( my $j = 0; $j < $m; $j+=1 ){
851 : apizer 1314
852 : lr003k 1120 for( my $i = 0; $i < $n; $i+=1)
853 :     {
854 :     $name = NEW_ANS_ARRAY_NAME_EXTENSION($num,$j,$i);
855 :     $array[$j][$i] = NAMED_ANS_ARRAY_EXTENSION($name,$col);
856 : apizer 1314
857 : lr003k 1120 }
858 : apizer 1314
859 : lr003k 1120 }
860 : gage 1251 my $ra_local_display_matrix=PG_restricted_eval(q!\&main::display_matrix!);
861 :     &$ra_local_display_matrix( \@array, @options );
862 : apizer 1314
863 : lr003k 1120 }
864 :    
865 :    
866 : sh002i 1050 # end answer blank macros
867 :    
868 :     =head2 Hints and solutions macros
869 :    
870 :     solution('text','text2',...);
871 :     SOLUTION('text','text2',...); # equivalent to TEXT(solution(...));
872 :    
873 :     hint('text', 'text2', ...);
874 :     HINT('text', 'text2',...); # equivalent to TEXT("$BR$HINT" . hint(@_) . "$BR") if hint(@_);
875 :    
876 :     Solution prints its concatenated input when the check box named 'ShowSol' is set and
877 :     the time is after the answer date. The check box 'ShowSol' is visible only after the
878 :     answer date or when the problem is viewed by a professor.
879 :    
880 : gage 1251 $main::envir{'displaySolutionsQ'} is set to 1 when a solution is to be displayed.
881 : sh002i 1050
882 :     Hints are shown only after the number of attempts is greater than $:showHint
883 :     ($main::showHint defaults to 1) and the check box named 'ShowHint' is set. The check box
884 :     'ShowHint' is visible only after the number of attempts is greater than $main::showHint.
885 :    
886 : gage 1251 $main::envir{'displayHintsQ'} is set to 1 when a hint is to be displayed.
887 : sh002i 1050
888 :    
889 :     =cut
890 :    
891 :    
892 :    
893 :     # solution prints its input when $displaySolutionsQ is set.
894 :     # use as TEXT(solution("blah, blah");
895 :     # \$solutionExists
896 :     # is passed to processProblem which displays a "show Solution" button
897 :     # when a solution is available for viewing
898 :    
899 :    
900 :     sub solution {
901 :     my @in = @_;
902 :     my $out = '';
903 : gage 1267 PG_restricted_eval(q!$main::solutionExists =1!);
904 :     if (PG_restricted_eval(q!$main::envir{'displaySolutionsQ'}!)) {$out = join(' ',@in);}
905 : sh002i 1050 $out;
906 :     }
907 :    
908 :    
909 :     sub SOLUTION {
910 :     TEXT( solution(@_)) ;
911 :     }
912 :    
913 :    
914 :    
915 :     sub hint {
916 :     my @in = @_;
917 :     my $out = '';
918 : apizer 1080
919 : gage 1267 PG_restricted_eval(q!$main::hintExists =1;
920 :     $main::numOfAttempts = 0 unless defined($main::numOfAttempts);
921 :     !);
922 : sh002i 1050
923 : gage 1267 if ($displayMode eq 'TeX') {
924 : sh002i 1050 $out = ''; # do nothing since hints are not available for download
925 : apizer 1314 } elsif (($envir->{'displayHintsQ'}) and
926 : gage 1267 PG_restricted_eval(q!($main::numOfAttempts >= $main::showHint)!))
927 : apizer 1080
928 : sh002i 1050 ## the second test above prevents a hint being shown if a doctored form is submitted
929 : apizer 1080
930 : sh002i 1050 {$out = join(' ',@in);} # show hint
931 : apizer 1080
932 : sh002i 1050 $out ;
933 :     }
934 :    
935 :    
936 :     sub HINT {
937 : gage 1267 TEXT("$BR" . hint(@_) . "$BR") if hint(@_);
938 : sh002i 1050 }
939 :    
940 :    
941 :    
942 :     # End hints and solutions macros
943 :     #################################
944 :    
945 :     # Produces a random number between $begin and $end with increment 1.
946 :     # You do not have to worry about integer or floating point types.
947 :    
948 :     =head2 Pseudo-random number generator
949 :    
950 :     Usage:
951 :     random(0,5,.1) # produces a random number between 0 and 5 in increments of .1
952 :     non_zero_random(0,5,.1) # gives a non-zero random number
953 :    
954 :     list_random(2,3,5,6,7,8,10) # produces random value from the list
955 :     list_random(2,3, (5..8),10) # does the same thing
956 :    
957 :     SRAND(seed) # resets the main random generator -- use very cautiously
958 :    
959 :    
960 :     SRAND(time) will create a different problem everytime it is called. This makes it difficult
961 :     to check the answers :-).
962 :    
963 : gage 1251 SRAND($envir->{'inputs_ref'}->{'key'} ) will create a different problem for each login session.
964 : sh002i 1050 This is probably what is desired.
965 :    
966 :     =cut
967 :    
968 :    
969 :     sub random {
970 :     my ($begin, $end, $incr) = @_;
971 : gage 1267 $PG_random_generator->random($begin,$end,$incr);
972 : sh002i 1050 }
973 :    
974 :    
975 :     sub non_zero_random { ##gives a non-zero random number
976 :     my (@arguments)=@_;
977 :     my $a=0;
978 :     my $i=100; #safety counter
979 :     while ($a==0 && ( 0 < $i-- ) ) {
980 :     $a=random(@arguments);
981 :     }
982 :     $a;
983 :     }
984 :    
985 :     sub list_random {
986 :     my(@li) = @_;
987 :     return $li[random(1,scalar(@li))-1];
988 :     }
989 :    
990 :     sub SRAND { # resets the main random generator -- use cautiously
991 :     my $seed = shift;
992 : gage 1328 $PG_random_generator -> srand($seed);
993 : sh002i 1050 }
994 :    
995 :     # display macros
996 :    
997 :     =head2 Display Macros
998 :    
999 :     These macros produce different output depending on the display mode being used to show
1000 :     the problem on the screen, or whether the problem is being converted to TeX to produce
1001 :     a hard copy output.
1002 :    
1003 :     MODES ( TeX => "Output this in TeX mode",
1004 :     HTML => "output this in HTML mode",
1005 :     HTML_tth => "output this in HTML_tth mode",
1006 :     HTML_dpng => "output this in HTML_dpng mode",
1007 :     Latex2HTML => "output this in Latex2HTML mode",
1008 :     )
1009 :    
1010 :     TEX (tex_version, html_version) #obsolete
1011 :    
1012 :     M3 (tex_version, latex2html_version, html_version) #obsolete
1013 :    
1014 :    
1015 :    
1016 :     =cut
1017 :    
1018 :    
1019 :     sub TEX {
1020 :     my ($tex, $html ) = @_;
1021 :     MODES(TeX => $tex, HTML => $html, HTML_tth => $html, HTML_dpng => $html);
1022 :     }
1023 :    
1024 :    
1025 :     sub M3 {
1026 :     my($tex,$l2h,$html) = @_;
1027 :     MODES(TeX => $tex, Latex2HTML => $l2h, HTML => $html, HTML_tth => $html, HTML_dpng => $html);
1028 :     }
1029 :    
1030 :     # This replaces M3. You can add new modes at will to this one.
1031 :    
1032 :     sub MODES {
1033 :     my %options = @_;
1034 :     return $options{$displayMode}
1035 :     if defined( $options{$displayMode} );
1036 :    
1037 :     # default searches.
1038 :     if ($displayMode eq "Latex2HTML") {
1039 :     return $options{TeX}
1040 :     if defined( $options{TeX} );
1041 :     return $options{HTML}
1042 :     if defined( $options{HTML} );
1043 :     die " ERROR in using MODES: 'HTML' and 'TeX' options not defined for 'Latex2HTML'";
1044 :     }
1045 :    
1046 :     if ($displayMode eq "HTML_tth") {
1047 :     return $options{HTML}
1048 :     if defined( $options{HTML} );
1049 :     die " ERROR in using MODES: 'HTML' option not defined for HTML_tth";
1050 :    
1051 :     }
1052 : apizer 1080
1053 : sh002i 1050 if ($displayMode eq "HTML_img") {
1054 :     return $options{HTML_dpng} if defined $options{HTML_dpng};
1055 :     return $options{HTML_tth} if defined $options{HTML_tth};
1056 :     return $options{HTML} if defined $options{HTML};
1057 :     die " ERROR in using MODES: 'HTML' option not defined for HTML_img";
1058 :     }
1059 : apizer 1080
1060 : sh002i 1050 if ($displayMode eq "HTML_dpng") {
1061 :     return $options{HTML_tth}
1062 :     if defined( $options{HTML_tth} );
1063 :     return $options{HTML}
1064 :     if defined( $options{HTML} );
1065 :     die " ERROR in using MODES: 'HTML' option not defined for HTML_dpng";
1066 :    
1067 :     }
1068 :    
1069 :     # trap undefined errors
1070 :     die "ERROR in defining MODES: Can't find |$displayMode| among
1071 : apizer 1080 available options:" . join(" ", keys(%options) )
1072 : sh002i 1050 . " file " . __FILE__ ." line " . __LINE__."\n\n";
1073 :    
1074 :     }
1075 :    
1076 :    
1077 :     # end display macros
1078 :    
1079 :    
1080 :     =head2 Display constants
1081 :    
1082 :     @ALPHABET ALPHABET() capital letter alphabet -- ALPHABET[0] = 'A'
1083 :     $PAR PAR() paragraph character (\par or <p>)
1084 :     $BR BR() line break character
1085 :     $LQ LQ() left double quote
1086 :     $RQ RQ() right double quote
1087 :     $BM BM() begin math
1088 :     $EM EM() end math
1089 :     $BDM BDM() begin display math
1090 :     $EDM EDM() end display math
1091 :     $LTS LTS() strictly less than
1092 :     $GTS GTS() strictly greater than
1093 :     $LTE LTE() less than or equal
1094 :     $GTE GTE() greater than or equal
1095 :     $BEGIN_ONE_COLUMN BEGIN_ONE_COLUMN() begin one-column mode
1096 :     $END_ONE_COLUMN END_ONE_COLUMN() end one-column mode
1097 :     $SOL SOLUTION_HEADING() solution headline
1098 :     $SOLUTION SOLUTION_HEADING() solution headline
1099 :     $HINT HINT_HEADING() hint headline
1100 :     $US US() underscore character
1101 :     $SPACE SPACE() space character (tex and latex only)
1102 :     $BBOLD BBOLD() begin bold typeface
1103 :     $EBOLD EBOLD() end bold typeface
1104 :     $BITALIC BITALIC() begin italic typeface
1105 :     $EITALIC EITALIC() end italic typeface
1106 :     $BCENTER BCENTER() begin centered environment
1107 : apizer 1080 $ECENTER ECENTER() end centered environment
1108 : sh002i 1050 $HR HR() horizontal rule
1109 :     $LBRACE LBRACE() left brace
1110 :     $LB LB () left brace
1111 :     $RBRACE RBRACE() right brace
1112 :     $RB RB () right brace
1113 :     $DOLLAR DOLLAR() a dollar sign
1114 :     $PERCENT PERCENT() a percent sign
1115 :     $CARET CARET() a caret sign
1116 :     $PI PI() the number pi
1117 :     $E E() the number e
1118 :    
1119 :     =cut
1120 :    
1121 :    
1122 :    
1123 :    
1124 :    
1125 :     # A utility variable. Notice that "B"=$ALPHABET[1] and
1126 :     # "ABCD"=@ALPHABET[0..3].
1127 :    
1128 :     sub ALPHABET {
1129 :     ('A'..'ZZ')[@_];
1130 :     }
1131 :    
1132 :     ###############################################################
1133 :     # Some constants which are different in tex and in HTML
1134 :     # The order of arguments is TeX, Latex2HTML, HTML
1135 : apizer 1390 # Adopted Davide Cervone's improvements to PAR, LTS, GTS, LTE, GTE, LBRACE, RBRACE, LB, RB. 7-14-03 AKP
1136 : apizer 1379 sub PAR { MODES( TeX => '\\par ', Latex2HTML => '\\begin{rawhtml}<P>\\end{rawhtml}', HTML => '<P>'); };
1137 : apizer 1390 sub BR { MODES( TeX => '\\par\\noindent ', Latex2HTML => '\\begin{rawhtml}<BR>\\end{rawhtml}', HTML => '<BR>'); };
1138 :     # Alternate definition of BR which is slightly more flexible and gives more white space in printed output
1139 :     # which looks better but kills more trees.
1140 :     #sub BR { MODES( TeX => '\\\\', Latex2HTML => '\\begin{rawhtml}<BR>\\end{rawhtml}', HTML => '<BR>'); };
1141 : sh002i 1050 sub LQ { MODES( TeX => "``", Latex2HTML => '"', HTML => '&quot;' ); };
1142 :     sub RQ { MODES( TeX => "''", Latex2HTML => '"', HTML => '&quot;' ); };
1143 :     sub BM { MODES(TeX => '\\(', Latex2HTML => '\\(', HTML => ''); }; # begin math mode
1144 :     sub EM { MODES(TeX => '\\)', Latex2HTML => '\\)', HTML => ''); }; # end math mode
1145 :     sub BDM { MODES(TeX => '\\[', Latex2HTML => '\\[', HTML => '<P ALIGN=CENTER>'); }; #begin displayMath mode
1146 :     sub EDM { MODES(TeX => '\\]', Latex2HTML => '\\]', HTML => '</P>'); }; #end displayMath mode
1147 : apizer 1379 sub LTS { MODES(TeX => '<', Latex2HTML => '\\lt ', HTML => '&lt;', HTML_tth => '<' ); };
1148 :     sub GTS { MODES(TeX => '>', Latex2HTML => '\\gt ', HTML => '&gt;', HTML_tth => '>' ); };
1149 :     sub LTE { MODES(TeX => '\\le ', Latex2HTML => '\\le ', HTML => '<U>&lt;</U>', HTML_tth => '\\le ' ); };
1150 :     sub GTE { MODES(TeX => '\\ge ', Latex2HTML => '\\ge ', HTML => '<U>&gt;</U>', HTML_tth => '\\ge ' ); };
1151 : sh002i 1050 sub BEGIN_ONE_COLUMN { MODES(TeX => " \\end{multicols}\n", Latex2HTML => " ", HTML => " "); };
1152 :     sub END_ONE_COLUMN { MODES(TeX =>
1153 :     " \\begin{multicols}{2}\n\\columnwidth=\\linewidth\n",
1154 :     Latex2HTML => ' ', HTML => ' ');
1155 :    
1156 :     };
1157 :     sub SOLUTION_HEADING { MODES( TeX => '\\par {\\bf Solution:}',
1158 :     Latex2HTML => '\\par {\\bf Solution:}',
1159 :     HTML => '<P><B>Solution:</B>');
1160 :     };
1161 :     sub HINT_HEADING { MODES( TeX => "\\par {\\bf Hint:}", Latex2HTML => "\\par {\\bf Hint:}", HTML => "<P><B>Hint:</B>"); };
1162 :     sub US { MODES(TeX => '\\_', Latex2HTML => '\\_', HTML => '_');}; # underscore, e.g. file${US}name
1163 :     sub SPACE { MODES(TeX => '\\ ', Latex2HTML => '\\ ', HTML => '&nbsp;');}; # force a space in latex, doesn't force extra space in html
1164 :     sub BBOLD { MODES(TeX => '{\\bf ', Latex2HTML => '{\\bf ', HTML => '<B>'); };
1165 :     sub EBOLD { MODES( TeX => '}', Latex2HTML => '}',HTML => '</B>'); };
1166 :     sub BITALIC { MODES(TeX => '{\\it ', Latex2HTML => '{\\it ', HTML => '<I>'); };
1167 :     sub EITALIC { MODES(TeX => '} ', Latex2HTML => '} ', HTML => '</I>'); };
1168 :     sub BCENTER { MODES(TeX => '\\begin{center} ', Latex2HTML => ' \\begin{rawhtml} <div align="center"> \\end{rawhtml} ', HTML => '<div align="center">'); };
1169 :     sub ECENTER { MODES(TeX => '\\end{center} ', Latex2HTML => ' \\begin{rawhtml} </div> \\end{rawhtml} ', HTML => '</div>'); };
1170 :     sub HR { MODES(TeX => '\\par\\hrulefill\\par ', Latex2HTML => '\\begin{rawhtml} <HR> \\end{rawhtml}', HTML => '<HR>'); };
1171 : apizer 1379 sub LBRACE { MODES( TeX => '\{', Latex2HTML => '\\lbrace', HTML => '{' , HTML_tth=> '\\lbrace' ); };
1172 :     sub RBRACE { MODES( TeX => '\}', Latex2HTML => '\\rbrace', HTML => '}' , HTML_tth=> '\\rbrace',); };
1173 :     sub LB { MODES( TeX => '\{', Latex2HTML => '\\lbrace', HTML => '{' , HTML_tth=> '\\lbrace' ); };
1174 :     sub RB { MODES( TeX => '\}', Latex2HTML => '\\rbrace', HTML => '}' , HTML_tth=> '\\rbrace',); };
1175 : sh002i 1050 sub DOLLAR { MODES( TeX => '\\$', Latex2HTML => '\\$', HTML => '$' ); };
1176 :     sub PERCENT { MODES( TeX => '\\%', Latex2HTML => '\\%', HTML => '%' ); };
1177 :     sub CARET { MODES( TeX => '\\verb+^+', Latex2HTML => '\\verb+^+', HTML => '^' ); };
1178 :     sub PI {4*atan2(1,1);};
1179 :     sub E {exp(1);};
1180 :    
1181 :     ###############################################################
1182 :     ## Evaluation macros
1183 :    
1184 :    
1185 :     =head2 TEXT macros
1186 :    
1187 :     Usage:
1188 :     TEXT(@text);
1189 :    
1190 :     This is the simplest way to print text from a problem. The strings in the array C<@text> are concatenated
1191 :     with spaces between them and printed out in the text of the problem. The text is not processed in any other way.
1192 :     C<TEXT> is defined in PG.pl.
1193 :    
1194 :     Usage:
1195 :     BEGIN_TEXT
1196 :     text.....
1197 :     END_TEXT
1198 :    
1199 :     This is the most common way to enter text into the problem. All of the text between BEGIN_TEXT and END_TEXT
1200 :     is processed by the C<EV3> macro described below and then printed using the C<TEXT> command. The two key words
1201 :     must appear on lines by themselves. The preprocessing that makes this construction work is done in F<PGtranslator.pm>.
1202 :     See C<EV3> below for details on the processing.
1203 :    
1204 :    
1205 :     =cut
1206 :    
1207 :     =head2 Evaluation macros
1208 :    
1209 :     =head3 EV3
1210 :    
1211 :     TEXT(EV3("This is a formulat \( \int_0^5 x^2 \, dx \) ");
1212 :     TEXT(EV3(@text));
1213 :    
1214 :     TEXT(EV3(<<'END_TEXT'));
1215 :     text stuff...
1216 :     END_TEXT
1217 :    
1218 :    
1219 :     The BEGIN_TEXT/END_TEXT construction is translated into the construction above by PGtranslator.pm. END_TEXT must appear
1220 :     on a line by itself and be left justified. (The << construction is known as a "here document" in UNIX and in PERL.)
1221 :    
1222 :     The single quotes around END_TEXT mean that no automatic interpolation of variables takes place in the text.
1223 :     Using EV3 with strings which have been evaluated by double quotes may lead to unexpected results.
1224 :    
1225 :    
1226 :     The evaluation macro E3 first evaluates perl code inside the braces: C<\{ code \}>.
1227 :     Any perl statment can be put inside the braces. The
1228 :     result of the evaluation (i.e. the last statement evaluated) replaces the C<\{ code \}> construction.
1229 :    
1230 :     Next interpolation of all variables (e.g. C<$var or @array> ) is performed.
1231 :    
1232 :     Then mathematical formulas in TeX are evaluated within the
1233 :     C<\( tex math mode \)> and
1234 :     C<\[ tex display math mode \] >
1235 :     constructions, in that order:
1236 :    
1237 :     =head3 FEQ
1238 :    
1239 :     FEQ($string); # processes and outputs the string
1240 :    
1241 :    
1242 :     The mathematical formulas are run through the macro C<FEQ> (Format EQuations) which performs
1243 :     several substitutions (see below).
1244 :     In C<HTML_tth> mode the resulting code is processed by tth to obtain an HTML version
1245 :     of the formula. (In the future processing by WebEQ may be added here as another option.)
1246 :     The Latex2HTML mode does nothing
1247 :     at this stage; it creates the entire problem before running it through
1248 :     TeX and creating the GIF images of the equations.
1249 :    
1250 :     The resulting string is output (and usually fed into TEXT to be printed in the problem).
1251 :    
1252 :     Usage:
1253 :    
1254 :     $string2 = FEQ($string1);
1255 :    
1256 :     This is a filter which is used to format equations by C<EV2> and C<EV3>, but can also be used on its own. It is best
1257 :     understood with an example.
1258 :    
1259 :     $string1 = "${a}x^2 + ${b}x + {$c:%.1f}"; $a = 3;, $b = -2; $c = -7.345;
1260 :    
1261 :     when interpolated becomes:
1262 :    
1263 :     $string1 = '3x^2 + -2x + {-7.345:%0.1f}
1264 :    
1265 :     FEQ first changes the number of decimal places displayed, so that the last term becomes -7.3 Then it removes the
1266 :     extraneous plus and minus signs, so that the final result is what you want:
1267 :    
1268 :     $string2 = '3x^2 - 2x -7.3';
1269 :    
1270 :     (The %0.1f construction
1271 :     is the same formatting convention used by Perl and nearly identical to the one used by the C printf statement. Some common
1272 :     usage: %0.3f 3 decimal places, fixed notation; %0.3e 3 significant figures exponential notation; %0.3g uses either fixed
1273 :     or exponential notation depending on the size of the number.)
1274 :    
1275 :     Two additional legacy formatting constructions are also supported:
1276 :    
1277 :     C<?{$c:%0.3f} > will give a number with 3 decimal places and a negative
1278 :     sign if the number is negative, no sign if the number is positive.
1279 :    
1280 :     C<!{$c:%0.3f}> determines the sign and prints it
1281 :     whether the number is positive or negative.
1282 :    
1283 :     =head3 EV2
1284 :    
1285 :     TEXT(EV2(@text));
1286 :    
1287 :     TEXT(EV2(<<END_OF_TEXT));
1288 :     text stuff...
1289 :     END_OF_TEXT
1290 :    
1291 :     This is a precursor to EV3. In this case the constants are interpolated first, before the evaluation of the \{ ...code...\}
1292 :     construct. This can lead to unexpected results. For example C<\{ join(" ", @text) \}> with C<@text = ("Hello","World");> becomes,
1293 :     after interpolation, C<\{ join(" ",Hello World) \}> which then causes an error when evaluated because Hello is a bare word.
1294 :     C<EV2> can still be useful if you allow for this, and in particular it works on double quoted strings, which lead to
1295 :     unexpected results with C<EV3>. Using single quoted strings with C<EV2> may lead to unexpected results.
1296 :    
1297 :     The unexpected results have to do with the number of times backslashed constructions have to be escaped. It is quite messy. For
1298 :     more details get a good Perl book and then read the code. :-)
1299 :    
1300 :    
1301 :    
1302 :    
1303 :     =cut
1304 :    
1305 :    
1306 :     sub ev_substring {
1307 :     my $string = shift;
1308 :     my $start_delim = shift;
1309 :     my $end_delim = shift;
1310 :     my $actionRef = shift;
1311 :     my ($eval_out,$PG_eval_errors,$PG_full_error_report)=();
1312 :     my $out = "";
1313 : apizer 1379 #
1314 :     # DPVC -- 2001/12/07
1315 :     # original "while ($string)" fails to process the string "0" correctly
1316 :     #
1317 :     while ($string ne "") {
1318 :     #
1319 :     # end DPVC
1320 :     #
1321 : sh002i 1050 if ($string =~ /\Q$start_delim\E/s) {
1322 :     #print "$start_delim $end_delim evaluating_substring=$string<BR>";
1323 :     $string =~ s/^(.*?)\Q$start_delim\E//s; # get string up to next \{ ---treats string as a single line, ignoring returns
1324 :     $out .= $1;
1325 :     #print "$start_delim $end_delim substring_out=$out<BR>";
1326 :     $string =~ s/^(.*?)\Q$end_delim\E//s; # get perl code up to \} ---treats string as a single line, ignoring returns
1327 :     #print "$start_delim $end_delim evaluate_string=$1<BR>";
1328 :     ($eval_out,$PG_eval_errors,$PG_full_error_report) = &$actionRef($1);
1329 :     $eval_out = "$start_delim $eval_out $end_delim" if $PG_full_error_report;
1330 :     $out = $out . $eval_out;
1331 :     #print "$start_delim $end_delim new substring_out=$out<BR><p><BR>";
1332 : gage 1267 $out .="$PAR ERROR $0 in ev_substring, PGbasicmacros.pl:$PAR <PRE> $@ </PRE>$PAR" if $@;
1333 : sh002i 1050 }
1334 :     else {
1335 :     $out .= $string; # flush the last part of the string
1336 :     last;
1337 :     }
1338 :    
1339 :     }
1340 :     $out;
1341 :     }
1342 :     sub safe_ev {
1343 :     my ($out,$PG_eval_errors,$PG_full_error_report) = &old_safe_ev; # process input by old_safe_ev first
1344 :     $out =~s/\\/\\\\/g; # protect any new backslashes introduced.
1345 :     ($out,$PG_eval_errors,$PG_full_error_report)
1346 :     }
1347 :    
1348 :     sub old_safe_ev {
1349 :     my $in = shift;
1350 :     my ($out,$PG_eval_errors,$PG_full_error_report) = PG_restricted_eval("$in;");
1351 :     # the addition of the ; seems to provide better error reporting
1352 :     if ($PG_eval_errors) {
1353 :     my @errorLines = split("\n",$PG_eval_errors);
1354 : gage 1267 #$out = "<PRE>$PAR % ERROR in $0:old_safe_ev, PGbasicmacros.pl: $PAR % There is an error occuring inside evaluation brackets \\{ ...code... \\} $BR % somewhere in an EV2 or EV3 or BEGIN_TEXT block. $BR % Code evaluated:$BR $in $BR % $BR % $errorLines[0]\n % $errorLines[1]$BR % $BR % $BR </PRE> ";
1355 : sh002i 1050 warn " ERROR in old_safe_ev, PGbasicmacros.pl: <PRE>
1356 :     ## There is an error occuring inside evaluation brackets \\{ ...code... \\}
1357 :     ## somewhere in an EV2 or EV3 or BEGIN_TEXT block.
1358 :     ## Code evaluated:
1359 :     ## $in
1360 :     ##" .join("\n ", @errorLines). "
1361 : gage 1267 ##</PRE>$BR
1362 : sh002i 1050 ";
1363 : gage 1267 $out ="$PAR $BBOLD $in $EBOLD $PAR";
1364 : sh002i 1050
1365 :    
1366 :     }
1367 :    
1368 :     ($out,$PG_eval_errors,$PG_full_error_report);
1369 :     }
1370 :    
1371 :     sub FEQ { # Format EQuations
1372 :     my $in = shift;
1373 :     # formatting numbers -- the ?{} and !{} constructions
1374 :     $in =~s/\?\s*\{([.\-\$\w\d]+):?([%.\da-z]*)\}/${ \( &sspf($1,$2) )}/g;
1375 :     $in =~s/\!\s*\{([.\-\$\w\d]+):?([%.\da-z]*)\}/${ \( &spf($1,$2) )}/g;
1376 :    
1377 :     # more formatting numbers -- {number:format} constructions
1378 :     $in =~ s/\{(\s*[\+\-\d\.]+[eE]*[\+\-]*\d*):(\%\d*.\d*\w)}/${ \( &spf($1,$2) )}/g;
1379 :     $in =~ s/\+\s*\-/ - /g;
1380 :     $in =~ s/\-\s*\+/ - /g;
1381 :     $in =~ s/\+\s*\+/ + /g;
1382 :     $in =~ s/\-\s*\-/ + /g;
1383 :     $in;
1384 :     }
1385 :    
1386 :     #sub math_ev3 {
1387 :     # my $in = shift; #print "in=$in<BR>";
1388 :     # my ($out,$PG_eval_errors,$PG_full_error_report);
1389 :     # $in = FEQ($in);
1390 :     # $in =~ s/%/\\%/g; # % causes trouble in TeX and HTML_tth it usually (always?) indicates an error, not comment
1391 : gage 1267 # return("$BM $in $EM") unless ($displayMode eq 'HTML_tth');
1392 : sh002i 1050 # $in = "\\(" . $in . "\\)";
1393 :     # $out = tth($in);
1394 :     # ($out,$PG_eval_errors,$PG_full_error_report);
1395 :     #
1396 :     #}
1397 :     #
1398 :     #sub display_math_ev3 {
1399 :     # my $in = shift; #print "in=$in<BR>";
1400 :     # my ($out,$PG_eval_errors,$PG_full_error_report);
1401 :     # $in = FEQ($in);
1402 :     # $in =~ s/%/\\%/g;
1403 :     # return("$main::BDM $in $main::EDM") unless $displayMode eq 'HTML_tth' ;
1404 :     # $in = "\\[" . $in . "\\]";
1405 :     # $out =tth($in);
1406 :     # ($out,$PG_eval_errors,$PG_full_error_report);
1407 :     #}
1408 :    
1409 :     sub math_ev3 {
1410 :     my $in = shift;
1411 :     return general_math_ev3($in, "inline");
1412 :     }
1413 :    
1414 :     sub display_math_ev3 {
1415 :     my $in = shift;
1416 :     return general_math_ev3($in, "display");
1417 :     }
1418 :    
1419 :     sub general_math_ev3 {
1420 :     my $in = shift;
1421 :     my $mode = shift || "inline";
1422 : apizer 1080
1423 : sh002i 1155 $in = FEQ($in); # Format EQuations
1424 :     $in =~ s/%/\\%/g; # avoid % becoming TeX comments
1425 : apizer 1314
1426 : apizer 1379 ## remove leading and trailing spaces so that HTML mode will
1427 :     ## not include unwanted spaces as per Davide Cervone.
1428 :     $in =~ s/^\s+//;
1429 :     $in =~ s/\s+$//;
1430 :    
1431 : sh002i 1155 # some modes want the delimiters, some don't
1432 :     my $in_delim = $mode eq "inline"
1433 :     ? "\\($in\\)"
1434 :     : "\\[$in\\]";
1435 : apizer 1314
1436 : sh002i 1050 my $out;
1437 : gage 1071 if($displayMode eq "HTML_tth") {
1438 :     $out = tth($in_delim);
1439 : apizer 1379 ## remove leading and trailing spaces as per Davide Cervone.
1440 :     $in =~ s/^\s+//;
1441 :     $in =~ s/\s+$//;
1442 : gage 1071 } elsif ($displayMode eq "HTML_dpng") {
1443 : sh002i 1158 # for jj's version of ImageGenerator
1444 : gage 1251 $out = $envir->{'imagegen'}->add($in_delim);
1445 : sh002i 1158 # for my version of ImageGenerator
1446 : gage 1251 #$out = $envir->{'imagegen'}->add($in, $mode);
1447 : sh002i 1050 } elsif ($displayMode eq "HTML_img") {
1448 :     $out = math2img($in, $mode);
1449 :     } else {
1450 :     $out = "\\($in\\)" if $mode eq "inline";
1451 :     $out = "\\[$in\\]" if $mode eq "display";
1452 :     }
1453 :     return $out;
1454 :     }
1455 :    
1456 :     sub EV2 {
1457 :     my $string = join(" ",@_);
1458 :     # evaluate code inside of \{ \} (no nesting allowed)
1459 :     $string = ev_substring($string,"\\{","\\}",\&old_safe_ev);
1460 :     $string = ev_substring($string,"\\<","\\>",\&old_safe_ev);
1461 :     $string = ev_substring($string,"\\(","\\)",\&math_ev3);
1462 :     $string = ev_substring($string,"\\[","\\]",\&display_math_ev3);
1463 :     # macros for displaying math
1464 : gage 1267 $string =~ s/\\\(/$BM/g;
1465 :     $string =~ s/\\\)/$EM/g;
1466 :     $string =~ s/\\\[/$BDM/g;
1467 :     $string =~ s/\\\]/$EDM/g;
1468 : sh002i 1050 $string;
1469 :     }
1470 :    
1471 :     sub EV3{
1472 :     my $string = join(" ",@_);
1473 :     # evaluate code inside of \{ \} (no nesting allowed)
1474 :     $string = ev_substring($string,"\\\\{","\\\\}",\&safe_ev); # handles \{ \} in single quoted strings of PG files
1475 :     # interpolate variables
1476 :     my ($evaluated_string,$PG_eval_errors,$PG_full_errors) = PG_restricted_eval("<<END_OF_EVALUATION_STRING\n$string\nEND_OF_EVALUATION_STRING\n");
1477 :     if ($PG_eval_errors) {
1478 :     my @errorLines = split("\n",$PG_eval_errors);
1479 :     $string =~ s/</&lt;/g; $string =~ s/>/&gt;/g;
1480 : gage 1267 $evaluated_string = "<PRE>$PAR % ERROR in $0:EV3, PGbasicmacros.pl: $PAR % There is an error occuring in the following code:$BR $string $BR % $BR % $errorLines[0]\n % $errorLines[1]$BR % $BR % $BR </PRE> ";
1481 : sh002i 1050 $@="";
1482 :     }
1483 :     $string = $evaluated_string;
1484 :     $string = ev_substring($string,"\\(","\\)",\&math_ev3);
1485 :     $string = ev_substring($string,"\\[","\\]",\&display_math_ev3);
1486 :     $string;
1487 :     }
1488 :    
1489 :     =head2 Formatting macros
1490 :    
1491 :     beginproblem() # generates text listing number and the point value of
1492 :     # the problem. It will also print the file name containing
1493 :     # the problem for users listed in the PRINT_FILE_NAMES_FOR PG_environment
1494 :     # variable.
1495 :     OL(@array) # formats the array as an Ordered List ( <OL> </OL> ) enumerated by letters.
1496 :    
1497 :     htmlLink($url, $text)
1498 :     # Places a reference to the URL with the specified text in the problem.
1499 :     # A common usage is \{ htmlLink(alias('prob1_help.html') \}, 'for help')
1500 :     # where alias finds the full address of the prob1_help.html file in the same directory
1501 :     # as the problem file
1502 :     appletLink($url, $parameters)
1503 :     # For example
1504 :     # appletLink(q! archive="http: //webwork.math.rochester.edu/gage/xFunctions/xFunctions.zip"
1505 :     code="xFunctionsLauncher.class" width=100 height=14!,
1506 :     " parameter text goes here")
1507 :     # will link to xFunctions.
1508 :    
1509 :     low level:
1510 :    
1511 :     spf($number, $format) # prints the number with the given format
1512 :     sspf($number, $format) # prints the number with the given format, always including a sign.
1513 :     protect_underbar($string) # protects the underbar (class_name) in strings which may have to pass through TeX.
1514 :    
1515 :     =cut
1516 :    
1517 :     sub beginproblem {
1518 :     my $out = "";
1519 : gage 1267 my $problemValue = $envir->{problemValue};
1520 : gage 1298 my $fileName = $envir->{fileName};
1521 : gage 1267 my $probNum = $envir->{probNum};
1522 :     my $TeXFileName = protect_underbar($envir->{fileName});
1523 :     my $l2hFileName = protect_underbar($envir->{fileName});
1524 : sh002i 1050 my %inlist;
1525 :     my $points ='pts';
1526 : apizer 1314
1527 : gage 1267 $points = 'pt' if $problemValue == 1;
1528 : sh002i 1050 ## Prepare header for the problem
1529 : gage 1251 grep($inlist{$_}++,@{ $envir->{'PRINT_FILE_NAMES_FOR'} });
1530 : gage 1267 if ( defined($inlist{$envir->{studentLogin}}) and ($inlist{$envir->{studentLogin}} > 0) ) {
1531 :     $out = &M3("\n\n\\medskip\\hrule\\smallskip\\par{\\bf ${probNum}.{\\footnotesize ($problemValue $points) $TeXFileName}}\\newline ",
1532 :     " \\begin{rawhtml} ($problemValue $points) <B>$l2hFileName</B><BR>\\end{rawhtml}",
1533 :     "($problemValue $points) <B>$fileName</B><BR>"
1534 : sh002i 1050 );
1535 :     } else {
1536 : gage 1267 $out = &M3("\n\n\\smallskip\\hrule\\smallskip\\par{\\bf ${probNum}.}($problemValue $points) ",
1537 :     "($problemValue $points) ",
1538 :     "($problemValue $points) "
1539 : sh002i 1050 );
1540 :     }
1541 :     $out;
1542 :    
1543 :     }
1544 :    
1545 :     # kludge to clean up path names
1546 :     ## allow underscore character in set and section names and also allows line breaks at /
1547 :     sub protect_underbar {
1548 :     my $in = shift;
1549 :     if ($displayMode eq 'TeX') {
1550 :    
1551 :     $in =~ s|_|\\\_|g;
1552 :     $in =~ s|/|\\\-/|g; # allows an optional hyphenation of the path (in tex)
1553 :     }
1554 :     $in;
1555 :     }
1556 :    
1557 :    
1558 :     # An example of a macro which prints out a list (with letters)
1559 :     sub OL {
1560 :     my(@array) = @_;
1561 :     my $i = 0;
1562 :     my $out= &M3(
1563 :     "\\begin{enumerate}\n",
1564 :     " \\begin{rawhtml} <OL TYPE=\"A\" VALUE=\"1\"> \\end{rawhtml} ",
1565 :     "<OL TYPE=\"A\" VALUE=\"1\">\n"
1566 :     ) ;
1567 :     my $elem;
1568 :     foreach $elem (@array) {
1569 :     $out .= MODES(
1570 : gage 1267 TeX=> "\\item[$ALPHABET[$i].] $elem\n",
1571 : sh002i 1050 Latex2HTML=> " \\begin{rawhtml} <LI> \\end{rawhtml} $elem ",
1572 :     HTML=> "<LI> $elem\n",
1573 :     HTML_dpng=> "<LI> $elem <br /> <br /> \n"
1574 :     );
1575 :     $i++;
1576 :     }
1577 :     $out .= &M3(
1578 :     "\\end{enumerate}\n",
1579 :     " \\begin{rawhtml} </OL>\n \\end{rawhtml} ",
1580 :     "</OL>\n"
1581 :     ) ;
1582 :     }
1583 :    
1584 :     sub htmlLink {
1585 :     my $url = shift;
1586 :     my $text = shift;
1587 :     my $options = shift;
1588 :     $options = "" unless defined($options);
1589 : gage 1267 return "$BBOLD\[ broken link: $text \] $EBOLD" unless defined($url);
1590 : sh002i 1050 M3( "{\\bf \\underline{$text} }",
1591 :     "\\begin{rawhtml} <A HREF=\"$url\" $options> $text </A>\\end{rawhtml}",
1592 :     "<A HREF=\"$url\" $options> $text </A>"
1593 :     );
1594 :     }
1595 : gage 1267
1596 : sh002i 1050 sub appletLink {
1597 :     my $url = shift;
1598 :     my $options = shift;
1599 :     $options = "" unless defined($options);
1600 :     M3( "{\\bf \\underline{APPLET} }",
1601 :     "\\begin{rawhtml} <APPLET $url> $options </APPLET>\\end{rawhtml}",
1602 :     "<APPLET $url> $options </APPLET>"
1603 :     );
1604 :     }
1605 :     sub spf {
1606 :     my($number,$format) = @_; # attention, the order of format and number are reversed
1607 :     $format = "%4.3g" unless $format; # default value for format
1608 :     sprintf($format, $number);
1609 :     }
1610 :     sub sspf {
1611 :     my($number,$format) = @_; # attention, the order of format and number are reversed
1612 :     $format = "%4.3g" unless $format; # default value for format
1613 :     my $sign = $number>=0 ? " + " : " - ";
1614 :     $number = $number>=0 ? $number : -$number;
1615 :     $sign .sprintf($format, $number);
1616 :     }
1617 :    
1618 :     =head2 Sorting and other list macros
1619 :    
1620 :    
1621 :    
1622 :     Usage:
1623 :     lex_sort(@list); # outputs list in lexigraphic (alphabetical) order
1624 :     num_sort(@list); # outputs list in numerical order
1625 :     uniq( @list); # outputs a list with no duplicates. Order is unspecified.
1626 :    
1627 :     PGsort( \&sort_subroutine, @list);
1628 :     # &sort_subroutine defines order. It's output must be -1,0 or 1.
1629 :    
1630 :     =cut
1631 :    
1632 :     # uniq gives unique elements of a list:
1633 :     sub uniq {
1634 :     my (@in) =@_;
1635 :     my %temp = ();
1636 :     while (@in) {
1637 :     $temp{shift(@in)}++;
1638 :     }
1639 :     my @out = keys %temp; # sort is causing trouble with Safe.??
1640 :     @out;
1641 :     }
1642 :    
1643 :     sub lex_sort {
1644 : apizer 1080 PGsort sub {$_[0] cmp $_[1]}, @_;
1645 : sh002i 1050 }
1646 :     sub num_sort {
1647 :     PGsort sub {$_[0] <=> $_[1]}, @_;
1648 :     }
1649 :    
1650 :    
1651 :     =head2 Macros for handling tables
1652 :    
1653 :     Usage:
1654 :     begintable( number_of_columns_in_table)
1655 :     row(@dataelements)
1656 :     endtable()
1657 :    
1658 :     Example of useage:
1659 :    
1660 :     BEGIN_TEXT
1661 :     This problem tests calculating new functions from old ones:$BR
1662 :     From the table below calculate the quantities asked for:$BR
1663 :     \{begintable(scalar(@firstrow)+1)\}
1664 :     \{row(" \(x\) ",@firstrow)\}
1665 :     \{row(" \(f(x)\) ", @secondrow)\}
1666 :     \{row(" \(g(x)\) ", @thirdrow)\}
1667 :     \{row(" \(f'(x)\) ", @fourthrow)\}
1668 :     \{row(" \(g'(x)\) ", @fifthrow)\}
1669 :     \{endtable()\}
1670 :    
1671 :     (The arrays contain numbers which are placed in the table.)
1672 :    
1673 :     END_TEXT
1674 :    
1675 :     =cut
1676 :    
1677 :     sub begintable {
1678 :     my ($number)=shift; #number of columns in table
1679 :     my %options = @_;
1680 :     warn "begintable(cols) requires a number indicating the number of columns" unless defined($number);
1681 :     my $out = "";
1682 :     if ($displayMode eq 'TeX') {
1683 :     $out .= "\n\\par\\smallskip\\begin{center}\\begin{tabular}{" . "|c" x $number . "|} \\hline\n";
1684 :     }
1685 :     elsif ($displayMode eq 'Latex2HTML') {
1686 :     $out .= "\n\\begin{rawhtml} <TABLE , BORDER=1>\n\\end{rawhtml}";
1687 :     }
1688 :     elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng' || $displayMode eq 'HTML_img') {
1689 :     $out .= "<TABLE BORDER=1>\n"
1690 :     }
1691 : apizer 1080 else {
1692 : apizer 1379 $out = "Error: PGbasicmacros: begintable: Unknown displayMode: $displayMode.\n";
1693 : sh002i 1050 }
1694 :     $out;
1695 :     }
1696 :    
1697 :     sub endtable {
1698 :     my $out = "";
1699 :     if ($displayMode eq 'TeX') {
1700 :     $out .= "\n\\end {tabular}\\end{center}\\par\\smallskip\n";
1701 :     }
1702 :     elsif ($displayMode eq 'Latex2HTML') {
1703 :     $out .= "\n\\begin{rawhtml} </TABLE >\n\\end{rawhtml}";
1704 :     }
1705 :     elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng' ||$displayMode eq 'HTML_img') {
1706 :     $out .= "</TABLE>\n";
1707 :     }
1708 :     else {
1709 : apizer 1379 $out = "Error: PGbasicmacros: endtable: Unknown displayMode: $displayMode.\n";
1710 : sh002i 1050 }
1711 :     $out;
1712 :     }
1713 :    
1714 :    
1715 :     sub row {
1716 :     my @elements = @_;
1717 :     my $out = "";
1718 :     if ($displayMode eq 'TeX') {
1719 :     while (@elements) {
1720 :     $out .= shift(@elements) . " &";
1721 :     }
1722 :     chop($out); # remove last &
1723 :     $out .= "\\\\ \\hline \n";
1724 :     # carriage returns must be added manually for tex
1725 :     }
1726 :     elsif ($displayMode eq 'Latex2HTML') {
1727 :     $out .= "\n\\begin{rawhtml}\n<TR>\n\\end{rawhtml}\n";
1728 :     while (@elements) {
1729 :     $out .= " \n\\begin{rawhtml}\n<TD> \n\\end{rawhtml}\n" . shift(@elements) . " \n\\begin{rawhtml}\n</TD> \n\\end{rawhtml}\n";
1730 :     }
1731 :     $out .= " \n\\begin{rawhtml}\n</TR> \n\\end{rawhtml}\n";
1732 :     }
1733 : gage 1267 elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng'||$displayMode eq 'HTML_img') {
1734 : sh002i 1050 $out .= "<TR>\n";
1735 :     while (@elements) {
1736 :     $out .= "<TD>" . shift(@elements) . "</TD>";
1737 :     }
1738 :     $out .= "\n</TR>\n";
1739 :     }
1740 :     else {
1741 : apizer 1379 $out = "Error: PGbasicmacros: row: Unknown displayMode: $displayMode.\n";
1742 : sh002i 1050 }
1743 :     $out;
1744 :     }
1745 :    
1746 :     =head2 Macros for displaying static images
1747 :    
1748 :     Usage:
1749 :     $string = image($image, width => 100, height => 100, tex_size => 800)
1750 :     $string = image([$image1, $image2], width => 100, height => 100, tex_size => 800)
1751 :     $string = caption($string);
1752 :     $string = imageRow([$image1, $image2 ], [$caption1, $caption2]);
1753 :     # produces a complete table with rows of pictures.
1754 :    
1755 :    
1756 :     =cut
1757 :    
1758 :     # More advanced macros
1759 :     sub image {
1760 :     my $image_ref = shift;
1761 :     my @opt = @_;
1762 :     unless (scalar(@opt) % 2 == 0 ) {
1763 :     warn "ERROR in image macro. A list of macros must be inclosed in square brackets.";
1764 :     }
1765 :     my %in_options = @opt;
1766 :     my %known_options = (
1767 :     width => 100,
1768 :     height => 100,
1769 :     tex_size => 800,
1770 :     );
1771 :     # handle options
1772 :     my %out_options = %known_options;
1773 :     foreach my $opt_name (keys %in_options) {
1774 :     if ( exists( $known_options{$opt_name} ) ) {
1775 :     $out_options{$opt_name} = $in_options{$opt_name} if exists( $in_options{$opt_name} ) ;
1776 :     } else {
1777 :     die "Option $opt_name not defined for image. " .
1778 :     "Default options are:<BR> ", display_options2(%known_options);
1779 :     }
1780 :     }
1781 :     my $width = $out_options{width};
1782 :     my $height = $out_options{height};
1783 :     my $tex_size = $out_options{tex_size};
1784 :     my $width_ratio = $tex_size*(.001);
1785 :     my @image_list = ();
1786 :    
1787 :     if (ref($image_ref) =~ /ARRAY/ ) {
1788 :     @image_list = @{$image_ref};
1789 :     } else {
1790 :     push(@image_list,$image_ref);
1791 :     }
1792 : apizer 1080
1793 : gage 1071 my @output_list = ();
1794 : sh002i 1050 while(@image_list) {
1795 :     my $imageURL = alias(shift @image_list);
1796 :     my $out="";
1797 : apizer 1080
1798 : gage 1267 if ($displayMode eq 'TeX') {
1799 : sh002i 1050 my $imagePath = $imageURL; # in TeX mode, alias gives us a path, not a URL
1800 : gage 1251 if ($envir->{texDisposition} eq "pdf") {
1801 : sh002i 1050 # We're going to create PDF files with our TeX (using pdflatex), so
1802 :     # alias should have given us the path to a PNG image. What we need
1803 :     # to do is find out the dimmensions of this image, since pdflatex
1804 :     # is too dumb to live.
1805 : apizer 1080
1806 : sh002i 1050 #my ($height, $width) = getImageDimmensions($imagePath);
1807 :     ##warn "&image: $imagePath $height $width\n";
1808 :     #unless ($height and $width) {
1809 :     # warn "Couldn't get the dimmensions of image $imagePath.\n"
1810 :     #}
1811 :     #$out = "\\includegraphics[bb=0 0 $height $width,width=$width_ratio\\linewidth]{$imagePath}\n";
1812 :     $out = "\\includegraphics[width=$width_ratio\\linewidth]{$imagePath}\n";
1813 :     } else {
1814 :     # Since we're not creating PDF files, alias should have given us the
1815 :     # path to an EPS file. latex can get its dimmensions no problem!
1816 : apizer 1080
1817 : sh002i 1050 $out = "\\includegraphics[width=$width_ratio\\linewidth]{$imagePath}\n";
1818 :     }
1819 : gage 1267 } elsif ($displayMode eq 'Latex2HTML') {
1820 : sh002i 1050 $out = qq!\\begin{rawhtml}\n<A HREF= "$imageURL" TARGET="ZOOM"><IMG SRC="$imageURL" WIDTH="$width" HEIGHT="$height"></A>\n
1821 :     \\end{rawhtml}\n !
1822 : gage 1267 } elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng' || $displayMode eq 'HTML_img') {
1823 : sh002i 1050 $out = qq!<A HREF= "$imageURL" TARGET="ZOOM"><IMG SRC="$imageURL" WIDTH="$width" HEIGHT="$height"></A>
1824 :     !
1825 :     } else {
1826 : apizer 1379 $out = "Error: PGbasicmacros: image: Unknown displayMode: $displayMode.\n";
1827 : sh002i 1050 }
1828 :     push(@output_list, $out);
1829 :     }
1830 :     return wantarray ? @output_list : $output_list[0];
1831 :     }
1832 :    
1833 :     # This is legacy code.
1834 :     sub images {
1835 :     my @in = @_;
1836 :     my @outlist = ();
1837 :     while (@in) {
1838 :     push(@outlist,&image( shift(@in) ) );
1839 :     }
1840 :     @outlist;
1841 :     }
1842 :    
1843 :    
1844 :     sub caption {
1845 :     my ($out) = @_;
1846 : gage 1267 $out = " $out \n" if $displayMode eq 'TeX';
1847 :     $out = " $out " if $displayMode eq 'HTML';
1848 :     $out = " $out " if $displayMode eq 'HTML_tth';
1849 :     $out = " $out " if $displayMode eq 'HTML_dpng';
1850 :     $out = " $out " if $displayMode eq 'HTML_img';
1851 :     $out = " $out " if $displayMode eq 'Latex2HTML';
1852 : sh002i 1050 $out;
1853 :     }
1854 :    
1855 :     sub captions {
1856 :     my @in = @_;
1857 :     my @outlist = ();
1858 :     while (@in) {
1859 :     push(@outlist,&caption( shift(@in) ) );
1860 :     }
1861 :     @outlist;
1862 :     }
1863 :    
1864 :     sub imageRow {
1865 :    
1866 :     my $pImages = shift;
1867 :     my $pCaptions=shift;
1868 :     my $out = "";
1869 :     my @images = @$pImages;
1870 :     my @captions = @$pCaptions;
1871 :     my $number = @images;
1872 :     # standard options
1873 :     my %options = ( 'tex_size' => 200, # width for fitting 4 across
1874 :     'height' => 100,
1875 :     'width' => 100,
1876 :     @_ # overwrite any default options
1877 :     );
1878 :    
1879 : gage 1267 if ($displayMode eq 'TeX') {
1880 : sh002i 1050 $out .= "\n\\par\\smallskip\\begin{center}\\begin{tabular}{" . "|c" x $number . "|} \\hline\n";
1881 :     while (@images) {
1882 :     $out .= &image( shift(@images),%options ) . '&';
1883 :     }
1884 :     chop($out);
1885 :     $out .= "\\\\ \\hline \n";
1886 :     while (@captions) {
1887 :     $out .= &caption( shift(@captions) ) . '&';
1888 :     }
1889 :     chop($out);
1890 :     $out .= "\\\\ \\hline \n\\end {tabular}\\end{center}\\par\\smallskip\n";
1891 : gage 1267 } elsif ($displayMode eq 'Latex2HTML'){
1892 : sh002i 1050
1893 :     $out .= "\n\\begin{rawhtml} <TABLE BORDER=1><TR>\n\\end{rawhtml}\n";
1894 :     while (@images) {
1895 :     $out .= "\n\\begin{rawhtml} <TD>\n\\end{rawhtml}\n" . &image( shift(@images),%options )
1896 :     . "\n\\begin{rawhtml} </TD>\n\\end{rawhtml}\n" ;
1897 :     }
1898 :    
1899 :     $out .= "\n\\begin{rawhtml}</TR><TR>\\end{rawhtml}\n";
1900 :     while (@captions) {
1901 :     $out .= "\n\\begin{rawhtml} <TH>\n\\end{rawhtml}\n".&caption( shift(@captions) )
1902 :     . "\n\\begin{rawhtml} </TH>\n\\end{rawhtml}\n" ;
1903 :     }
1904 :    
1905 :     $out .= "\n\\begin{rawhtml} </TR> </TABLE >\n\\end{rawhtml}";
1906 : gage 1267 } elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng'|| $displayMode eq 'HTML_img'){
1907 : sh002i 1050 $out .= "<P>\n <TABLE BORDER=2 CELLPADDING=3 CELLSPACING=2 ><TR ALIGN=CENTER VALIGN=MIDDLE>\n";
1908 :     while (@images) {
1909 :     $out .= " \n<TD>". &image( shift(@images),%options ) ."</TD>";
1910 :     }
1911 :     $out .= "</TR>\n<TR>";
1912 :     while (@captions) {
1913 :     $out .= " <TH>". &caption( shift(@captions) ) ."</TH>";
1914 :     }
1915 :     $out .= "\n</TR></TABLE></P>\n"
1916 :     }
1917 :     else {
1918 : apizer 1379 $out = "Error: PGbasicmacros: imageRow: Unknown languageMode: $displayMode.\n";
1919 : sh002i 1050 warn $out;
1920 :     }
1921 :     $out;
1922 :     }
1923 :    
1924 :    
1925 :     ###########
1926 :     # Auxiliary macros
1927 :    
1928 :     sub display_options2{
1929 :     my %options = @_;
1930 :     my $out_string = "";
1931 :     foreach my $key (keys %options) {
1932 :     $out_string .= " $key => $options{$key},<BR>";
1933 :     }
1934 :     $out_string;
1935 :     }
1936 :    
1937 :    
1938 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9