Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Question] Simplest way to use latex to svg function? #3323

Closed
Marchiuzzz opened this issue Jan 16, 2025 · 1 comment
Closed

[Question] Simplest way to use latex to svg function? #3323

Marchiuzzz opened this issue Jan 16, 2025 · 1 comment

Comments

@Marchiuzzz
Copy link

Greetings, this is a very powerful tool, which I am, unfortunately, not able to grasp it seems.
I have read the documentation, googled and asked Chat GPT, all of which seem to fail me.

My use case is relatively simple - I need to use this package's tex2svg function. That is basically all I need from this package. I'm building my own Vue custom component library for internal use, one of component's ideas is to generate latex expression using MathLive keyboard and then convert that into an svg (that's where I though MathJax would come in). All seems to be working fine within the component library, though once I install my library, which contains mathjax into other project, I start getting Cannot set property Package of #<Object> which has only a getter error (yes, I've already read all the related issues, they don't seem to help much). This is the snippet of the component that I'm building:

<template>
	<div>
		<div ref="mathEditor"></div>
		<div></div>
		<div
			v-if="svgHtml"
			v-html="svgHtml"></div>
	</div>
</template>
<script>
	import { Utils } from "../classes/Utils"
	import { MathfieldElement } from "mathlive"
	import "mathjax/es5/tex-svg.js"

	export default {
		name: "MathEditor",
		data() {
			return {
				mathEditor: null,
				formula: "",
				svgHtml: null,
			}
		},
                <...>
                methods:{
                      handleInput(value) {
				this.formula = value
				let svg = Utils.latexToSvg(value)
				this.$emit("update", { svg: svg, latex: this.formula })
			},
               }

Utils.js is also pretty simple

class Utils {
	static latexToSvg(latex) {
		if (window.MathJax) {
			let svgNode = window.MathJax.tex2svg(latex).getElementsByTagName("svg")[0]
			return svgNode.outerHTML
		}
	}
}
export { Utils }

What am I doing wrong? Is there a simpler way to achieve what I'm trying to do? Any help would be greatly appreciated

@Marchiuzzz
Copy link
Author

Since I'm using Vue, I ended up with this solution, which seems to do the trick

loadMathJax() {
  // Check if MathJax is already loaded
  if (!window.MathJax) {
  	// Create the script element and load MathJax
  	const script = document.createElement("script")
  	script.id = "MathJax-script"
  	script.src = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"
  	script.async = true
  	document.head.appendChild(script)
  }
},

Calling loadMathJax in mounted()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant