Robert A. Beezer, David Farmer, Alex Jordan, Mitchel T. Keller, Oscar Levin
Section7.1WeBWorK Problems
You must extract WeBWorK content as described in Chapter 38 before you will be able to see any WeBWorK content in your output. For most cases, you should be able to do this by running pretext generate webwork to process WeBWorK exercises.
A <webwork> tag must be inside an <exercise> or a PROJECT-LIKE block, optionally preceded by an <introduction>, and optionally followed by a <conclusion>.
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.)
If a problem already exists and is accessible from the hosting courseβs templates/ folder, then you can try to simply include it with 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/.../foo.pg and you may use:
Warning7.1.1.Not Every PG Problem is Compatible with PreTeXt.
Some problems that work fine within WeBWorK are not compatible with PreTeXt. Typically, these are exercises that use older PG coding techniques. To be compatible with PreTeXt, all of the macros used by a problem must be updated to give PreTeXt output. We have done this with modern PG macros and macros that are frequently used. But still, not every PG problem is compatible with PreTeXt. In some cases, maybe it would be straightforward to train the macros to give PreTeXt output. But in many cases, older macros and problem files are not structured well and PreTeXt is all about good structure. So it could be a significant project to retrofit PreTeXt compatibility.
If you elect to use a problem that is incompatible with PreTeXt but you donβt yet know that, a few things could happen. One is that when you process the problems to gather static representations, you will get an error message that the problem did not return valid XML.
Or you might coincidentally get valid XML back, but something is just missing or wrong. Thereβs no automated check for that; you should read the output to check that the problem is complete. We recommend reading your PDF output with hints, answers, and solutions exposed, to be sure you are seeing the static version of the 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 7.1.3.
In the above example, '3' is the @name attribute to a <var> element. There is actually no βvariableβ named β3β; we are just using the slot where more complicated exercises would place a Perl variable answer.
So the above is how to create an answer blank that is expecting as the answer. What you give as a @name attribute will be passed to PGβs Compute() constructor, 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 differences. For example:
8**2 will process a perl real using exponentiation and lead to the MathObject Real 64.
The default context is Numeric, which understands numerical expressions and formulaic expressions in the variable . You can activate some other context as in this example.
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 7.1.3.
To have randomization in problems or otherwise take advantage of the algorithmic programming capabilities of Perl and WeBWorKβs PG language requires using a <pg-code> 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 pretext/examples/webwork/. A <statement>, optional<hint>, and optional<solution> follow.
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 precedes the first BEGIN_PGML. If your code needs any special WeBWorK macro libraries, you may load them in a <pg-macros> tag prior to <pg-code>, 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.
Within the <statement>, a <var> tag with either a @width or @form attribute creates an input field answer blank that expects the variable with that @name to be the answer.
A <var> can have @form with value 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 PreTeXt HTML output, there will just be a message explaining that there is no place to enter an answer.
A <var> can have @form with value array. You would use this when the answer is a Point, Vector, ColumnVector, or Matrix MathObject to cause the input form to be an array of smaller input fields instead of one big field.
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 with value $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.
. 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.
An <exercisegroup> is a collection of exercises with common instructions that are put into an <introduction>. If you put WeBWorK exercises in an exercisegroup, then when the exercises are exported to .pg problem files for use as online homework from a WeBWorK server it makes sense that the instructions from the <exercisegroup>βs <introduction> should be included in the .pg file. And so they are included there. Note that they are not included when you are building HTML or LaTeX output for your project. (Rather, the <exercisegroup>βs <introduction> appears in its normal place.)
You should be aware of this when you write the <exercisegroup>βs <introduction>. It impacts the specific language you should use. For example, if you write βDifferentiate the following functions.β or βDifferentiate each of the functions below.β, then you have language that doesnβt fit the individual problem when it is used for homework on a WeBWorK server. Instead you might write βDifferentiate the functionβ. It makes sense as common instructions for the <exercisegroup> as well as the instructions for an individual exercise.
A <webwork>without a source attribute can have a plain text <description>. This should be a summary of what the exercise asks a user to do, including any relevant pedagogical details of the exercise. For example:
<webwork>
<description>
<line>
Add two fractions with distinct one-digit prime denominators.
</line>
<line>
One fraction is always positive, the other always negative.
</line>
</description>
...
</webwork>
The content of the description will be written into a PG COMMENT command, making the description visible in a WeBWorK Library Browser.
If a <webwork> has an @xml:id, then another <webwork> can βcopyβ the first one simply by using a @copy attribute whose value is the first <webwork>βs @xml:id. The @seed of the first <webwork> is ignored, and the second <webwork> may set its own @seed. For example:
The @copy attribute should point to a <webwork> that has PreTeXt-authored source, not to a <webwork> with a @source attribute. (If you want to copy one with a @source attribute, just reuse the same @source value.)
In a problem statement (or hint, or solution), you may use an <image> with a <latex-image> child. See Subsubsection 4.14.3.2 for generalities about using <latex-image>.
However, if a <latex-image> is inside a <webwork>, you must use \(...\) instead of $...$ to encase any inline math in the image. And if you have display math, that should use \[...\] instead of $$...$$.
Your latex-image code can use variables that you defined in the <pg-code> section. Scalar variables may be used simply by using their perl names including the dollar sign sigil. For example, \draw ($a,$b) -- ($c,$d); where $a, $b, $c, and $d are variables. However, any instance of \$ in code will be interpreted as an escaped dollar sign first, so rare situations may require you to be more explicit. The alternative is to include variables just as you would anywhere else in a problem statement, using a <var> element like <var name="$a"/>. You would also need to use a <var> element if you would like to insert a perl array, for example <var name="@a"/>.
In perl, $, @, and % each have special meaning. So you may wonder about using them in your latex-image code. The short answer is that you should use them just as you would use them in a regular LaTeX document. So when you would like a dollar sign, write \$. For a percent sign, use \%. An βatβ character does not need escaping, so write @.
As mentioned above, do not use a dollar sign to encase math. If you want to put a LaTeX comment in the code, you may write it in the usual way like % This is a comment.
Your projectβs <docinfo> may contain a <latex-image-preamble> element. If so, and if that preamble content should affect the latex-images inside the <webwork>, then you need to get that preamble content up on the WeBWorK host course. In many cases, you will need to get that preamble content up on the host course for the image to even compile. See Section 38.4.
In a problem statement (or hint, or solution), you may use an <image> with a @pg-name. This attributeβs values should be the name of an image created in the <pg-code> section. For example, an image created using init_graph from PGgraphmacros.pl.