/** * Builds a DOM sub-tree based on the currently parsed tokens, appending the results as * children of the given target root Element. This uses the default {@link DOMOutputOptions} * associated with this engine. * * @return true if completed successfully, false if the process was terminated by an error in the * input LaTeX and if the session was configured to fail on the first error. */ public boolean buildDOMSubtree(final Element targetRoot) { return buildDOMSubtree(targetRoot, defaultDOMOutputOptions); }
/** * Creates a new {@link SnuggleSession} with exactly the same state as the * {@link SnuggleSession} that created this {@link SnuggleSnapshot}. */ public SnuggleSession createSession() { return new SnuggleSession(this); } }
/** * Creates a well-formed external general parsed entity out of the currently parsed tokens. * <p> * The default {@link XMLStringOutputOptions} specified in the {@link SnuggleEngine} will be * used. * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public String buildXMLString() { return buildXMLString(defaultXMLStringOutputOptions); }
session.parseInput(input); session.writeWebPage(snuggleOptions, System.out, EndOutputAction.FLUSH); System.out.println(session.buildXMLString(snuggleOptions)); for (InputError error : session.getErrors()) { System.err.println(MessageFormatter.formatErrorAsString(error));
public static void main(String[] args) throws IOException { /* Create vanilla SnuggleEngine and new SnuggleSession */ SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); /* Parse some very basic Math Mode input */ SnuggleInput input = new SnuggleInput("$$ x+2=3 $$"); session.parseInput(input); /* Convert the results to an XML String, which in this case will * be a single MathML <math>...</math> element. */ String xmlString = session.buildXMLString(); System.out.println("Input " + input.getString() + " was converted to:\n" + xmlString); } }
/** * Creates a well-formed external general parsed entity out of the currently parsed tokens. * <p> * The given {@link XMLStringOutputOptions} Object is used to configure the process. * * @param options {@link XMLStringOutputOptions} to use, which must not be null. * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public String buildXMLString(final XMLStringOutputOptions options) { ConstraintUtilities.ensureNotNull(options, "XMLStringOutputOptions"); DocumentBuilder documentBuilder = XMLUtilities.createNSAwareDocumentBuilder(); Document document = documentBuilder.newDocument(); Element temporaryRoot = document.createElement("root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } return XMLUtilities.serializeNodeChildren(getStylesheetManager(), temporaryRoot, options); }
private Node latexToNode(String latex){ SnuggleEngine engine = new SnuggleEngine(); try { SnuggleSession createSession = engine.createSession(); createSession.parseInput(new SnuggleInput(latex)); NodeList dst = createSession.buildDOMSubtree(); return dst.item(0); } catch (IOException e) { throw new RuntimeException(e); } }
public Element doMathInput(String mathModeInput, DOMOutputOptions domOutputOptions) { ConstraintUtilities.ensureNotNull(mathModeInput, "Math Mode Input"); String latexInput = "\\[" + mathModeInput + "\\]"; session.reset(); try { session.parseInput(new SnuggleInput(latexInput, "Math Mode Input")); } catch (IOException e) { throw new SnuggleLogicException("Unexpected IOException"); } NodeList nodeList = session.buildDOMSubtree(domOutputOptions); if (!session.getErrors().isEmpty()) { return null; } /* Extract and check <math> element */ Element result = null; if (nodeList.getLength()==1) { result = (Element) nodeList.item(0); if (!MathMLUtilities.isMathMLElement(result, "math")) { throw new IllegalArgumentException("Input did not yield exactly 1 <math> element"); } } else { throw new IllegalArgumentException("Input did not yield exactly 1 result element"); } return result; }
/** * Convert a tex math formula into a Symja expression. The SnuggleTeX engine first converts the TeX expression in a * MathML expression. This MathML expression is then converted to Symja. * * @param texStr * the tex math formula * @return */ public IExpr toExpression(String texStr) { SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); session.getConfiguration().setFailingFast(true); SnuggleInput input = new SnuggleInput("$$ " + texStr + " $$"); try { if (session.parseInput(input)) { NodeList nodes = session.buildDOMSubtree(); int[] position = new int[] { 0 }; return convert(nodes, position, null, 0); } List<InputError> errors = session.getErrors(); for (int i = 0; i < errors.size(); i++) { fEngine.printMessage(errors.get(i).toString()); } } catch (Exception e) { if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } return F.$Aborted; }
public static void main(String[] args) throws IOException { /* Create vanilla SnuggleEngine and new SnuggleSession */ SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); /* Parse some very basic Math Mode input */ SnuggleInput input = new SnuggleInput("$$ e^{i\\pi} = -1 $$"); session.parseInput(input); /* Create "options" Object to SnuggleTeX what kind of web page we want. We're going * to generate one that will work fine with MOZILLA and tweak a few options, just for * fun! */ WebPageOutputOptions options = WebPageOutputOptionsTemplates.createWebPageOptions(WebPageType.MOZILLA); options.setTitle("My Web Page"); options.setAddingTitleHeading(true); options.setIndenting(true); options.setAddingMathSourceAnnotations(true); options.setIncludingStyleElement(false); /* Now ask SnuggleTeX to write the resulting output to the console. * (You would normally send the output somewhere more interesting, though!) */ session.writeWebPage(options, System.out); } }
/** * Builds a complete web page based on the currently parsed tokens, sending the results * to the given {@link OutputStream}, which is closed afterwards. * <p> * The provided {@link WebPageOutputOptions} Object is * used to determine which type of web page to generate and how it should be configured. * * @return true if completed successfully, false if the process was terminated by an error in the * input LaTeX and if the session was configured to fail on the first error. */ public boolean writeWebPage(final WebPageOutputOptions options, final OutputStream outputStream) throws IOException { return writeWebPage(options, null, outputStream, EndOutputAction.CLOSE); }
public List<InputError> getLastErrors() { return session.getErrors(); } }
session.parseInput(input); session.writeWebPage(snuggleOptions, System.out, EndOutputAction.FLUSH); System.out.println(session.buildXMLString(snuggleOptions)); for (InputError error : session.getErrors()) { System.err.println(MessageFormatter.formatErrorAsString(error));
public static void main(String[] args) throws IOException { /* Create vanilla SnuggleEngine and new SnuggleSession */ SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); /* Parse some very basic Math Mode input */ SnuggleInput input = new SnuggleInput("$$ x+2=3 $$"); session.parseInput(input); /* Convert the results to an XML String, which in this case will * be a single MathML <math>...</math> element. */ String xmlString = session.buildXMLString(); System.out.println("Input " + input.getString() + " was converted to:\n" + xmlString); } }
/** * Creates a well-formed external general parsed entity out of the currently parsed tokens. * <p> * The given {@link XMLStringOutputOptions} Object is used to configure the process. * * @param options {@link XMLStringOutputOptions} to use, which must not be null. * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public String buildXMLString(final XMLStringOutputOptions options) { ConstraintUtilities.ensureNotNull(options, "XMLStringOutputOptions"); DocumentBuilder documentBuilder = XMLUtilities.createNSAwareDocumentBuilder(); Document document = documentBuilder.newDocument(); Element temporaryRoot = document.createElement("root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } return XMLUtilities.serializeNodeChildren(getStylesheetManager(), temporaryRoot, options); }
private Node latexToNode(String latex){ SnuggleEngine engine = new SnuggleEngine(); try { SnuggleSession createSession = engine.createSession(); createSession.parseInput(new SnuggleInput(latex)); NodeList dst = createSession.buildDOMSubtree(); return dst.item(0); } catch (IOException e) { throw new RuntimeException(e); } }
public static void main(String[] args) throws IOException { /* Create vanilla SnuggleEngine and new SnuggleSession */ SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); /* Parse some very basic Math Mode input */ SnuggleInput input = new SnuggleInput("$$ a^2 = b^2 + c^2 $$"); session.parseInput(input); /* Create "options" Object to SnuggleTeX what kind of web page we want. We're going * to generate one that will work fine with MOZILLA and tweak a few options, just for * fun! */ WebPageOutputOptions options = WebPageOutputOptionsBuilder.createHTML5MathJaxOptions(); options.setTitle("My Web Page"); options.setAddingTitleHeading(true); options.setIndenting(true); options.setAddingMathSourceAnnotations(true); options.setIncludingStyleElement(false); /* Now ask SnuggleTeX to write the resulting output to the console. * (You would normally send the output somewhere more interesting, though!) */ session.writeWebPage(options, System.out); } }
/** * Builds a complete web page based on the currently parsed tokens, sending the results * to the given {@link OutputStream}, which is closed afterwards. * <p> * The provided {@link WebPageOutputOptions} Object is * used to determine which type of web page to generate and how it should be configured. * * @return true if completed successfully, false if the process was terminated by an error in the * input LaTeX and if the session was configured to fail on the first error. */ public boolean writeWebPage(final WebPageOutputOptions options, final OutputStream outputStream) throws IOException { return writeWebPage(options, null, outputStream, EndOutputAction.CLOSE); }
session.parseInput(input); session.writeWebPage(snuggleOptions, System.out, EndOutputAction.FLUSH); System.out.println(session.buildXMLString(snuggleOptions)); for (InputError error : session.getErrors()) { System.err.println(MessageFormatter.formatErrorAsString(error));
public static void main(String[] args) throws IOException { /* Create vanilla SnuggleEngine and new SnuggleSession */ SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); /* Parse some very basic Math Mode input */ SnuggleInput input = new SnuggleInput("$$ x+2=3 $$"); session.parseInput(input); /* Convert the results to an XML String, which in this case will * be a single MathML <math>...</math> element. */ String xmlString = session.buildXMLString(); System.out.println("Input " + input.getString() + " was converted to:\n" + xmlString); } }