Last active
December 26, 2023 02:05
-
-
Save joelsleeba/2db285a3eafc2c07a5b7fd21a28d3e49 to your computer and use it in GitHub Desktop.
JS to copy tex code from ar5iv mathML equations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To use with userscript managers like TamperMonkey go to | |
// https://greasyfork.org/en/scripts/483099-ar5iv-tex-copy | |
// What is ar5iv? See for yourself. Change the X in arXiv to 5 | |
// https://arxiv.org/abs/2204.08819v1 | |
// https://ar5iv.org/abs/2204.08819v1 | |
function handleBlockEquationClick(event) { | |
event.stopPropagation(); | |
const equation = event.target.closest(".ltx_equation") | |
if (equation) { | |
const mathElements = equation.querySelectorAll("math"); | |
const altTexts = Array.from(mathElements).map(element => { | |
const altText = element.getAttribute("alttext"); | |
return altText.replace(/\\displaystyle/g, ""); // Remove '\displaystyle' | |
}); | |
const joinedAltText = altTexts.join(" "); | |
console.log(joinedAltText); | |
// Do something with the joined alt text here, e.g., display it in an alert: | |
navigator.clipboard.writeText(joinedAltText) | |
.then(() => { | |
// Copying succeeded | |
console.log("Alt text copied to clipboard"); | |
}) | |
.catch(err => { | |
// Copying failed, handle the error | |
console.error("Failed to copy alt text:", err); | |
}); | |
} else { | |
console.log("Clicked element is not within an .ltx_equation"); | |
} | |
} | |
function handleInlineEquationClick(event) { | |
event.stopPropagation(); | |
const equation = event.target.closest("math") | |
if (equation) { | |
const altText = equation.getAttribute("alttext"); | |
console.log(altText); | |
// Do something with the joined alt text here, e.g., display it in an alert: | |
navigator.clipboard.writeText(altText) | |
.then(() => { | |
// Copying succeeded | |
console.log("Alt text copied to clipboard"); | |
}) | |
.catch(err => { | |
// Copying failed, handle the error | |
console.error("Failed to copy alt text:", err); | |
}); | |
} else { | |
console.log("Clicked element is not within an .ltx_equation"); | |
} | |
} | |
// Attach the event listener to all descendants of .ltx_equationgroup | |
document.querySelectorAll(".ltx_equation *").forEach(element => { | |
element.addEventListener("click", handleBlockEquationClick); | |
}); | |
// // Attach the event listener to all descendants of .ltx_Math which are not themselves descendants of .ltx_equation | |
document.querySelectorAll(".ltx_Math[display='inline']:not(.ltx_equationgroup *) *").forEach(element => { | |
element.addEventListener("click", handleInlineEquationClick); | |
}); | |
// Note that a similar script can be implemented to copy TeX from mathML equations of other websites |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment