Skip to content

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

Davide P. Cervone edited this page May 28, 2013 · 3 revisions

From https://groups.google.com/d/topic/mathjax-users/cpwy5eCH1ZQ/discussion


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


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


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


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

Clone this wiki locally