PREP 2014 Question Authoring - Archived

How are mathematical expressions displayed with radio button options?

How are mathematical expressions displayed with radio button options?

by Mary Cameron -
Number of replies: 4

When following the documentation for radio buttons from http://webwork.maa.org/wiki/MultipleChoiceRadio1if I replace “Green” with "\( \displaystyle \frac {$a x} {$b y}   \)   " what gets displayed after that radio button is:  [math] ax/by “>ax/by.   How do you get simply the fraction     ax/by?

Below is the code.  Thanks, Mary


DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"parserRadioButtons.pl",
);

TEXT(beginproblem());

############################ Setup
Context("Numeric");
Context() -> variables -> add( y => "Real");
$ar = 1;
while ( $ar == 1) {
   $a = random(2,10,1);
   $b = random(11,20, 1);
   ($ar, $br) = reduce($a, $b);
}

$radio = RadioButtons(
  ["Red","Blue","\( \displaystyle \frac {$ar x} {$br y} \)","None of these"],
  "Blue", # correct answer
  last => ["None of these"], # can be a list
);

############################ Text
Context()->texStrings;
BEGIN_TEXT
My favorite color is \( \displaystyle \frac {$ar x} {$br y} \)$BR  $BR
\{ $radio->buttons() \}
END_TEXT
Context()->normalStrings;
############################ Answers
install_problem_grader(~~&std_problem_grader);
$showPartialCorrectAnswers = 0;
ANS( $radio->cmp() );

ENDDOCUMENT();

In reply to Mary Cameron

Re: How are mathematical expressions displayed with radio button options?

by Paul Pearson -
Hi Mary,

The parserRadioButtons.pl macro is a MathObjects macro for making multiple choice questions.  There are macros that predate this one and can also generate multiple choice radio buttons questions.  For instance, see the first example at:  

http://webwork.maa.org/wiki/MultipleChoiceProblems 

I have included a pre-MathObjects example below my signature.

Best regards,

Paul Pearson

########### begin PG code

DOCUMENT();  

loadMacros(
"PGstandard.pl",
"PGchoicemacros.pl",
);


$ar = 1;
while ( $ar == 1) {
   $a = random(2,10,1);
   $b = random(11,20, 1);
   ($ar, $br) = reduce($a, $b);
}

$mc = new_multiple_choice();
$mc->qa(
"Select \( \displaystyle \frac {$ar x} {$br y} \)", 
"\( \displaystyle \frac{$ar x}{$br y} \)"
);
$mc->extra(
"red",
"green",
);
$mc->makeLast("none of the above");

BEGIN_TEXT
\{ $mc->print_q() \}
$BR
\{ $mc->print_a() \}
END_TEXT

$showPartialCorrectAnswers = 0;

ANS( radio_cmp( $mc->correct_ans() ) );

ENDDOCUMENT();
In reply to Paul Pearson

Re: How are mathematical expressions displayed with radio button options?

by Paul Pearson -
Hi Mary,

Thanks for pointing this out.  After some careful inspection, I could not see anything wrong with the PG code that you wrote.  (You could have avoided adding the variable y to the context since you only used variable names inside of strings.)

To this post, I have attached a png picture of what I see when I run your code using "images" display mode.

Best regards,

Paul Pearson
Attachment Images_BugfixRadio1.png
In reply to Paul Pearson

Re: How are mathematical expressions displayed with radio button options?

by Paul Pearson -
Hi Mary,

I had to make a second Moodle post to attach a picture of the question in "MathJax" display mode.  Why has the randomization changed just by changing the display mode from "images" to "MathJax" (nothing else changed)?  Something is fishy...

Best regards,

Paul Pearson
Attachment MathJax_BugfixRadio1.png
In reply to Paul Pearson

Re: How are mathematical expressions displayed with radio button options?

by Davide Cervone -
The RadioButton object doesn't supper math in its entries. The reason is that the button text is used as the value of the button (in the HTML that is produced) and math would mess that up. In addition, the object has to produce both a text version and a TeX version for the result table, and that is much more complicated if you can mix math and text.

The reason you are seeing the results that you are is that commands are performed before math is processed within BEGIN_TEXT/END_TEXT, so after the RadioButton menu is inserted into the problem, with something like

  <input type="radio button" value="\(\frac{2x}{3x}\)"> \(\frac{2x}{3x}
both copies of the math are processed, and that inserted HTML into the value attribute, which is illegal, and it turns out that it ends the <input> element early, leaving part of the rest of the HTML as plain text rather than tags. This is why you see things like "> or [math] with a second copy of the math.

I will need to see what I can do with the RadioButton object to improve the situation, but haven't had the chance to work on it yet.