latex-image in webwork statement

88 views
Skip to first unread message

Alex Jordan

unread,
May 2, 2021, 1:43:07 PM5/2/21
to prete...@googlegroups.com
I'm working on "image/latex-image" inside a "webwork/statement" (or
hint, solution). I have markup and XSLT questions that are
interconnected. Here is a small example for discussion:

<exercise>
<webwork>
<pg-code>
$h = random(1,3,1);
$k = random(1,3,1);
</pg-code>
<statement>
<p>
Here is a line segment.
</p>
<image>
<latex-image>
\draw (0,0) -&#x2d; ($h,$k) node
{\(~~$10\)};
</latex-image>
</image>
</statement>
</webwork>
</exercise>

1. Note that there is no \begin{tikzpicture} or \end{tikzpicture}.
This is because the WW tikz mechanism is built to write those for you.
Should the "latex-image" have @code="tikz" or something like that? And
a "latex-image" only becomes legal in a "webwork" if it has that
attribute and value?

2. The &#x2d; is a dash. That's just to make it legal to wrap this
whole thing as an XML comment, because a literal -- is illegal within
a comment.

3. Note the $h and $k in the tikz code. Those will be interpolated
using values of the perl variables $h and $k. Should the author be
writing this instead?
\draw (0,0) -&#x2d; (<var name="$h"/>,<var name="$k"/>);
Note that if that is how the markup should be, now we will need the
XSLT to process child elements.

4. Note the "~~$". Since dollar signs are the sigil that indicate a
perl variable, and the situation with backslashes is more complicated
than you might expect, this is the escape mechanism for printing a
bare dollar sign in tikz in WW.

5. We can either treat the "latex-image" as a string or use
"xsl:apply-templates" to it. If we do apply-templates, character
substitutions happen. Like \draw becomes \\draw. This is unwanted. How
could I make those character substitutions still work in the rest of
the statement (where a bare \ should become \\ in the PGML code) but
prevent those character substitutions from being applied within the
latex-image? In similar situations, I would treat the latex-image as a
string. But see item 3. If we have "var" children to process, don't I
need to use apply templates? Can I make a special template that
matches on text nodes inside a "webwork//latex-image" and doesn't
apply the character substitutions?

6. Meanwhile, how could I let authors write like:
node {\(\$x\)}
which is "normal" tikz, and turn the two-character "\$" into the
three-character "~~$" automatically? Or should we bring back the
"dollar" element for this?

7. Lastly, note that using dollars signs to open/close math in this
setting would open up further complications. I would like to just
outright require that math in these tikz images use \(...\) as in my
example above. Are there any objections to that?

Rob Beezer

unread,
May 2, 2021, 4:09:26 PM5/2/21
to prete...@googlegroups.com
Dear Alex,

Lots of good questions.

If you use "latex-image", then we need to defend against this exceptional case
everywhere we match on it as-is. (Or maybe we kill all the WW in the
"extract-latex-image" stylesheet?)

And then, even an attribute value of "tikz" will not differentiate unless we
look for ancestors. So at a minimum, all these questions suggest to me that
this code is different enough that it needs its own attribute value, since I see
a range of attribute values in our future.

@syntax="webwork-tikz"

Look at the current "extract-latex-image" stylesheet (I think). The new "label"
element, to support tactile images, was running into the same text-conversion
problem that defends against authors injecting raw LaTeX willy-nilly. I was
"apply-templates"'ing teh content of "label" so I think a modal template was the
solution? Holler if that is not enough guidance.

1. "Regular" tikz will allow authors to use \begin{tikzpicture} (or
\begin{circuittikz}) so one vote for this being a variant of its own.

2. I guess it is up to you?

5, 6. Fashion a big text string, interprwting any XML markup, save in a
variable, then search/replace for

"\$" => "~~$"

and similar. More votes a variant.

7. See (2).

I'm assuming this all goes to the WW server at representation-time and comes
back as images in teh previously mentioned *.tgz? Plus maybe in the problem
archive?

I've maybe not addressed every little thing, but perhaps that is ehough guidance
to run with?

Rob

Alex Jordan

unread,
May 2, 2021, 6:23:31 PM5/2/21
to prete...@googlegroups.com
Thanks, all very helpful.

> 5, 6. Fashion a big text string, interprwting any XML markup, save in a
> variable, then search/replace for

> "\$" => "~~$"

> and similar. More votes a variant.


I will want:
It costs \(\$x\).
to become:
It costs \(~~$x\).
OK, I can use
str:replace(string,'\$','~~$')

I think there could be something like:
\node {\parbox{5in}{there are\\$x$ lights}};
In this case, the ideal would be to leave that alone, not to turn the
"\$" into "~~$". But this solidifies the need to outlaw using dollar
signs for math mode. If a dollar sign is anywhere inside your
webwork//latex-image, it should either be used as \$ for a printed
dollar sign, or within a "var" as part of the @name. If it's never
meant to mean anything else, I think we can control it all.

