Well, I took one of your sample Graph Theory problems (IsConnected.pg ) and modified it to show you some ways to handle this. Since the image() command is in PGnauGraphics.pl , you can't change the size of the TeX image from within your .pg file using the NAU macros, so I give you an example that uses image_match_list and one that calls Image()
directly. These call on the Union College macros, so you will need to
have those files in your macros path. Since you can now add directories
to the search path for macro files, you can have your system locate
these files without having to add them to pg/macros or your course macros directory.
The first solution is the following: DOCUMENT();
loadMacros( "PGstandard.pl", "PGnauGraphtheory.pl", "PGunion.pl", "imageChoice.pl", );
TEXT(beginproblem());
@y=("Which graphs of the graphs are connected?"); @n=(); @yn=();
foreach $i (0..3) { $size = random(5,8,1); # number of vertices $offset = random(0, 25 - $size, 1); $graph = GRgraph_size_random($size,0.43); $labels = join('',@ALPHABET[$offset..($offset+$size-1)]); $pic[$i] = GRpic_graph_labels($graph,$labels); $name = "Graph ".$ALPHABET[$i]; $c = GRncomponents_graph($graph); if ($c == 1) {push @y,$name} else {push @n,$name} push @yn,$name; }
$cmc = new_checkbox_multiple_choice(); $cmc->{ImageOptions} = [ border => 1, # size of border around images columns => 2, # number of images per row separation => 30, # space between columns vseparation => 10, # space between rows size => $pic[0]->size, # size of pictures tex_size => 400, # size for images in TeX file ];
$cmc->qa(@y); $cmc->extra(@n); $cmc->makeLast(@yn);
BEGIN_TEXT \{img_print_a($cmc,@pic)\} $PAR \{$cmc->print_q\}$BR \{$cmc->print_a\} END_TEXT
ANS(checkbox_cmp($cmc->correct_ans)); $showPartialCorrectAnswers = 1;
ENDDOCUMENT();
Here, I have streamlined your creation of the data for the match list a little. I use @ALPHABET ,
since it is already predefined and there is no need to recreate it. I
also use letters for labeling the graphs, since that's how image_match_list will mark them. Then I add ImageOptions to the $cmc object to control img_print_a . Since img_print_a is really meant to work with a different type of list, I call it by hand rather than use it as a replacement for print_q , which would have provided it with the wrong parameters. Finally, I print the checkboxes and install the answer checker.
The array of images is shown as a 2x2 grid, which will be shown that
way also in the TeX file. The size of the images is looked up from one
of the graphs, and the TeX size is set appropriately for a 2x2 array in
the ImageOptions field of $cmc .
(The TeX size is given as a percent of the column width times 10, so
400 represents 40% of the column width, which lets two images fit
nicely across the column.)
Personally, I think that having the checkboxes right under the graphs
would be a better idea (rather than as a list at the bottom of the
page), so I offer this alternative approach were we create the table by
hand and set up the checkboxes ourselves rather than through one of the
canned list routines. DOCUMENT();
loadMacros( "PGstandard.pl", "PGnauGraphtheory.pl", "PGunion.pl", );
TEXT(beginproblem());
$ans = ''; foreach $i (0..3) { $size = random(5,8,1); # number of vertices $offset = random(0, 25 - $size, 1); $graph = GRgraph_size_random($size,0.43); $labels = join('',@ALPHABET[$offset..$offset+$size-1]); $pic[$i] = GRpic_graph_labels($graph,$labels); $pic[$i] = Image($pic[$i],size=>$pic[$i]->size,tex_size=>400,border=>0); $letter = $ALPHABET[$i]; $name = "Graph $letter"; $rule[$i] = ($i? NAMED_ANS_CHECKBOX_OPTION('checks',$letter,$name): NAMED_ANS_CHECKBOX('checks',$letter,$name)); # TeX-mode uses \item, but we're not in a list, so remove it # and put a fake box in front $rule[$i] =~ s/\item/\(\bigcirc\)\ / if $displayMode eq 'TeX'; $ans .= $letter if GRncomponents_graph($graph) == 1; }
BEGIN_TEXT
$BCENTER Select the graphs below that are ${BBOLD}connected${EBOLD}: $ECENTER $PAR
\{ BeginTable(spacing=>5). AlignedRow([$pic[0], $pic[1]]). AlignedRow([$rule[0],$rule[1]]). TableSpace(16,8). AlignedRow([$pic[2], $pic[3]]). AlignedRow([$rule[2],$rule[3]]). EndTable() \} END_TEXT
NAMED_ANS(checks => checkbox_cmp($ans)); $showPartialCorrectAnswers = 1;
ENDDOCUMENT();
Here, in the loop, we create a graph and then call Image() to get the proper HTML (or TeX) for it right away. Since the pictures already include a border, we set that to 0 in the Image() call.
Next, we create the checkbox. The first one (when $i == 0
is the one that gets tied to the answer checker, so we call
NAMED_ANS_CHECKBOX for that, and all others get
NAMED_ANS_CHECKBOX_OPTION with the same name so they get tied to the
same answer checker. (We take advantage of the fact that checkbox_cmp
already knows how to collect the extra answers together and form the
combined result that becomes the student's answer.) The one tricky part
is that, in TeX mode, these calls produce \item calls,
which we don't want, as these check boxes will not be an in an
enumerated list of any kind. So we do a substitution to replace the \item
by some TeX code to produce a big circle (to represent the check box)
instead. Note that we take advantage of the fact that WW automatically
doubles all backslashes in the .pg file, so in this substitution, the backslashes are already escaped. (I.e., the REAL substitution is s/\\item/\\(\\bigcirc\\)\\ / , so don't be fooled.)
At the end of the loop, we add the graph's label to the correct answer if it is one of the ones supposed to be checked.
Finally, we produce a table that has pictures with the associated
checkboes below in a 2x2 array. We add a little space between the rows
(16 pixels in HTML mode and 8 points in TeX mode), and we're done.
Hope one of these turns out the be useful to you.
Davide
<| Post or View Comments |> |