Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Clear the state #3275

Open
fachu000 opened this issue Aug 21, 2024 · 1 comment
Open

Clear the state #3275

fachu000 opened this issue Aug 21, 2024 · 1 comment

Comments

@fachu000
Copy link

I need a way to compile code without MathJax remembering the commands that I defined in previous compilations.

    async function printRenderedText(text: string) {
        const render = await window.MathJax.tex2chtmlPromise(text, { display: true });
        console.log(`For the code '${text}' the rendered HTML is: \n${render.outerHTML}\n\n`)
    }
    await printRenderedText("\\command")// Displays \command in red letters
    await printRenderedText("\\newcommand{\\command}{x+z} \\command ") // Displays x+z
    await printRenderedText("\\command")// I want it to display \command in red letters, but it displays x+z

Describe the solution you'd like
Something like window.MathJax.clear() so that MathJax forgets what I compiled before.

Describe alternatives you've considered
I tried enclosing the code in braces { } so that the command definition is local, I tried clearing the variables that I found that contained the new command:

window.MathJax.startup.document.menu.options.jax.CHTML.math.math = ""
window.MathJax.startup.document.menu.options.jax.CHTML.math.inputJax.latex = ""

and many other things.

Additional context
The output produced by the aforementioned code is

For the code '{\command}' the rendered HTML is: 
<mjx-container class="MathJax CtxtMenu_Attached_0" jax="CHTML" display="true" tabindex="0" ctxtmenu_counter="0" style="position: relative;"><mjx-math display="true" class="MJX-TEX" aria-hidden="true" style="margin-left: 0px; margin-right: 0px;"><mjx-texatom texclass="ORD"><mjx-mtext class="mjx-n" style="color: red;"><mjx-c class="mjx-c5C"></mjx-c><mjx-c class="mjx-c63"></mjx-c><mjx-c class="mjx-c6F"></mjx-c><mjx-c class="mjx-c6D"></mjx-c><mjx-c class="mjx-c6D"></mjx-c><mjx-c class="mjx-c61"></mjx-c><mjx-c class="mjx-c6E"></mjx-c><mjx-c class="mjx-c64"></mjx-c></mjx-mtext></mjx-texatom></mjx-math><mjx-assistive-mml unselectable="on" display="block"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow data-mjx-texclass="ORD"><mtext mathcolor="red">\command</mtext></mrow></math></mjx-assistive-mml></mjx-container>

For the code '{\newcommand{\command}{x+z} \command } ' the rendered HTML is: 
<mjx-container class="MathJax CtxtMenu_Attached_0" jax="CHTML" display="true" tabindex="0" ctxtmenu_counter="1" style="position: relative;"><mjx-math display="true" class="MJX-TEX" aria-hidden="true" style="margin-left: 0px; margin-right: 0px;"><mjx-texatom texclass="ORD"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D465 TEX-I"></mjx-c></mjx-mi><mjx-mo class="mjx-n" space="3"><mjx-c class="mjx-c2B"></mjx-c></mjx-mo><mjx-mi class="mjx-i" space="3"><mjx-c class="mjx-c1D467 TEX-I"></mjx-c></mjx-mi></mjx-texatom></mjx-math><mjx-assistive-mml unselectable="on" display="block"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow data-mjx-texclass="ORD"><mi>x</mi><mo>+</mo><mi>z</mi></mrow></math></mjx-assistive-mml></mjx-container>

For the code '{\command}' the rendered HTML is: 
<mjx-container class="MathJax CtxtMenu_Attached_0" jax="CHTML" display="true" tabindex="0" ctxtmenu_counter="2" style="position: relative;"><mjx-math display="true" class="MJX-TEX" aria-hidden="true" style="margin-left: 0px; margin-right: 0px;"><mjx-texatom texclass="ORD"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D465 TEX-I"></mjx-c></mjx-mi><mjx-mo class="mjx-n" space="3"><mjx-c class="mjx-c2B"></mjx-c></mjx-mo><mjx-mi class="mjx-i" space="3"><mjx-c class="mjx-c1D467 TEX-I"></mjx-c></mjx-mi></mjx-texatom></mjx-math><mjx-assistive-mml unselectable="on" display="block"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow data-mjx-texclass="ORD"><mi>x</mi><mo>+</mo><mi>z</mi></mrow></math></mjx-assistive-mml></mjx-container>

Help would be appreciated. Thanks!!

@dpvc
Copy link
Member

dpvc commented Aug 23, 2024

Macros, once defined (either by hand, or by loading an extension, including by using \require) are permanent, and can't be removed from the TeX input jax. You would need to create a new instance of the TeX input jax in order to get one without the previous definitions (and labels, etc.). The easiest way to do that is probably just to call MathJax.startup.getComponents() which will re-instantiate all the components (input and output jax, DOM adapter, and MathDocument). Its a bit of overkill, but should be the easiest to do. Note, however, that you will lose the CHTML character cache, so the CSS generated will be just for the next expression processed, so won't include the CSS for the previous expressions. But that may be what you want (it is not clear from your example). If you need to keep the CSS for the previous expressions, then you would need to re-instantiate only the TeX input jax and hook it into the existing MathDocument. That takes a little more work, but can be done if necessary.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants