Variable substitution - PGML

From WeBWorK_wiki
Jump to navigation Jump to search

Variable Substitution

Frequently, you will have computed or randomly generated values that you want to include in the text of your problem. To insert the value of a $a into PGML, use [$a]. Such values can be inserted into the text anywhere, including inside math delimiters.

 Suppose a manufacturer produces [$n] cars at a cost of [$cost] per car.


 Suppose [`f(x) = [$f]`].  Then [`f'(x) = `] [_________________].


 Suppose [: f(x) = [$f] :].  Then [: f'(x) = :] [_________________].

In the first example, the values of $a and $cost are inserted as plain text. In the second, example, if $f is a MathObject Formula, then its TeX form will be inserted and processed as TeX code (due to the [`...`] delimiters. In the third example, the Formula object's algebra string will be inserted into the math and processed in the Typeset Context documentation to generate the mathematical output.

You can access array elements and hash entries as well.

 @A = (1,2,3); %B = (x => 1, y => 2); $C = [1,2,3]; $D = {x => 1, y => 2};
 Array element: [$A[1]] produces 2  
 Hash element: [$B{x}] produces 1
 Array de-reference: [$C->[0]] produces 1
 Hash de-reference: [$D->{y}] produces 2

When a variable substitution is performed, the value of the variable will be displayed as part of the problem verbatim. So if the variable includes HTML code, for example, the code will be visible in the text, and will not have its normal formatting function. If you wish the HTML to be treated as actual HTML in the page, this can be accomplished. See the section on Escaping Substitutions for details.