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

Insert Math with Mathml or latex #2842

Open
king-jam1 opened this issue Nov 13, 2024 · 6 comments
Open

Insert Math with Mathml or latex #2842

king-jam1 opened this issue Nov 13, 2024 · 6 comments

Comments

@king-jam1
Copy link

I hope that the input of mathematical formulas and symbols can support the use of LaTeX or MathML, which is more compatible. Of course, existing methods can also be retained for coexistence
latexlive this website can export word. Looking forward to your answer.
Snipaste_2024-11-13_09-31-14
Snipaste_2024-11-13_09-32-16

@dolanmiu
Copy link
Owner

Yes, this is possible,

Is this what you need?

https://docx.js.org/#/usage/math

@king-jam1
Copy link
Author

Yes, this is possible,

Is this what you need?

https://docx.js.org/#/usage/math

I have reviewed the document you provided, but it is very inconvenient. For generating complex mathematical formulas, I would prefer to use LaTeX or MathML directly, and docx supports this approach. If the functionality hasn't been available yet, I really hope you can give me some advice on how to add such a feature on top of docx.

@dolanmiu
Copy link
Owner

That would require writing a parser/ast generator though. Are there any parsers available? I am sure this is not a trivial project

@king-jam1
Copy link
Author

I don't quite understand what you're trying to express. Can you explain it in detail?

@dolanmiu
Copy link
Owner

dolanmiu commented Dec 5, 2024

As in, you are suggesting docx to support LaTeX expressions? Something like latexlive

Feel free to make a PR to allow support for this, but from my knowledge, I am saying it is not easy

I can see someone has created a package: https://github.com/siefkenj/unified-latex?tab=readme-ov-file

@GreatApo
Copy link

GreatApo commented Dec 10, 2024

If anyone finds is helpful, I have written a very simple parser for a personal project. It supports divisions, subscripts and superscripts.

/** Converts simple mathematics equations to DOCX objects */
function stringToMathItem(l_str){
	if(l_str.includes("/")){
		// Find fraction
		let slpt = l_str.split("/");
		return new docx.MathFraction({numerator: [stringToMathItem(slpt[0])], denominator: [stringToMathItem(slpt[1])]});

	}else if(l_str.includes("_")){
		// Find subscript
		let slpt = l_str.split("_");
		return new docx.MathSubScript({children: [stringToMathItem(slpt[0])], subScript: [stringToMathItem(slpt[1])]});

	}else if(l_str.includes("^")){
		// Find superscript
		let slpt = l_str.split("^");
		return new docx.MathSuperScript({children: [stringToMathItem(slpt[0])], superScript: [stringToMathItem(slpt[1])]});
	}
	
	return new docx.MathRun(`${l_str}`);
};

It may require splitting a string first, e.g.:

let mathChildren = [];
mystr.split(" ").forEach(l_str =>{
    mathChildren.push(stringToMathItem(l_str));
    mathChildren.push(new docx.MathRun(` `));
});

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

3 participants