Variable substitution - PGML

From WeBWorK
(Difference between revisions)
Jump to: navigation, search
(Add hash and array element examples)
m (removed extra line break)
Line 12: Line 12:
  
 
   Suppose [: f(x) = [$f] :].  Then [: f'(x) = :] [_________________].
 
   Suppose [: f(x) = [$f] :].  Then [: f'(x) = :] [_________________].
 
  
 
In the first example, the values of <code>$a</code> and <code>$cost</code> are inserted as plain text.  In the second, example, if <code>$f</code> is a MathObject Formula, then its TeX form will be inserted and processed as TeX code (due to the <code>[`...`]</code> delimiters.  In the third example, the Formula object's algebra string will be inserted into the math and processed in the [[Typeset Context]] to generate the mathematical output.
 
In the first example, the values of <code>$a</code> and <code>$cost</code> are inserted as plain text.  In the second, example, if <code>$f</code> is a MathObject Formula, then its TeX form will be inserted and processed as TeX code (due to the <code>[`...`]</code> delimiters.  In the third example, the Formula object's algebra string will be inserted into the math and processed in the [[Typeset Context]] to generate the mathematical output.

Revision as of 15:44, 10 May 2015

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.

or

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

or

 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 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};
 BEGIN_PGML
 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
 END_PGML
follow us