## Section10.2WeBWorK Problems in Source

A <webwork> tag must be inside an <exercise>, optionally preceded by an <introduction>, and optionally followed by a <conclusion>.

<exercise>
<introduction>
</introduction>

<webwork>
</webwork>

<conclusion>
</conclusion>
</exercise>


There are several methods for putting content into the <webwork>. (Note that an empty <webwork> with no attributes will simply produce the camelcase WeBWorK logo.)

### Subsection10.2.1Using an Existing WeBWorK Problem

If a problem already exists and is accessible from the anonymous course's templates/ folder, then you can simply include it as a @source attribute. For example, if it is a problem in the Open Problem Library (OPL) then relative to the templates/ folder, its path is Library/... and you may use:

<webwork source="Library/PCC/BasicAlgebra/Exponents/exponentsMultiplication0.pg" />


Or if you have a problem's PG file, you can upload it into the anonymous course's templates/local/ folder and use it with:

<webwork source="local/my_problem.pg" />


### Subsection10.2.2Perl-free Problems

If you'd just like to rattle off a quick question with no randomization, you can do as in this example:

<exercise>
<webwork>
<statement>
<p><m>1+2=</m><var name="'3'" width="5" /></p>
</statement>
</webwork>
</exercise>


The <exercise> above could be given an optional <title>, <introduction>, and <conclusion>. The <webwork> inside could be given a <hint> and <solution>. These are discussed in Subsection 10.2.3.

In the above example, '3' is the @name attribute to a <var> element. This is how to create an answer blank that is expecting $3$ as the answer. What you give as a @name attribute will be passed to PG's Compute() command, so it needs to be valid input for Compute(). Note that you could pass a string encased in quotes, or a perl expression. Just be mindful of the difference:

• 8**2 will process a perl real using exponentiation and lead to the MathObject Real 64.

• '8^2' will process a perl string and lead to the MathObject Real 64.

• 8^2 will process the perl real using bitwise XOR and lead to the MathObject Real 10.

The default context is Numeric, which understands numerical expressions and formulaic expressions in the variable $x\text{.}$ You can activate some other context as in this example:

<exercise>
<webwork>
<setup>
<pg-code>
Context("ImplicitPlane");
</pg-code>
</setup>
<statement>
<p><var name="'x+y=1'" width="8" /></p>
</statement>
</webwork>
</exercise>


Many special contexts are automatically detected by PreTeXt, and it loads the appropriate macro file into the PG problem. However you may need to explicitly load a macro file as described in Subsection 10.2.3.

### Subsection10.2.3PG code in Problems

To have randomization in problems or otherwise take advantage of the algorithmic programming capabilities of Perl and WeBWorK's PG language requires using a <setup> tag. Having at least a little familiarity with coding problems in WeBWorK is necessary, although for simpler problems you could get away with mimicking the sample article in mathbook/examples/webwork/. A <statement>, (optional) <hint>, and (optional) <solution> follow.

<webwork>

<setup>
</setup>

<statement>
</statement>

<hint>
</hint>

<solution>
</solution>

</webwork>


The <setup> contains a <pg-code>. If you are familiar with code for WeBWorK PG problems, the <pg-code> contains lines of PG code that would appear in the “setup” portion of the problem. Typically, this is the code that follows TEXT(beginproblem()); and precedes the first BEGIN_TEXT or BEGIN_PGML. If your code needs any special WeBWorK macro libraries, you may load them in a <pg-macros> tag prior to <setup>, with each such .pl file's name inside a <macro-file> tag. However many of the most common macro libraries will be loaded automatically based on the content and attributes you use in the rest of your problem.

Here is a small example. Following the example, we'll continue discussing <statement> and <solution>.

<webwork>

<setup>
<pg-code>
$a = Compute(random(1, 9, 1));$b = Compute(random(1, 9, 1));
$c =$a + $b; </pg-code> </setup> <statement> <p>Compute <m><var name="$a" />+<var name="$b" /></m>.</p> <instruction>Type your answer without using the <c>+</c> sign.</instruction> <p>The sum is <var name="$c" width="2" />.</p>
</statement>

<solution>
<p><m><var name="$a" />+<var name="$b" />=<var name="$c" /></m>.</p> </solution> </webwork>  Within a <statement>, <hint>, or <solution>, reference <var> tags by @name. Within the <statement>, a <var> tag with either a @width or @form attribute creates an input field. The @name attribute declares what the answer will be. A <var> can have @form="essay", in which case it need not have a @name attribute. This is for open-ended questions that must be graded by a human. The form field will be an expandable input block if the question is served to an authenticated user within WeBWorK. But for the WeBWorK cells in PTX HTML output, there will just be a message explaining that there is no place to enter an answer. A <var> can have @form="array". You would use this when the answer is a Matrix or Vector MathObject (a WeBWorK classification) to cause the input form to be an array of smaller fields instead of one big field. A <var> can have @form="popup" or @form="buttons" for multiple choice questions. If you are familiar with PG, then in your <pg-code> you might write a custom evaluator (a combination of a custom answer checker, post filters, pre filters, etc.). If you store this similar to $my_evaluator = $answer -> cmp(...);  then the <var> can have @evaluator="$my_evaluator".

An <instruction> is specific instructions for how the reader might type or otherwise electronically submit their answer. Contents of an <instruction> will be omitted from print and other static output forms. The <instruction> is a peer to <p>, but may only contain “short text” children.

Some general information on authoring WeBWorK problems can be found in a set of videos at

webwork.maa.org/wiki/Problem_Authoring_Videos

Not all of this is relevant to authoring within PreTeXt but there are parts that will be helpful for constructing the Perl code necessary for randomized problems.

Planned.