Math Formula in Java

The challenge in this tutorial is to present a mathematical formulas written in LaTeX within our application. This example makes use of the third party library “JLaTeXMath – A Java API to render LaTeX”

which is available under GNU licence and can be obtained from – jlatexmath. Once in place then it’s a simple case to build a program to display the result. Create a new class extending the JFrame class and included a main() method.

Download source code and art work
(you will need to provide an email address)


Assigned The formula to a string where the Java formula using the JLaTexMath controls is “L = \\frac {1}{2}[x^2 + y^2]^\\frac{1}{2}”.

Pass the formula string as a parameter when creating a TeXFormula object. From the TeXFormula object call the createTeXIcon method passing the STYLE_DISPLAY constant from the TeXConstants class and the font point size which in this case I’m using 40. There are other styles which can be used such as STYLE_SCRIPT so feel free to try out some of the options.

Create a BufferedImage and pass it width, height and the constant “BufferedImage. TYPE_4BYTE_ABGR”

Then call the paintIcon method of the TeXIcon object passing a container, a graphics object and zeros for the other two parameters which are used to adjust the size of the image in some cases. For the container pass a new JLabel, for the graphics object pass the output of the getGraphics method from the BufferedImage.

To display our formula, create a JLabel and call the setIcon method for the label passing the TeXIcon object.

Finally we create a JPanel to use as the main panel within our Frame and add the JLabel to the panel. This panel is then added to our application frame, the frame is set as visible and the containers packed to use auto resizing.

That’s it, our formula is displayed within our Java application I chose to download the “JLaTeXMath minimal built for GeoGebra and MathGraph32” version which is a zip file and then extracted the JAR file and added this as an external library to my project.

6 Replies to “Math Formula in Java”

  1. Hello Sir. Please how is the font point size determinedd(in TexIcon)? sometimes, and depending on the size of the final image output, the formula is either blurry or overly sharp

    1. Hi, the size of the characters is determined by the last parameter on the line “ti =formel.createTeXIcon(TeXConstants.STYLE_DISPLAY, 80);”. When I scale the resultant image the text size does not alter, so I’m not seeing the blurring issue you mention. Are you able to provide more information? Are following exactly what was in the tutorial or have you made your own changes? If you have made changes can you provide some information on those changes.

  2. Hello, If my Formula contains a Variable. I want to create hyperlink from this variable to some Location.
    Is it possible in JLatexMath?

    1. Hi Imtiaz,
      As you probably are aware, when using “LaTex” there is the option to include packages and one of these is the “hyperref” package. This would allow you to add hyperlinks. The JLatexMath library used in this example does not seem to provide a means of including external packages so it does not look like you can. If anyone knows differently then please let us know.

    1. Hi Hector,
      The Math Formula example actually creates an image. Take a look at the line where we create a BufferedImage, the image is created from the TeXIcon object. Does that help?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.