Giter Club home page Giter Club logo

pretty-formula's Introduction

pretty-formula

A small Java library to parse mathematical formulas to LaTeX and display them as images.

Formula:

(a_1 / (b_1 + sqrt(c))^2) + sin(a_2 * b_2)

Image:

example

LaTeX:

\left(\frac{{a}_{1}}{{\left({b}_{1}+\sqrt{c}\right)}^{2}}\right)+\sin{\left({a}_{2}\cdot {b}_{2}\right)}

Installation

Grab the latest release and add the pretty-formula.jar as well as all the dependencies from the lib directory to your project.

Usage

Pretty-formula provides three functions to parse mathematical formulas either to LaTeX or to bitmap/vector graphics. It provides a basic GUI as an example how to use it as well. This means you even can run pretty-formula.jar on its own!

All the following functions are located in the package de.uni_bielefeld.cebitec.mzurowie.pretty_formula.main.

String parseToLatex(String formula)

Parses a mathematical formula String like (a+b)/c to valid math LaTeX.

formula: A raw formula input String.

Returns: The formula parsed to a small subset of LaTeX.

Throws: DetailedParseCancellationException When the parsing fails.

BufferedImage parseToImage(String formula)

Parses a mathematical formula String like (a+b)/c to a pretty image.

formula: A raw formula input String.

Returns: An image object containing the rendered formula.

Throws: ParseException When the formula rendering fails.

Throws: DetailedParseCancellationException when the formula parsing fails.

void saveToSVG(String formula, File file)

Parses a mathematical formula like (a+b)/c to a pretty image and saves it as an SVG file.

formula: A raw formula input String.

file: The SVG file to save to.

Throws: ParseException When parsing the LaTeX formula failed.

Throws: IOException When writing the file failed.

Throws: DetailedParseCancellationException When parsing the raw formula to LaTeX failed.

Example

You can find the fully working GUI example in GUIWindow.java. To run it, simply execute pretty-formula.jar.

The important part of parsing the formula to an image and do error handling and user feedback is this:

private void jTextPane1KeyReleased(java.awt.event.KeyEvent evt) {
	// feedback message
   this.jLabel1.setText("");
   
   // clear previously drawn formula
   this.jLabel2.getGraphics().clearRect(0, 0, this.jLabel2.getWidth(), this.jLabel2.getHeight());
   // remove error highlights from user input
   this.jTextPane1.getHighlighter().removeAllHighlights();

   try {
   	// parse the image
      BufferedImage image = FormulaParser.parseToImage(this.jTextPane1.getText());
      // display the image
      this.jLabel2.getGraphics().drawImage(image, 0, 0, null);
      
   } catch (DetailedParseCancellationException e) {
     // display user feedback on erroneous input
     this.handleDetailedParseCancellationException(e);
   } catch (ParseException e) {
   	// display user feedback on erroneous parsing (shouldn't happen, though)
      this.jLabel1.setText(e.getMessage());
   }
}

private void handleDetailedParseCancellationException(DetailedParseCancellationException e) {
   try {
      // highlight the position at which the error occurred
      this.jTextPane1.getHighlighter().addHighlight(
              e.getCharPositionInLine(), e.getEndCharPositionInLine(),
              this.errorHighlighter);
   } catch (BadLocationException ex) {
      // simply don't highlight
   }

   // display the error message in addition to the error highlighting
   this.jLabel1.setText(e.getMessage());
}

pretty-formula's People

Contributors

mzur avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.