# How BEGIN TEXT...END TEXT blocks work

BEGIN_TEXT and END_TEXT must each appear on a line by themselves, with no additional white space. (This last requirement can sometimes cause problems -- make sure there is no white space on these lines.) The text between these two tokens is evaluated as described below, and then added to the text of the problem.

## Order of evaluation

1. First everything inside of each \{ ... \} construct is evaluated as a Perl statement (or statements). The output of the statement is expected to be a scalar (not a list or an array) and this string is inserted at the point where the braces occurred. If there is more than one perl statement only the output of the last statement is printed, so for example \{ $a=5; "" \} sets the variable $a to 5, but does not produce any output. \{ (4, 5, 6) \} will print 3 because it is evaluated in scalar context, and lists, in scalar context, output their length. In order to output the list itself use \{ join(' ', (4,5,6)) \} which joins the elements of the list together into a string, separating them with spaces.
2. Next all of the variables are interpolated -- that is, $a is replaced by its value, @list is replaced by its list of elements separated by spaces. 3. The math and display math mode text (found within $$...$$ or $...$) are processed according to the display mode to result in readable typeset mathematics. ## Final processing The output from all of the BEGIN_TEXT... END_TEXT blocks, as well as the TEXT() output (see below) is joined together to produce the complete problem. When generating hardcopy this final result is a fragment of a TeX document which is then processed by pdfLaTeX to produce the final PDF. In the other modes, no further processing is done, since the output is already in HTML format. ## Additional details The PG macro TEXT($in1, $in2,$in3); concatenates its inputs and produces the output of the problem. It is the "print" command of the PG language. It is sometimes convenient to use TEXT when it is known that no further processing of the input string is necessary:

TEXT(beginproblem());


The PG macro EV3($in1,$in2,...) (EValuation3) concatenates its input, processes the string as described above in the order of evaluation section, and outputs a string.

The BEGIN_TEXT...END_TEXT construction is literally replaced by:

TEXT(EV3(<<'END_TEXT'));
problem text goes here ...
END_TEXT


It is possible to write evaluators to replace EV3 which evaluate the text differently. EV3 is defined in PGbasicmacros.pl.