Extending FITB exercises with JS extensions

9 views
Skip to first unread message

D. Brian Walton

unread,
Jul 20, 2026, 2:48:44 PM (11 days ago) Jul 20
to pretext-dev
This is an update to a question asked about a month ago (see this thread: https://groups.google.com/g/pretext-dev/c/SLHGR4u-zfQ) about fill-in-the-blank exercises allowing for various extensions.

The Runestone Component side of things already has everything in place if the javascript files that will be included have permanent URLs. The PreTeXt side of things had not yet had the work put into (not much) to define the necessary files that you want to include. This is what I am working on, in addition to making it so that the Runestone Component code also allows for locally included javascript files (e.g. in assets).

The Runestone side is going to involve a one-line addition (as far as I can tell). The PreTeXt side involves extending the schema for the #setup block to define the files that will be imported. Here is a sample proposed markup for such a problem:

<exercise label="fitb-find-row-echelon">
<setup>
<jsimports>
<jslibrary source="code/btm-matrix-library.js" />
</jsimports>
<setupScript>
<![CDATA[
v.baseMatrix = randomMatrix(RNG, 3, 3, 2, true);
v.rrefMatrix = rrefMatrix(v.baseMatrix);
]]>
</setupScript>
</setup>
<statement>
<p>
Consider the matrix defined by
<md>
A = <eval obj="baseMatrix"/>
</md>
Find the reduced row echelon form of <m>A</m>.
</p>
<p>
<m>\mathrm{rref}(A) = </m> <fillin name="rref" parser="parseMatrix"/>
</p>
<p>
A matrix is entered by listing the rows separated by commas between brackets [ and ], and for each row listing entries separated by commas between additional brackets.
The matrix
<m>\begin{bmatrix} 1 \amp 3 \amp 5 \\ 2 \amp 4 \amp 6 \end{bmatrix}</m>
would be entered as <c>[ [1, 3, 5], [2, 4, 6] ]</c>.
</p>
</statement>
<evaluation>
<evaluate name="rref">
<test correct="yes">
<jscmp>
testMatrixEqual(ans, rrefMatrix)
</jscmp>
</test>
</evaluate>
</evaluation>
</exercise>

In this specific example, the CDATA escaping of code was not really needed, but was included to emphasize that the script block could contain characters that break the XML.

There are two additions to the schema here. The first is the #jsimports block inside of #setup. The #jslibrary element is illustrated here with @source when the included file is a local file. Alternatively, it will be possible to use @url when the included js file is referenced by URL. The second addition is to specify a custom answer parser as an additional attribute of #fillin.

In my example, the `btm-matrix-library.js` file defines the functions `randomMatrix()` and `rrefMatrix()` that are called in the custom setup script. It also defines the `parseMatrix()` function that is used to parse the user's response to an internal matrix object and the `testMatrixEqual()` function used to see if the answer is correct. The library also ensures that the matrix objects have valid `toTeX()` formatters.

Are there any immediate things that jump out that should also be considered before I proceed?

Brian

D. Brian Walton

unread,
Jul 20, 2026, 2:51:07 PM (11 days ago) Jul 20
to pretext-dev
For Rob:

For the pending PR, would you prefer an example included in both sample-article and sample-book, or just one of those? I will also be trying my hand at keeping the schema up-to-date when I do this. I assume you prefer separate commits for the (1) implementation, (2) examples, (3) schema.

Brian
Reply all
Reply to author
Forward
0 new messages