Skip to content

Conversation

@Alex-Jordan
Copy link
Contributor

Before this commit, when a WW exercise uses a custom LaTeX macro, and when that exercise is going to be rendered live, we open each chunk of math with whatever custom LaTeX definitions are needed for that chunk of math. This is pretty inefficient, since you might be redefining macros over and over again in the same exercise. Also it clutters up the MathJax tool that gives a user the tex code that was used to generate that chunk of math.

This changes all that. The PG being used for rendering the live version of the exercise now has no latex macro definitions at all. Instead, all of the project's custom LaTeX macros are loaded into MathJax once at the start of the problem (which, I should remind everyone, is within an iframe and not subject to the MathJax happenings in the ambient page.)

To accomplish that, the LaTeX macros text is kept in a visually hidden place in the ambient page, so that javascript can grab it and insert it at the start of the problem. This needs to be a separate instance of writing the LaTeX macros into the ambient page from what is already there. Because what is already there gets ingestedby MathJax, and becomes something completely different in the DOM to how it was born.

I can't think of an alternate way to reach the objective, except maybe to put the latex macros into a data attribute instead of into an element. I'll make that change if it seems better.

Either way, the second copy of the latex macros text will only show up in the HTML if the project is using WeBWorK. So no unnecessary clutter for other projects.

@Alex-Jordan
Copy link
Contributor Author

Since this came up before, and I'm looking this over again just now, I though I should explain better something that I wrote concisely above. We already write the author's LaTeX macros into the HTML like this:

<span class="process-math">\(\newcommand{\definiteintegral}[4]{\int_{#1}^{#2}\,#3\,d#4}
\newcommand{\indefiniteintegral}[2]{\int#1\,d#2} 
\newcommand{\lt}{&lt;}
\newcommand{\gt}{&gt;}
\newcommand{\amp}{&amp;}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle     \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle        \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle      \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)</span>

But when the page loads, that span has been changed by MathJax to this:

<span class="process-math">
    <mjx-container
        class="MathJax CtxtMenu_Attached_0"
        jax="CHTML"
        style="font-size: 110.9%; position: relative;"
        sre-explorer-id="0"
        tabindex="0"
        role="application"
        ctxtmenu_oldtabindex="1"
        ctxtmenu_counter="0">
        <mjx-math
            data-semantic-type="empty"
            data-semantic-role="unknown"
            data-semantic-id="0"
            data-semantic-speech=""
            class="MJX-TEX"
            aria-hidden="true">
        </mjx-math>
        <mjx-assistive-mml
            unselectable="on"
            display="inline">
            <math
                xmlns="http://www.w3.org/1998/Math/MathML"
                data-semantic-type="empty"
                data-semantic-role="unknown"
                data-semantic-=""
                data-semantic-speech="">
            </math>
        </mjx-assistive-mml>
    </mjx-container>
</span>

In other words, the definitions of the macros are gone. The pretext-webwork.js has nowhere to look for them. So this explains the need for a redundant copy (in order to accomplish the goal here).

@rbeezer
Copy link
Collaborator

rbeezer commented Aug 27, 2025

I presume we will wait a bit on

https://groups.google.com/g/mathjax-users/c/0aG9NmDAIgI/m/eqIbGkSOAAAJ

I'll see progress. Holler when you are satisfied (or give up!).

@Alex-Jordan
Copy link
Contributor Author

Got some help from Davide, and I can use the outer page's instance of MathJax to access an array containing all the math chunks. Can I presume the 0th chunk (earliest span.process-math on the page) will always be the one that loads macros? And will always be nonempty because it defines \amp, and a few more things? I think so, but I want to check. Prototype is working for 2.20, and I will port it over to earlier versions if you agree that I can rely on the 0th item being the one I want to grab.

@rbeezer
Copy link
Collaborator

rbeezer commented Aug 28, 2025

Great! That mathjax-users thread sounded very hopeful, so I'm glad that offers a way forward.

I can't see any reason why the macros can't be first on the page, now or later. It'll come before any math in the masthead/title, no?

Let's put extensive code comments in the XSL so nobody is later tempted to move these to a bad place. Also, let's put a short one-line HTML comment into every page right prior to the macros saying "these go first for WW" and try to make it so that any bad pre-/post- diff might see that comment migrating around. Or something like that.

You've probably already thought this through. I saw that this must be some sort of JS array. No way to tag anything (classes, etc) that will make it less dependent on position?

Off-duty for much of the day - run with this?

@Alex-Jordan
Copy link
Contributor Author

I poked around more in the giant MathJax object but I can't work out a better way to zero in on which math chunk has the macros. I could search the math chunks for strings like \newcommand but it seems unnecessary.

Except you are right that any math in the title will come earlier in the linearization of the page than where we currently place the macros. So here are two options:

  • Move the latex-macros earlier in the HTML output. The very first thing after the Skip to Main Content link.
  • Search through the math chunks in that MathJax array looking for something... Maybe \newcommand{\amp}{&} or maybe I add something even more distinctive to lock in on.

@Alex-Jordan
Copy link
Contributor Author

Stand by. Off-list messaging with Davide have led to a better way.

@Alex-Jordan Alex-Jordan marked this pull request as draft August 28, 2025 19:47
@rbeezer
Copy link
Collaborator

rbeezer commented Aug 28, 2025

or maybe I add something even more distinctive to lock in on

I know I am on standby. We could add a LaTeX comment with a magic string. There is one floating around that might be available for reuse...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants