Skip to main content

The PreTeXt Guide

Section 16.1 <exercise> structure

An <exercise> can be rather freeform, containing elements such as <p>, <figure>, <image>, etc. However, an author will typically think about an exercise as having an associated correct answer, perhaps with a hint or a detailed solution. PreTeXt has tags to support that sort of content, but it does require that your <exercise> be structured. This is nearly identical to the structure of a project-like element.
Listing 16.1.1. An exercise
<exercise>
  <statement>
    <p>
      <idx><h sortby="statement"><tag>statement</tag></h><h sortby="of an exercise">of an <tag>exercise</tag></h></idx>
      <idx><h sortby="exercise"><tag>exercise</tag></h><h sortby="statement"><tag>statement</tag></h></idx>
      The <tag>statement</tag> is mandatory when any of
      <tag>hint</tag>, <tag>answer</tag>, or <tag>solution</tag>
      is included as a child of <tag>exercise</tag>,
      otherwise it may be omitted.
    </p>
  </statement>
  <hint>
    <p>
      <idx><h sortby="hint"><tag>hint</tag></h><h sortby="of an exercise">of an <tag>exercise</tag></h></idx>
      <idx><h sortby="exercise"><tag>exercise</tag></h><h sortby="hint"><tag>hint</tag></h></idx>
      Optional.
      Just an suggestion of what to try.
    </p>
  </hint>
  <answer>
    <p>
      <idx><h sortby="answer"><tag>answer</tag></h><h sortby="of an exercise">of an <tag>exercise</tag></h></idx>
      <idx><h sortby="exercise"><tag>exercise</tag></h><h sortby="answer"><tag>answer</tag></h></idx>
      Optional.
      Just the <q>final answer</q>.
    </p>
  </answer>
  <solution>
    <p>
      <idx><h sortby="solution"><tag>solution</tag></h><h sortby="of an exercise">of an <tag>exercise</tag></h></idx>
      <idx><h sortby="exercise"><tag>exercise</tag></h><h sortby="solution"><tag>solution</tag></h></idx>
      Optional.
      All the gory details.
    </p>
  </solution>
</exercise>
The code in Listing 16.1.1 produces the following output:

Checkpoint 16.1.2.

The <statement> is mandatory when any of <hint>, <answer>, or <solution> is included as a child of <exercise>, otherwise it may be omitted.
Hint.
Optional. Just an suggestion of what to try.
Answer.
Optional. Just the β€œfinal answer”.
Solution.
Optional. All the gory details.
Note that you can have multiple <hint>, <answer>, and <solution> elements. But you must put all the <hint>s first, then all the <answer>s, and then all the <solution>s. There are a variety of options for determining where hints, answers, and solutions appear (at all). Check Section 28.1 for information about stringparams.
An <exercise> can also have a more complicated structure that assigns a sequence of steps for a student to complete. PreTeXt provides the same <task> tag that is used in project-like elements to give structure to such an <exercise>.
Listing 16.1.3. An exercise with tasks
<exercise>
  <title>A structured exercise</title>
  <introduction>
    <p>
      Here is where we give the student the background information
      required to start accomplishing tasks.
    </p>
  </introduction>

  <task>
    <statement>
      <p>
        The first step to do.
      </p>
    </statement>
    <hint>
      <p>
        A little hint.
      </p>
    </hint>
    <answer>
      <p>
        Just the answer.
      </p>
    </answer>
    <solution>
      <p>
        All the glorious details about how to do the first step.
      </p>
    </solution>
  </task>

  <task>
    <statement>
      <p>
        The second step to do.
        We'll be lazy and just include an answer.
      </p>
    </statement>
    <answer>
      <p>
        Just the answer.
      </p>
    </answer>
  </task>

  <conclusion>
    <p>
      A little wrap up.
    </p>
  </conclusion>

</exercise>
The code in Listing 16.1.3 produces the following output:

Checkpoint 16.1.4. A structured exercise.

You have attempted 1 of 1 activities on this page.