Live preview leads to "Label 'x' mutiply defined" error

167 views
Skip to first unread message

Susam Pal

unread,
Mar 30, 2012, 1:39:29 PM3/30/12
to MathJax Users
Hi,

I have an online application that shows live preview of the output
rendered by MathJax as the user types his LaTeX code in a form. I have
run into an issue regarding duplicate definition of labels while
updating the preview.

A demonstration of the issue is available in my application itself:
http://mathb.in/119

When you first load that page, the equation will be rendered fine. But
if you edit anything, it will trigger a preview update. After the
update, MathJax would display this error:

Label 'eq1' mutiply defined

The preview update is implemented as:

MathJax.Hub.Queue(['Typeset', MathJax.Hub, previewArea]);

If you are interested in the actual code, please see updatePreview()
function in http://mathb.in/scripts/mathb.js

I am able to reproduce this issue in the math.stackexchange.com form
and other websites that support live MathJax preview. Visit
http://math.stackexchange.com/questions/ask and paste this code:

\begin{equation}
\label{eq2}
\sum_{i=1}^n i = \frac{n(n + 1)}{2}
\end{equation}

The equation is rendered fine, but the moment you edit anything,
you'll see an error.

Is there a way to resolve this issue? I would want the users of my
website to be able to use \label and \eqref while posting their math
snippets.

Susam Pal

Peter Krautzberger

unread,
Mar 30, 2012, 2:06:50 PM3/30/12
to mathja...@googlegroups.com
This came up during the beta tests cf. http://groups.google.com/group/mathjax-users/browse_thread/thread/93338e170d6ab71c

Davide, you mentioned an API for clearing labels to be in 2.0, but I can't find it in the docs. Did that feature make it to 2.0?

Peter.

Susam Pal

unread,
Mar 30, 2012, 3:27:41 PM3/30/12
to MathJax Users
Thanks Peter.

This seems to work for me:

var AMS = MathJax.Extension["TeX/AMSmath"];
AMS.startNumber = 0;
AMS.labels = {};
MathJax.Hub.Queue(['Typeset', MathJax.Hub, previewArea]);

Now, I have this code every time I want to update the preview.
http://mathb.in/119 works fine as well. I have prepared a jsfiddle
demo to experiment with it: http://jsfiddle.net/rhDRz/

I didn't have to use MathJax.Hub.Reprocess(). I'll wait for Davide's
comment on this as well to know if this fix is okay or if there is a
better way to fix this.

Susam Pal

On Mar 30, 11:06 pm, Peter Krautzberger
<p.krautzber...@googlemail.com> wrote:
> This came up during the beta tests cf.http://groups.google.com/group/mathjax-users/browse_thread/thread/933...
>
> Davide, you mentioned an API for clearing labels to be in 2.0, but I can't
> find it in the docs. Did that feature make it to 2.0?
>
> Peter.
>
>
> On Fri, Mar 30, 2012 at 1:39 PM, Susam Pal <su...@susam.in> wrote:
> > Hi,
>
> > I have an online application that shows live preview of the output
> > rendered by MathJax as the user types his LaTeX code in a form. I have
> > run into an issue regarding duplicate definition of labels while
> > updating the preview.
>
> > A demonstration of the issue is available in my application itself:
> >http://mathb.in/119
>
> > When you first load that page, the equation will be rendered fine. But
> > if you edit anything, it will trigger a preview update. After the
> > update, MathJax would display this error:
>
> >    Label 'eq1' mutiply defined
>
> > The preview update is implemented as:
>
> >    MathJax.Hub.Queue(['Typeset', MathJax.Hub, previewArea]);
>
> > If you are interested in the actual code, please see updatePreview()
> > function inhttp://mathb.in/scripts/mathb.js
>
> > I am able to reproduce this issue in the math.stackexchange.com form
> > and other websites that support live MathJax preview. Visit
> >http://math.stackexchange.com/questions/askand paste this code:

Davide P. Cervone

unread,
Mar 31, 2012, 11:10:18 AM3/31/12
to mathja...@googlegroups.com
The feature did make it into v2.0 -- it's just the documentation that didn't.  I did say what it would be in the message you cite:

BTW, the new routine that I've added to the API is  MathJax.InputJax.TeX.resetEquationNumbers, so for the final 2.0  release, you should be able to use 

        MathJax.Hub.Queue( 
          ["resetEquationNumbers",MathJax.InputJax.TeX], 
          ["PreProcess",MathJax.Hub], 
          ["Reprocess",MathJax.Hub] 
        ); 


So I would go with this rather than changing the internal variables by hand.  That way, if the code for this gets updated, resetEquationNumbers will be updated as well, and you won't have to fix your own code.

Davide

Susam Pal

unread,
Apr 2, 2012, 8:18:43 AM4/2/12
to MathJax Users
Hi Davide,

