From 6d6eccead08ccfe7d87b1284c032b11f97466de6 Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Sat, 15 Feb 2014 11:15:07 -0500 Subject: [PATCH] Keep track of ID's uses on the page, and make unique ones if there are repeats. Resolves issue #240. --- unpacked/extensions/TeX/AMSmath.js | 31 +++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/unpacked/extensions/TeX/AMSmath.js b/unpacked/extensions/TeX/AMSmath.js index a9b064ae4..eb591db2f 100644 --- a/unpacked/extensions/TeX/AMSmath.js +++ b/unpacked/extensions/TeX/AMSmath.js @@ -29,6 +29,8 @@ MathJax.Extension["TeX/AMSmath"] = { number: 0, // current equation number startNumber: 0, // current starting equation number (for when equation is restarted) + IDs: {}, // IDs used in previous equations + eqIDs: {}, // IDs used in this equation labels: {}, // the set of labels eqlabels: {}, // labels in the current equation refs: [] // array of jax with unresolved references @@ -187,7 +189,7 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () { global.label = label; if (AMS.labels[label] || AMS.eqlabels[label]) {TEX.Error(["MultipleLabel","Label '%1' multiply defined",label])} - AMS.eqlabels[label] = "???"; // will be replaced by tag value later + AMS.eqlabels[label] = {tag:"???", id:""}; // will be replaced by tag value later } }, @@ -197,11 +199,10 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () { HandleRef: function (name,eqref) { var label = this.GetArgument(name); var ref = AMS.labels[label] || AMS.eqlabels[label]; - if (!ref) {ref = "??"; AMS.badref = !AMS.refUpdate} - var tag = ref; if (eqref) {tag = CONFIG.formatTag(tag)} - if (CONFIG.useLabelIds) {ref = label} + if (!ref) {ref = {tag:"???",id:""}; AMS.badref = !AMS.refUpdate} + var tag = ref.tag; if (eqref) {tag = CONFIG.formatTag(tag)} this.Push(MML.mrow.apply(MML,this.InternalMath(tag)).With({ - href:CONFIG.formatURL(CONFIG.formatID(ref)), "class":"MathJax_ref" + href:CONFIG.formatURL(ref.id), "class":"MathJax_ref" })); }, @@ -423,7 +424,7 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () { if (!global.notag) { AMS.number++; global.tagID = CONFIG.formatNumber(AMS.number.toString()); var mml = TEX.Parse("\\text{"+CONFIG.formatTag(global.tagID)+"}",{}).mml(); - global.tag = MML.mtd(mml.With({id:CONFIG.formatID(global.tagID)})); + global.tag = MML.mtd(mml).With({id:CONFIG.formatID(global.tagID)}); } }, @@ -433,9 +434,20 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () { getTag: function () { var global = this.global, tag = global.tag; global.tagged = true; if (global.label) { - AMS.eqlabels[global.label] = global.tagID; if (CONFIG.useLabelIds) {tag.id = CONFIG.formatID(global.label)} + AMS.eqlabels[global.label] = {tag:global.tagID, id:tag.id}; } + // + // Check for repeated ID's (either in the document or as + // a previous tag) and find a unique related one. (#240) + // + if (document.getElementById(tag.id) || AMS.IDs[tag.id] || AMS.eqIDs[tag.id]) { + var i = 0, ID; + do {i++; ID = tag.id+"_"+i} + while (document.getElementById(ID) || AMS.IDs[ID] || AMS.eqIDs[ID]); + tag.id = ID; if (global.label) {AMS.eqlabels[global.label].id = ID} + } + AMS.eqIDs[tag.id] = 1; delete global.tag; delete global.tagID; delete global.label; return tag; }, @@ -586,12 +598,13 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () { TEX.prefilterHooks.Add(function (data) { AMS.display = data.display; AMS.number = AMS.startNumber; // reset equation numbers (in case the equation restarted) - AMS.eqlabels = {}; AMS.badref = false; + AMS.eqlabels = AMS.eqIDs = {}; AMS.badref = false; if (AMS.refUpdate) {AMS.number = data.script.MathJax.startNumber} }); TEX.postfilterHooks.Add(function (data) { data.script.MathJax.startNumber = AMS.startNumber; AMS.startNumber = AMS.number; // equation numbers for next equation + MathJax.Hub.Insert(AMS.IDs,AMS.eqIDs); // save IDs from this equation MathJax.Hub.Insert(AMS.labels,AMS.eqlabels); // save labels from this equation if (AMS.badref && !data.math.texError) {AMS.refs.push(data.script)} // reprocess later }); @@ -619,7 +632,7 @@ MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () { // TEX.resetEquationNumbers = function (n,keepLabels) { AMS.startNumber = (n || 0); - if (!keepLabels) {AMS.labels = {}} + if (!keepLabels) {AMS.labels = AMS.IDs = {}} } /******************************************************************************/