> 2. I guess it is up to you?

That one wasn't a question, just an oddity that comes up with tikz in
XML. People are welcome to use literal --, they will just have trouble
commenting out the image when needed.

> @syntax="webwork-tikz"

It may be more technically precise to go with:
@syntax="PGtikz"
The PG macro library that this is for is called PGtikz.pl. I can
imagine other WeBWorK tikz approaches in the future, and maybe it's
wise not to use a broad name like "webwork-tikz"


So my original example has become:
<exercise>
<webwork>
<pg-code>
$h = random(1,3,1);
$k = random(1,3,1);
</pg-code>
<statement>
<p>
Here is a line segment.
</p>
<image>
<latex-image syntax="PGtikz">
\draw (0,0) -&#x2d; (<var
name="$h"/>,<var name="$k"/>) node {\(\$10\)};
</latex-image>
</image>
</statement>
</webwork>
</exercise>

> --
> You received this message because you are subscribed to the Google Groups "PreTeXt development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pretext-dev...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-dev/68abd862-24a9-dc3e-b54d-3d308ee0c5cc%40ups.edu.

Alex Jordan

unread,
May 2, 2021, 6:40:36 PM5/2/21
to prete...@googlegroups.com
> I'm assuming this all goes to the WW server at representation-time and comes
back as images in teh previously mentioned *.tgz? Plus maybe in the problem
archive?

Yes, it all goes to the server at representation-time, and the server
provides a tgz file. pretext/pretext unpacks it and puts all the image
files adjacent to the representations file. This much is working on my
PCC server. I think it will be working soon for the AIM servers. David
is helping me get ImageMagick and pdf2svg installed on the utmost
server and I'll be able to test the out-of-the-box sample-chapter
build using utmost (webwork-dev). With sl2x (webwork-ptx) I think I
want to wait to upgrade it until WW 2.16 is officially released, but
ImageMagick and pdf2svg are already there.

> Plus maybe in the problem
archive?

No, I think the problem archive just gets PG problems, and those PG
problems still have tikz code. So when those PG problems are rendered,
the server rebuilds images on the fly. Similar to what currently
happens with the WWPlot images (the pixelated png images you get from
WW's older image-making tool).

Rob Beezer

unread,
May 2, 2021, 7:05:56 PM5/2/21
to prete...@googlegroups.com
On 5/2/21 3:23 PM, Alex Jordan wrote:
>> 2. I guess it is up to you?
>
> That one wasn't a question, just an oddity

Oops. In addition to numerous typos, that was suppose to be (3).

> It may be more technically precise to go with:
> @syntax="PGtikz"

Sure! I think I just prefer @syntax to @code.

Sounding good.

Rob

Rob Beezer

unread,
May 2, 2021, 7:09:18 PM5/2/21
to prete...@googlegroups.com
On 5/2/21 3:40 PM, Alex Jordan wrote:
> No, I think the problem archive just gets PG problems, and those PG
> problems still have tikz code. So when those PG problems are rendered,
> the server rebuilds images on the fly. Similar to what currently
> happens with the WWPlot images (the pixelated png images you get from
> WW's older image-making tool).

I think that is what I meant. ;-) PreTeXt does its thing, and a problem in the
problem archive is sanitized enough to be palatable to anybody's WW server (at
the right version).

Rob


Sean Fitzpatrick

unread,
May 2, 2021, 7:38:51 PM5/2/21
to PreTeXt development
Does x equal 4 or 5 in this example?

Alex Jordan

unread,
May 7, 2021, 7:23:18 PM5/7/21
to prete...@googlegroups.com
For Sean:
https://www.youtube.com/watch?v=wjKQQpPVifY&t=194s

Using the AIM development WW server, I have this working. See the results at:
http://spot.pcc.edu/~ajordan/tikzwwtesting/section-9.html
When you click to make the exercise interactive, it takes a moment to
load. And then it takes even longer for the image to appear, because
the WW server is still generating it, going from tex to pdf to svg.
And if you click things like the re-randomize button, it will take a
moment again, because the problem is unaware that the image will be
the same image (it could be a dynamic image that depends on the seed,
so it is appropriate to regenerate it). Perhaps in the future the
initial click of the "make interactive" button could still use the
local image file instead of reproducing it that first time.

WW version 2.16 is still baking, although we really hope it comes out
of the oven soon. But for example, trying to get this work revealed a
bug in 2.16 for which I just submitted a fix. So it's an ongoing
effort. And I don't want to move the production AIM WW server to
version 2.16 until version 2.16 is officially released.

Is it OK that I move forward with the PTX pull request that has all
this? The code is dependent upon checking the WW server version, so
should not affect current users. The exception *might* be Sean, who
has a fake "2.16" server that is really 2.15 but identifies as 2.16 to
get early access to some other features. But I think this won't hurt
Sean either, because this is only to support problems that produce
tikz-based images, and nothing in APEX does that (yet). Testing can be
done with the AIM development server.

PTX source looks like:

<exercise>
<title>Solve using a tikz graph</title>
<webwork>
<pg-code>
Context()->variables->are(n=>'Real');
$answer = Formula("n(n+1)/2");
$refreshCachedImages=1;
</pg-code>
<statement>
<p>
This image is a visual proof that
<m>\sum_{k=1}^{n}k</m> equals what?
</p>
<image width="50%">
<latex-image>
\draw [dashed]
(0.5-0.5*6.5,-0.866025*6.5) -&#x2d; (7-0.5*6.5,-0.866025*6.5);
\draw [very thick]
(2-0.5*7,-0.866025*7) -&#x2d; (2-0.5*4,-0.866025*4) -&#x2d;
(5-0.5*7,-0.866025*7);
\foreach \x in {1,...,6}
\foreach \y in {1,...,\x}
\shade[ball color=blue!30]
(\y-0.5*\x,-0.866025*\x) circle (0.2cm);
\foreach \y in {1,...,7}
\shade[ball color=orange!40]
(\y-0.5*7,-0.866025*7) circle (0.2cm);
\node at (2.5,-0.5) [anchor=north
west]{\(\color{blue}\sum\limits_{k=1}^nk\color{black}\)};
\shade[ball color=blue]
(2-0.5*4,-0.866025*4) circle (0.2cm);
\shade[ball color=orange]
(2-0.5*7,-0.866025*7) circle (0.2cm);
\shade[ball color=orange]
(5-0.5*7,-0.866025*7) circle (0.2cm);
</latex-image>
</image>
<p>
<var name="$answer" width="20"/>
</p>
</statement>
</webwork>
</exercise>


This does not yet include any latex-image-preamble content. I have
that going separately. This seems like a good place to pause and get
merged before things like late-image-preamble and archiving.



On Sun, May 2, 2021 at 4:38 PM Sean Fitzpatrick <dsfitz...@gmail.com> wrote:
>
> Does x equal 4 or 5 in this example?
>
>
>> \node {\parbox{5in}{there are\\$x$ lights}};
>
> --
> You received this message because you are subscribed to the Google Groups "PreTeXt development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pretext-dev...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-dev/CAH%2BNcPatL-ZMcdVjRw5wCOWTk1f0pJs5BFyhs2kS0Ee18%2BDv2g%40mail.gmail.com.

Sean Fitzpatrick

unread,
May 7, 2021, 9:56:12 PM5/7/21
to PreTeXt development
Impressive!
I both dread and look forward to converting all the APEX exercises from pg image back to tikz.

Rob Beezer

unread,
May 7, 2021, 11:47:07 PM5/7/21
to prete...@googlegroups.com
Really nice! Once I used the right variable (n, not k), I even got the problem
right.

Yes, that all sounds like a good plan. As usual, just be explicit on the PRs as
to what is ready *and* what is not.

Thanks,
Rob

Alex Jordan

unread,
May 8, 2021, 1:41:48 AM5/8/21
to prete...@googlegroups.com
For David:

At http://spot.pcc.edu/~ajordan/tikzwwtesting/section-9.html#exercise-24
open Checkpoint 9.2. That image should be 50% wide. And well, it is.
But the ambient div.exercise-wrapper is smaller than 100% for some
reason I don't understand. Can you make div.exercise-wrapper be 100%
so the image really is (at least close to) 50% of the text width? (Or
maybe not literally div.exercise-wrapper, but whatever CSS you think
is best.)

Before that is changed, if you "Make Interactive" the problem, there
are similar but different issues with the image. It's possible that
after div.exercise-wrapper is 100%, those issues will go away. But
would you follow up by clicking to "Make Interactive" and see if you
see issues?
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-dev/aa635a48-9461-7bea-6378-4a91eae7d098%40ups.edu.

David Farmer

unread,
May 8, 2021, 8:36:46 AM5/8/21
to prete...@googlegroups.com

Should be okay now.

The "Make Interactive" did not have the image centered,
but it does now.

Please let me know if I missed something.

I am not sure what I did will work if the exercise is not
born in knowl. The div.exercise-wrapper appears in different
contexts and I am worried about messing it up in another situation
(such as multicolumn or side-by-side).
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-dev/CA%2BR-jrfJkZ_Vmx2Q5hTd0f-Z9cOpv3YbuoE2s7TH_tF4BjOoHA%40mail.gmail.com.
>

Alex Jordan

unread,
May 8, 2021, 12:28:29 PM5/8/21
to prete...@googlegroups.com
Thanks. Here is a place where there are WW exercises with images in an
exercisegroup:
Exercise 11
https://opentext.uleth.ca/apex-video/sec_continuity.html#exercise-122

When I make it interactive, it grows to full width. With this one, the
image is 100% wide in PTX source because otherwise, in print, it is
50% of 50% and too small.

I cannot make up my mind if it is good or bad that the problem becomes
100% wide.

Also, there is an issue where the opening paragraph of the exercise
starts after a line break. IIRC, that was previously addressed. But my
guess is that it's one of those things that is fragile and can break
with other CSS changes.

If you'd like to do anything wrt these issues, that's great and
appreciated. But I also think these last issues I raised could wait
until the June/July workshop. Glenn and I will be working to make the
PTX-WW interaction better, and maybe underlying things will change.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-dev/alpine.LRH.2.21.2105080751480.23771%40li375-150.members.linode.com.

Sean Fitzpatrick

unread,
May 8, 2021, 12:34:46 PM5/8/21
to PreTeXt development
With respect to image width in an exercise group: this is getting a bit into publisher territory, but...


Would it be possible to allow an author to specify image width as 50% of page width vs. column width?

Or even default to text width instead of column width?

On Sat., May 8, 2021, 10:28 a.m. Alex Jordan wrote:
Thanks. Here is a place where there are WW exercises with images in an
exercisegroup:
Exercise 11
https://opentext.uleth.ca/apex-video/sec_continuity.html#exercise-122

When I make it interactive, it grows to full width. With this one, the
image is 100% wide in PTX source because otherwise, in print, it is
50% of 50% and too small.

I cannot make up my mind if it is good or bad that the problem becomes
100% wide.

Also, there is an issue where the opening paragraph of the exercise
starts after a line break. IIRC, that was previously addressed. But my
guess is that it's one of those things that is fragile and can break
with other CSS changes.

If you'd like to do anything wrt these issues, that's great and
appreciated. But I also think these last issues I raised could wait
until the June/July workshop. Glenn and I will be working to make the
PTX-WW interaction better, and maybe underlying things will change.


--

Alex Jordan

unread,
May 8, 2021, 1:42:07 PM5/8/21
to prete...@googlegroups.com
Anything is possible :)

I think the hard(est) part would be LaTeX output? In some kind of
multicolumn environment (let's say two-column), is a column really 50%
of the page? Usually not. So what happens when an author would mark
the image as 50% wide? Or let's say, 47% wide, but that is still not
wide enough to fit?

Option 1: we try to calculate ahead of time when something is too
wide, and alert the publisher at run time. This seems hard, especially
since a publisher can change page margins, inter-column spacing, etc.

Option 2: we just let it be too wide and there will be warnings in the
latex log and something will look bad in the PDF and hopefully catch
your eye.

My idea, a version of which I do with ORCCA processing: author can
declare 47% width or whatever on an image. But that is interpreted as
a "max width" relative to the text width. At run time (like pdflatex,
not xsltproc), some calculation is done, and if 47% of textwidth is
larger than the current linewidth, you go with 100%.
> --
> You received this message because you are subscribed to the Google Groups "PreTeXt development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pretext-dev...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-dev/CAH%2BNcPZHD2iNXY3jG%3DRzv-TRqJ2JiPcsXa_%3DLSNeO_ZS8zuUxA%40mail.gmail.com.

David Farmer

unread,
May 8, 2021, 2:13:49 PM5/8/21
to prete...@googlegroups.com

The closer I look, the more issues I find. So leaving this
until a time we can work together might be a good idea.

But one of the issues is that initially that image has
an inline style of width:100%, but after making it interactive
it has it set to width:600 , which means 600px. So that is
the reason it gets so big. I don't want to add an !important
to over-ride the inline style, so that needs to be fixed in
the WW code.

In the test example you shared earlier, the image in the
interactive version has the width set to 300. (And prior to
being interactive no width, because it has class "contained"
inside an image-box.)

In main PreTeXt we have moved away from inline styles as much
as possible, and no longer directly style images. (But we do
style the containing .image-box .) We can explore removing
the img styles in this context.

We are in desperate need of a catalog of possible layouts,
particularly for exercises, because it is hard to detect
unwanted consequences. Maybe in a month or so I'll have the
energy to do that. Maybe adding to the sample article is
better that links which eventually will go out of date?
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-dev/CA%2BR-jrfX82nRNm3rHLEeyq5K0xYObYcf1VkSGz%2Bg%2BuWcW%3DLsWQ%40mail.gmail.com.
>
Reply all
Reply to author
Forward
0 new messages