Thank you for your response. I tried following your suggestion but it
doesn't work for me. It could be because of my lack of understanding
of how exactly MathJax.Hub.Reprocess and MathJax.Hub.PreProcess work.
I have created a jsFiddle paste to demonstrate the issue I am facing:
http://jsfiddle.net/WbCy3/ . We can see in this demo that the math
equations are rendered fine when we first press the 'Update' button,
but they are not rendered when we press the button the second time.
This is how I am doing the update when the button is pressed. Note
that previewArea.innerHTML is being completely overwritten before the
following code executes.

if (firstUpdate) { // firstUpdate is initialized to 'true'
MathJax.Hub.Queue(['Typeset', MathJax.Hub, previewArea]);
firstUpdate = false;
} else {
MathJax.Hub.Queue(
['resetEquationNumbers', MathJax.InputJax.TeX],
['Reprocess', MathJax.Hub, previewArea],
['PreProcess', MathJax.Hub, previewArea]
);
}

I am able to fix this by replacing calls to MathJax.Hub.Reprocess and
MathJax.Hub.PreProcess with MathJax.Hub.Typeset as follows:

if (firstUpdate) {
MathJax.Hub.Queue(['Typeset', MathJax.Hub, previewArea]);
firstUpdate = false;
} else {
MathJax.Hub.Queue(
['resetEquationNumbers', MathJax.InputJax.TeX],
['Typeset', MathJax.Hub, previewArea]
);
}

Here is the demo URL for my fix: http://jsfiddle.net/TXUMz/

Could you please explain me why MathJax.Hub.Reprocess and
MathJax.Hub.PreProcess calls do not re-render the math equations?
Also, do you think my way of fixing this using a call to
MathJax.Hub.Typeset is fine?

Susam Pal

On Mar 31, 8:10 pm, "Davide P. Cervone" <d...@union.edu> wrote:
> The feature did make it into v2.0 -- it's just the documentation that
> didn't.  I did say what it would be in the message you cite:
>
>         BTW, the new routine that I've added to the API is
> MathJax.InputJax.TeX.resetEquationNumbers, so for the final 2.0
> release, you should be able to use
>                 MathJax.Hub.Queue(
>                   ["resetEquationNumbers",MathJax.InputJax.TeX],
>                   ["PreProcess",MathJax.Hub],
>                   ["Reprocess",MathJax.Hub]
>                 );
>
> So I would go with this rather than changing the internal variables by
> hand.  That way, if the code for this gets updated,
> resetEquationNumbers will be updated as well, and you won't have to
> fix your own code.
>
> Davide
>
> On Mar 30, 2012, at 2:06 PM, Peter Krautzberger wrote:
>
>
>
>
>
>
>
> > This came up during the beta tests cf.http://groups.google.com/group/mathjax-users/browse_thread/thread/933...
>
> > Davide, you mentioned an API for clearing labels to be in 2.0, but I
> > can't find it in the docs. Did that feature make it to 2.0?
>
> > Peter.
>
> > On Fri, Mar 30, 2012 at 1:39 PM, Susam Pal <su...@susam.in> wrote:
> > Hi,
>
> > I have an online application that shows live preview of the output
> > rendered by MathJax as the user types his LaTeX code in a form. I have
> > run into an issue regarding duplicate definition of labels while
> > updating the preview.
>
> > A demonstration of the issue is available in my application itself:
> >http://mathb.in/119
>
> > When you first load that page, the equation will be rendered fine. But
> > if you edit anything, it will trigger a preview update. After the
> > update, MathJax would display this error:
>
> >    Label 'eq1' mutiply defined
>
> > The preview update is implemented as:
>
> >    MathJax.Hub.Queue(['Typeset', MathJax.Hub, previewArea]);
>
> > If you are interested in the actual code, please see updatePreview()
> > function inhttp://mathb.in/scripts/mathb.js
>
> > I am able to reproduce this issue in the math.stackexchange.com form
> > and other websites that support live MathJax preview. Visit
> >http://math.stackexchange.com/questions/askand paste this code:

Davide P. Cervone

unread,
Apr 2, 2012, 8:57:59 AM4/2/12
to mathja...@googlegroups.com
You have switched the order of Reprocess and PreProcess from the code
that I suggested. That will fail because the PreProcess phase is what
finds the math on the page and puts it in the form that MathJax is
looking for in the Reprocess phase. Your Typeset call works because
it does PreProcess then Process internally, so things are done in the
right order.

If the previewArea is the only place where the math is numbered, then
calling Typeset is fine because you have completely replaced the
previewArea HTML (so there are no previously typeset equations there
to be reprocessed). But if there are other equations elsewhere on the
page that are part of the same numbering sequence, then you will need
to use PreProcess on previewArea, but Reprocess on the entire page,
otherwise the numbers for the preview will not integrate properly with
the rest of the page. Hope that makes sense.

Davide

Susam Pal

unread,
Apr 2, 2012, 10:03:27 AM4/2/12
to mathja...@googlegroups.com
Thanks Davide. Yes, this makes sense and solves my problem.
Reply all
Reply to author
Forward
0 new messages