{"id":20583742,"url":"https://github.com/mzur/pretty-formula","last_synced_at":"2025-07-01T08:37:04.748Z","repository":{"id":24157864,"uuid":"27547779","full_name":"mzur/pretty-formula","owner":"mzur","description":"A small Java library to parse mathematical formulas to LaTeX and display them as images","archived":false,"fork":false,"pushed_at":"2014-12-04T16:59:41.000Z","size":4684,"stargazers_count":31,"open_issues_count":0,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T20:42:45.908Z","etag":null,"topics":["formula","images","java","latex"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mzur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-04T15:55:44.000Z","updated_at":"2024-03-30T21:35:35.000Z","dependencies_parsed_at":"2022-08-22T11:01:40.059Z","dependency_job_id":null,"html_url":"https://github.com/mzur/pretty-formula","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/mzur/pretty-formula","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzur%2Fpretty-formula","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzur%2Fpretty-formula/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzur%2Fpretty-formula/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzur%2Fpretty-formula/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mzur","download_url":"https://codeload.github.com/mzur/pretty-formula/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mzur%2Fpretty-formula/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262929399,"owners_count":23386356,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["formula","images","java","latex"],"created_at":"2024-11-16T06:43:32.781Z","updated_at":"2025-07-01T08:37:04.707Z","avatar_url":"https://github.com/mzur.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pretty-formula\n\nA small Java library to parse mathematical formulas to LaTeX and display them as images.\n\nFormula:\n\n```\n(a_1 / (b_1 + sqrt(c))^2) + sin(a_2 * b_2)\n```\nImage:\n\n![example](example.png?raw=true)\n\nLaTeX:\n\n```latex\n\\left(\\frac{{a}_{1}}{{\\left({b}_{1}+\\sqrt{c}\\right)}^{2}}\\right)+\\sin{\\left({a}_{2}\\cdot {b}_{2}\\right)}\n```\n\n## Installation\n\nGrab the latest release and add the `pretty-formula.jar` as well as all the dependencies from the `lib` directory to your project.\n\n## Usage\n\nPretty-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!\n\nAll the following functions are located in the package `de.uni_bielefeld.cebitec.mzurowie.pretty_formula.main`.\n\n### String parseToLatex(String formula)\n\nParses a mathematical formula String like `(a+b)/c` to valid math LaTeX.\n\n`formula`: A raw formula input String.\n\n**Returns:** The formula parsed to a small subset of LaTeX.\n\n**Throws:** `DetailedParseCancellationException` When the parsing fails.\n\n### BufferedImage parseToImage(String formula)\n\nParses a mathematical formula String like `(a+b)/c` to a pretty image.\n\n`formula`: A raw formula input String.\n\n**Returns:** An image object containing the rendered formula.\n\n**Throws:** `ParseException` When the formula rendering fails.\n\n**Throws:** `DetailedParseCancellationException` when the formula parsing fails.\n\n### void saveToSVG(String formula, File file)\n\nParses a mathematical formula like `(a+b)/c` to a pretty image and saves it as an SVG file.\n\n`formula`: A raw formula input String.\n\n`file`: The SVG file to save to.\n\n**Throws:** `ParseException` When parsing the LaTeX formula failed.\n\n**Throws:** `IOException` When writing the file failed.\n\n**Throws:** `DetailedParseCancellationException` When parsing the raw formula to LaTeX failed.\n\n## Example\n\nYou can find the fully working GUI example in [`GUIWindow.java`](src/de/uni_bielefeld/cebitec/mzurowie/pretty_formula/main/GUIWindow.java). To run it, simply execute `pretty-formula.jar`.\n\nThe important part of parsing the formula to an image and do error handling and user feedback is this:\n\n```java\nprivate void jTextPane1KeyReleased(java.awt.event.KeyEvent evt) {\n\t// feedback message\n   this.jLabel1.setText(\"\");\n   \n   // clear previously drawn formula\n   this.jLabel2.getGraphics().clearRect(0, 0, this.jLabel2.getWidth(), this.jLabel2.getHeight());\n   // remove error highlights from user input\n   this.jTextPane1.getHighlighter().removeAllHighlights();\n\n   try {\n   \t// parse the image\n      BufferedImage image = FormulaParser.parseToImage(this.jTextPane1.getText());\n      // display the image\n      this.jLabel2.getGraphics().drawImage(image, 0, 0, null);\n      \n   } catch (DetailedParseCancellationException e) {\n     // display user feedback on erroneous input\n     this.handleDetailedParseCancellationException(e);\n   } catch (ParseException e) {\n   \t// display user feedback on erroneous parsing (shouldn't happen, though)\n      this.jLabel1.setText(e.getMessage());\n   }\n}\n\nprivate void handleDetailedParseCancellationException(DetailedParseCancellationException e) {\n   try {\n      // highlight the position at which the error occurred\n      this.jTextPane1.getHighlighter().addHighlight(\n              e.getCharPositionInLine(), e.getEndCharPositionInLine(),\n              this.errorHighlighter);\n   } catch (BadLocationException ex) {\n      // simply don't highlight\n   }\n\n   // display the error message in addition to the error highlighting\n   this.jLabel1.setText(e.getMessage());\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmzur%2Fpretty-formula","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmzur%2Fpretty-formula","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmzur%2Fpretty-formula/lists"}