## WeBWorK Problems

### How use formula as alternative in multiple-choice

by Murray Eisenberg -
Number of replies: 3
I'm trying to write my first multiple-choice question. Each alternative is going to consist of text that include a math expression (an equation or inequality).
I'm trying:
Context("Numeric");
# INITIALIZATION
$num = random(1,5,1);$alt1 = Formula("E_i = h^$num");$alt2 = Formula("|E_i| = h^$num");$alt3 = Formula("|E_i| \leq K h^$num for some constant $$K$$");$radio = new_multiple_choice();
$radio->qa("This means that", "$alt1");
$radio->extra($alt2);
$radio->makeLast($alt3);

# QUESTION
TEXT(beginproblem());
Context()->texStrings;
BEGIN_TEXT
$BR \{$radio->print_q() \} \{ $radio->print_a() \}  Etc. But clearly this is not working. How do I form the alternatives so that they can combine TeX markup along with a snippet that's a formula? I'm blind as to how to combine the various forms. In reply to Murray Eisenberg ### Re: How use formula as alternative in multiple-choice by Murray Eisenberg - I guess my question boils down to the following: How do I create a string (which might include some TeX expressions) that also includes the VALUE of some numeric variable. In reply to Murray Eisenberg ### Re: How use formula as alternative in multiple-choice by Murray Eisenberg - OK, I think I figured out how to do this, using Perl's substitution operator s. What I use are definitions like the following: $num = random(1,5,1);
$alt1 = "$$E_i = h^num$$";$alt1 =~ s/num/$num/;  etc. Then I just use$alt1, etc. in the arguments to qa and extra in:
$radio = new_multiple_choice();$radio -> qa("some text",$alt1);$radio -> extra($alt2,$alt3, $alt4);  etc. Can I do the creation of$alt1 and the substitution all in a single statement? (Obviously, I'm a newbie to Perl as well as to WeBWorK.)
In reply to Murray Eisenberg

### Re: How use formula as alternative in multiple-choice

by Michael Gage -
If I have understood your question correctly, you want to use perl's interpolation feature to create $alt1 in one step: $num = random(1,5,1);
\$alt1 = "$$E_i = h^num$$";



Any dollar sign variable inside double quotes in perl is replaced by its value. Single quotes prevent variable interpolation. For a very quick start on perl see

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

particularly the section on quotes.

For more details see the links on that page and for a book I highly recommend "Learning Perl" by Randal Schwartz and Tom Phoenix.