Difference between revisions of "Variable substitution - PGML"

From WeBWorK_wiki
Jump to navigation Jump to search
(Add hash and array element examples)
(Add link to Escaping Substitutions)
 
(One intermediate revision by the same user not shown)
Line 13: Line 13:
 
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]] documentation 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.
 
   
 
You can access array elements and hash entries as well.
 
You can access array elements and hash entries as well.
Line 25: Line 24:
 
Hash de-reference: [$D->{y}] produces 2
 
Hash de-reference: [$D->{y}] produces 2
 
END_PGML
 
END_PGML
  +
  +
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 - PGML|Escaping Substitutions]] for details.
   
 
[[Category:PGML]]
 
[[Category:PGML]]

Latest revision as of 17:22, 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 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};
 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

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.