Docx equation color
Posted: Mon Oct 09, 2023 11:01 pm
Hello, i need to set color to math equations using docx4j. I am receiving latex equation converting it to MathML using SnuggleTex and after that converting MathML to OMML with MML2OMML.XSL file. Here is example of my code:
- Code: Select all
public P convert(String formula, WordprocessingMLPackage wordprocessingMLPackage) {
P paragraph = new P();
formula = Jsoup.parse(formula).text();
formula = formula.replace("checkmark", "lor").replaceAll("\\\\\\\\\\[[0-9.]+em\\]&", "");
if (formula.equals("\\(\\)")) {
return DocxToolkit.getP(" ");
}
if (!formula.isEmpty() && formula.charAt(0) != '\\') {
return DocxToolkit.getP(formula);
}
if (formula.contains("\\underset") || formula.contains("\\phantom")) {
paragraph.getContent().add(convertToImage(formula, wordprocessingMLPackage));
return paragraph;
}
SnuggleEngine snuggleTeX = new SnuggleEngine();
SnuggleSession session = snuggleTeX.createSession();
SnuggleInput input = new SnuggleInput(formula);
try {
session.parseInput(input);
} catch (IOException e) {
log.error("Unable to convert latex formula: {}", formula);
}
String mathMLString = session.buildXMLString();
String officeMathMLString = convertMathMLToOMML(mathMLString).replace("</m:t>", " </m:t>");
JAXBElement<CTOMath> ctoMathParaJAXBElement;
try {
ctoMathParaJAXBElement =
(JAXBElement<CTOMath>) XmlUtils.unmarshalString(officeMathMLString);
} catch (JAXBException e) {
log.error("Unable to insert formula", e);
return null;
}
paragraph.getContent().add(ctoMathParaJAXBElement);
return paragraph;
}