# Difference between revisions of "NamedAnswerRules"

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.

PG problem file Explanation
DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl"
);
TEXT(beginproblem());


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>
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!