WeBWorK Main Forum

Adding { and } into Answer Preview section

Adding { and } into Answer Preview section

by Carl Yao -
Number of replies: 11
Hello:

I'm asking students to enter set notation, like {x|x>1}. However, the Answer Preview section does not show special characters like { and }. Of course students don't know they need to enter \{x|x>1\}.

I added in custom checker to add in backslashes, but the checker won't affect the Answer Preview section.

Anyone has dealt with this issue before? Thank you for your help!

Carl Yao
Portland Community College
In reply to Carl Yao

Re: Adding { and } into Answer Preview section

by Carl Yao -
# WeBWorK problem written by Carl Yao
# Portland Community College
# ENDDESCRIPTION

## DBsubject('Algebra')
## DBchapter('Basic Algebra')
## DBsection('Algebraic Expressions')
## KEYWORDS('solve','linear','inequality','inequalities','interval','notation','set')
## DBCCSS('7.EE');

## Section1('2.6')
## Problem1('20')
## Author('Carl Yao')
## Institution('PCC')

##############################################

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGcourse.pl",
"PGML.pl",
"contextFraction.pl",
"contextArbitraryString.pl"
);

##############################################

Context("Numeric");

$a=random(2,5,1);
$c=list_random((2,3,4));
$b=$a*$c;
$left = Compute("$b");
$comp = "\geq";
$right = Compute("-$a*x");

Context("ArbitraryString");
$setstr = "\{x|x>=-$c\}";
$setans = Compute($setstr);
$alt = "\{x|-$c<=x\}";

#create an array with all possible answers
push(@ans,$setans);
push(@ans,$alt);

#This part shows up in the Correct Answer section
$ansDisplay ="";
@ans1 = @ans;
foreach $ele (@ans1) {
$ele =~ s/>=/ \geq /;
$ele =~ s/<=/ \leq /;
$ansDisplay = $ansDisplay.$ele."\text{ or }";
}
$ansDisplay = substr($ansDisplay,0,length($ansDisplay)-11);

Context("Interval");
Context()->flags->set("formatStudentAnswer"=>'parsed');
$intans = Compute("[-$c,inf)");

##############################################

TEXT(beginproblem());

BEGIN_PGML

Solve this inequality. Please type: [`\qquad`] "inf" for [`\infty`], [`\qquad`] ">=" for [`\geq`], [`\qquad`] and "<=" for [`\leq`].

[`` [$left] [$comp] [$right] ``]

Solution in set notation is [______________]. Set notation looks like [`\{x|x>1\}`]. The | symbol is above the keyboard's Enter key.

Solution in interval notation is [______________]. Interval notation looks like [`(1,\infty)`].

END_PGML

##############################################

$showPartialCorrectAnswers=1;

