Difference between revisions of "NamedAnswerRules"

From WeBWorK_wiki
Jump to navigation Jump to search
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{historical}}
  +
 
<h2>Name Answer Rules</h2>
 
<h2>Name Answer Rules</h2>
   
Line 25: Line 27:
 
DOCUMENT();
 
DOCUMENT();
 
loadMacros(
 
loadMacros(
"PGstandard.pl",
+
"PGstandard.pl",
"MathObjects.pl",
+
"PGML.pl"
"PGML.pl"
 
 
);
 
);
TEXT(beginproblem());
 
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 75: Line 76:
 
# Basic PG usage (prefer PGML)
 
# Basic PG usage (prefer PGML)
 
BEGIN_TEXT
 
BEGIN_TEXT
Enter \($ans\): \{$ans->named_ans_rule($ansName, 6)\}
+
Enter \($ans\): \{$ans->named_ans_rule($ansName, 5)\}
 
END_TEXT
 
END_TEXT
 
LABELED_ANS($ansName, $ans->cmp);
 
LABELED_ANS($ansName, $ans->cmp);
   
  +
# Add 'less than or equal' and 'greater than or equal' MathQuill toolbar buttons.
 
TEXT(MODES(TeX => "", HTML => <<END_SCRIPT));
 
TEXT(MODES(TeX => "", HTML => <<END_SCRIPT));
 
<script>
 
<script>
 
window.addEventListener('DOMContentLoaded', function() {
 
window.addEventListener('DOMContentLoaded', function() {
if (!window.answerQuills) return;
+
if (!window.answerQuills) return;
answerQuills.$ansName.buttons.splice(-1, 0,
+
answerQuills.$ansName.buttons.splice(-1, 0,
{ id: 'leq', latex: '\\leq',
+
{ id: 'leq', latex: '\\leq',
tooltip: 'less than or equal (<=)',
+
tooltip: 'less than or equal (<=)',
icon: '\\leq' },
+
icon: '\\leq' },
{ id: 'geq', latex: '\\geq',
+
{ id: 'geq', latex: '\\geq',
tooltip: 'greater than or equal (>=)',
+
tooltip: 'greater than or equal (>=)',
icon: '\\geq' });
+
icon: '\\geq' });
 
});
 
});
 
</script>
 
</script>
 
 
END_SCRIPT
 
END_SCRIPT
 
   
 
ENDDOCUMENT();
 
ENDDOCUMENT();
Line 101: Line 101:
 
<p>
 
<p>
 
<b>Main Text:</b>
 
<b>Main Text:</b>
Insert all of your problem text using $ansName for the answer name.
+
Insert all of your problem text using $ansName for the answer name. Both the PGML and basic PG usage are demonstrated. Your problem should only use one of these. If you are not using PGML you should make the switch!
 
</p>
 
</p>
 
<p>
 
<p>
This is an example of a possible usage of the named answer rule. Here we add "greater than or equal to" and "less than or equal to" buttons to the MathQuill toolbar for the answer. This will only have effect if MathQuill is enabled for the course.
+
An example of a possible usage of the named answer rule is shown. Here we add "greater than or equal to" and "less than or equal to" buttons to the MathQuill toolbar for the answer. This will only have effect if MathQuill is enabled for the course.
 
</p>
 
</p>
 
</td>
 
</td>
Line 116: Line 116:
   
 
[[Category:Problem Techniques]]
 
[[Category:Problem Techniques]]
 
 
<ul>
 
<li>POD documentation: [https://webwork.maa.org/pod/pg/current/nameOfMacro.html nameOfMacro.pl]</li>
 
<li>PG macro: [https://github.com/openwebwork/pg/blob/master/macros/nameOfMacro.pl nameOfMacro.pl]</li>
 
</ul>
 

Latest revision as of 07:51, 30 October 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

Name Answer Rules


This PG code shows how to used named answers in problems in such a way that the problem will work in both homework and gateway quizzes. Note that this is an insertion, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
    "PGstandard.pl",
    "PGML.pl"
);

Initialization: The usual stuff here.

$ans = Compute(random(1, 5));

$ansName = NEW_ANS_NAME;

Setup: Create the answers and do the usual setup.

Generate an answer name to use. This answer name can now be used via the variable $ansName as needed to do whatever it is that you needed the answer name for. It will be the id of the html input for the answer name, and so can be used to do things with the html input via javascript, etc.

Many older problems just used a made up answer. That method will not work in gateway quizzes as it lacks the quiz prefix. The NEW_ANS_NAME method will give you an answer name that contains the quiz prefix if the problem is used in a gateway quiz.

# PGML usage
BEGIN_PGML
Enter [`[$ans]`]: [_]{$ans}{5}{$ansName}
END_PGML

# Basic PG usage (prefer PGML)
BEGIN_TEXT
Enter \($ans\): \{$ans->named_ans_rule($ansName, 5)\}
END_TEXT
LABELED_ANS($ansName, $ans->cmp);

# Add 'less than or equal' and 'greater than or equal' MathQuill toolbar buttons.
TEXT(MODES(TeX => "", HTML => <<END_SCRIPT));
<script>
window.addEventListener('DOMContentLoaded', function() {
    if (!window.answerQuills) return;
    answerQuills.$ansName.buttons.splice(-1, 0,
        { id: 'leq', latex: '\\leq',
            tooltip: 'less than or equal (<=)',
            icon: '\\leq' },
        { id: 'geq', latex: '\\geq',
            tooltip: 'greater than or equal (>=)',
            icon: '\\geq' });
});
</script>
END_SCRIPT

ENDDOCUMENT();

Main Text: Insert all of your problem text using $ansName for the answer name. Both the PGML and basic PG usage are demonstrated. Your problem should only use one of these. If you are not using PGML you should make the switch!

An example of a possible usage of the named answer rule is shown. Here we add "greater than or equal to" and "less than or equal to" buttons to the MathQuill toolbar for the answer. This will only have effect if MathQuill is enabled for the course.

Problem Techniques Index