ANS($setans->cmp
(correct_ans=>$ansDisplay,
checker => sub {
my ($correct,$student,$ans) = @_;
$student = $student->value;
$student =~ s/ +//g;
$student =~ s/{/\{/;
$student =~ s/}/\}/;
if ($student !~ /^\{/) {
Value->Error("Set notation must be included inside {}");
}
foreach $ele (@ans) {
if ($student eq $ele) {return 1;}
}
return 0;
}
));

ANS($intans->cmp);

BEGIN_PGML_SOLUTION

[`
\begin{alignedat}{2}
[$b] & \geq & -[$a]x \\
\frac{[$b]}{-[$a]} & \leq & \frac{-[$a]x}{-[$a]} \\
-[$c] & \leq & x \\
\end{aligned}
`]

The solution in set notation is [`[$ansDisplay]`].

The solution in interval notation is [`[$intans]`].

END_PGML_SOLUTION

ENDDOCUMENT();


In reply to Carl Yao

Re: Adding { and } into Answer Preview section

by Davide Cervone -
The contextArbitraryString.pl macro file was updated about five months ago, and the new version properly handles special characters like { and } in the student and correct answers. So one quick solution would be to get the current version of that file. See
contextArbitraryString.pl on GitHub.

A better approach, however, would be to make a custom context that properly handles the braces and vertical bar, creating an appropriate MathObject. This is non-trivial, but has a number of important benefits. For example, the display will be better, students will get appropriate error messages for syntax problems, and alternative forms will be acceptable (e.g., both {x | x > 1} and {x | 1 < x}, etc.). I really don't recommend string checking for mathematical expressions, because you just don't have the flexibility to recognize other appropriate forms very easily.

I'll see about putting together such a context when I get the chance.

Davide
In reply to Davide Cervone

Re: Adding { and } into Answer Preview section

by Alex Jordan -
Hi Davide,

I'm thinking about trying to do this myself. I was thinking that I could copy contextInequalities.pl and add enough to it to handle
{x|-1<x<=2}
etc.

Does this sound like a good approach to you?

In reply to Alex Jordan

Re: Adding { and } into Answer Preview section

by Davide Cervone -
Subclassing the inequality classes is certainly one way to go. But the inequality classes are pretty complicated, with some subtle interactions to make the transition from inequality to interval and back in the right places. I actually was starting on a context to handle these sets this afternoon, and while it is easy to get it to parse the notation, getting it to produce a usable object in the end is turning out to be more complicated than I had hoped. It's not really turning out to be a good first project for someone taking a first dive into defining MathObjects and Contexts of their own.

Let me see what I can come up with, and if I don't get time to finish it in the next few days, then you can go ahead with your plan.

Davide
In reply to Davide Cervone

Re: Adding { and } into Answer Preview section

by Carl Yao -
Hello Davide:

Thank you for your advice! We updated contextArbitraryString.pl, and now it handles brackets correctly. However, we ran into a new problem: We can no longer custom the Correct Answer any more.

ANS($setans->cmp
(correct_ans=>$ansDisplay,
...

The above code has no control over the Correct Answer area, but I did have control before the update of contextArbitraryString.pl.

Code is attached. Do you have any insight into this?

Carl Yao
Portland Community College




# WeBWorK problem written by Carl Yao
# Portland Community College
# ENDDESCRIPTION

## DBsubject('Algebra')
## DBchapter('Basic Algebra')
## DBsection('Algebraic Expressions')
## KEYWORDS('solve','linear','inequality','inequalities','interval','notation','set')
## DBCCSS('7.EE');

## Section1('2.6')
## Problem1('15')
## Author('Carl Yao')
## Institution('PCC')

##############################################

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGcourse.pl",
"PGML.pl",
"contextFraction.pl",
"contextArbitraryString.pl",
"answerHints.pl",
);

##############################################

Context("Numeric");

$a=random(1,5,1);
$b=random(6,10,1);
$left = Compute("$a");
$comp = "\leq";
$right = Compute("x+$b");

$c = $a-$b;
Context("ArbitraryString");
$setstr = "{x|x>=$c}";
$setans = Compute($setstr);
$alt = Compute("{x|$c<=x}");

#create an array with all possible answers
push(@ans,$setans);
push(@ans,$alt);

#This part shows up in the Correct Answer section
$ansDisplay ="";
@ans1 = @ans;
foreach $ele (@ans1) {
$ele =~ s/{/\{/;
$ele =~ s/}/\}/;
$ele =~ s/>=/ \geq /;
$ele =~ s/<=/ \leq /;
$ansDisplay = $ansDisplay.$ele."\text{ or }";
}
$ansDisplay = substr($ansDisplay,0,length($ansDisplay)-11);

Context("Interval");
Context()->flags->set("formatStudentAnswer"=>'parsed');
$intans = Compute("[$c,inf)");

##############################################

TEXT(beginproblem());

BEGIN_PGML

Solve this inequality. Please type: [`\qquad`] "inf" for [`\infty`], [`\qquad`] ">=" for [`\geq`], [`\qquad`] and "<=" for [`\leq`].

[`` [$left] [$comp] [$right] ``]

Solution in set notation is [______________]. Set notation looks like [`\{x|x>1\}`]. The | symbol is above the keyboard's Enter key.

Solution in interval notation is [______________]. Interval notation looks like [`(1,\infty)`].

END_PGML

##############################################

$showPartialCorrectAnswers=1;

ANS($setans->cmp
(correct_ans=>$ansDisplay,
checker => sub {
my ($correct,$student,$ans) = @_;
$student = $student->value;
$student =~ s/ +//g;
if ($student !~ /^{/) {
Value->Error("Set notation must be included inside {}");
}
if (index($student, '|') == -1) {
Value->Error("Set notation must include the '|' symbol.");
}
foreach $ele (@ans) {
if ($student eq $ele) {return 1;}
}
return 0;
}
));

ANS($intans->cmp);

BEGIN_PGML_SOLUTION

[`
\begin{alignedat}{2}
[$a] & \leq & x &{}+[$b] \\
-[$b] & & &{}-[$b] \\
[$c] & \leq & x \\
\end{aligned}
`]

The solution in set notation is [`[$ansDisplay]`].

The solution in interval notation is [`[$intans]`].

END_PGML_SOLUTION

ENDDOCUMENT();



In reply to Carl Yao

Re: Adding { and } into Answer Preview section

by Davide Cervone -
We can no longer custom the Correct Answer any more.

Actually, you can (and are doing so), but in recent versions of WeBWorK, there are now two values that control the answer shown in the correct answer column of the results table: one is a string like what students type, and one is typeset mathematics. You are setting the string like what the students type when you set correct_ans and it should not be TeX code, but an algebra string. This value is not the one shown in the correct answer column, but is available in a tooltip window when you hover over the correct answer with the mouse.

The value that is actually displayed is the one from correct_ans_latex_string, which is a relatively new value that has not traditionally been set by the answer checkers. For MathObject-based answers, if correct_ans_latex_string is not set, the correct_ans is parsed and the TeX form of the resulting Formula is used. But if the parsing fails (as it will with your correct_ans, since it is TeX code rather than valid algebra notation), correct_ans will be used directly. That is why you were getting the output you wanted (even though you were setting the wrong parameter).

In release 2.7 of WeBWorK, the MathObjects will set both the correct_ans and corrections_latex_strings automatically, and newer macro files, like the contextArbitraryString.pl that you downloaded already do that. So in this case, you set correct_ans, which overrides the one created by the ArbitraryString MathObject, but don't set correct_ans_latex_string, so the one that was originally created for the ArbitraryString stays in effect. If you hover over the correct answer column, you should see your $ansDisplay value in the tooltip.

To correct the problem, you should pass your TeX string to correct_ans_latex_string and a plain algebra string to correct_ans instead.

Of course, with the contextInequalitySetBuilder.pl file, you don't need to set either one, as both should be set properly for you by the SetBuilder MathObject.

Hope that clears things up.

Davide
In reply to Carl Yao

Re: Adding { and } into Answer Preview section

by Davide Cervone -
Well, I just couldn't stand the idea of your using string comparisons for this, so I wrote the context for set-builder notation using inequalities. I've attached the file. It turns out not to be as extensive as I had originally thought (though it went through some larger phases during development). So I'm pleased with it over all.

There is documentation at the top of the file, but basically you just load it and select one of the two contexts that it contains (InequalitySetBuilder or InequalitySetBuilder-Only). The former allows interval notation as well as set-builder notation, and the second only allows set-builder notation. Note, however, that you can use unions and differences (as with intervals and sets). I don't have anything in place to prevent that at the moment, but it could be done, if needed.

Also, note that the "such that" symbol is : not | because | is already in use for absolute values. If you must use |, there is a InequalitySetBuilder::UseVerticalSuchThat() to disable the absolute value | and make it "such that". Call this function before selecting the context.

A sample would be

    loadMacros("contextInequalitySetBuilder.pl");

    InequalitySetBuilder::UserVerticalSuchThat();
    Context("InequalitySetBuilder-Only");

    $set = Compute("{ x | x > 1 or x < -1 }");

    Context->texStrings;
    BEGIN_TEXT
    \($set\) = \{$set->ans_rule\}
    END_TEXT
    Context()->normalStrings;

    ANS($set->cmp);

Hope that works for you. Let me know if you have problems. This is new code, and hasn't been used in a classroom setting yet.

Davide

In reply to Davide Cervone

Re: Adding { and } into Answer Preview section

by Carl Yao -
Hi Davide:

There is one more issue, though:

Say the solution is {x|x>1}. Right now, if a student simply enters x>1, it's counted correct. I would love to have a flag to force students to practice set notation, including {x|...}.

Could you add a flag for this?

Carl Yao
Portland Community College
In reply to Carl Yao

Re: Adding { and } into Answer Preview section

by Davide Cervone -
Could you add a flag for this?

Sorry, I had meant to take care of that situation, and forgot about it while taking care of a number of other issues.

I have updated the package to math that a type match error. See if that works better for you.

Davide
In reply to Davide Cervone

Re: Adding { and } into Answer Preview section

by Alex Jordan -
We're having trouble with this context in some files, and I cannot figure out what is going on.

In the attached file with seed 2012, the first answer is:
{x | x > 8/7}

But this is not counted as correct. Do you have any idea why? I get the same behavior if I change $c (the randomly generated 8/7) to something simpler like 1.
In reply to Alex Jordan

Re: Adding { and } into Answer Preview section

by Alex Jordan -
We found a fix. Changing the Formula calls to Compute has solved our problem. Contrary to my understanding of Compute, I now need to manually set correct_ans_latex_string to keep the fraction from being converted to a decimal, but I'm OK with that.