Skip to content

feat: TeX/LaTeX formula rendering in chart elements (closes #125)#953

Merged
timmolter merged 1 commit into
developfrom
timmolter/issue-125-tex-formulas-chart-elements
May 27, 2026
Merged

feat: TeX/LaTeX formula rendering in chart elements (closes #125)#953
timmolter merged 1 commit into
developfrom
timmolter/issue-125-tex-formulas-chart-elements

Conversation

@timmolter
Copy link
Copy Markdown
Member

Summary

Adds optional TeX/LaTeX math formula rendering to XChart chart elements. Any string wrapped in $...$ is rendered as a LaTeX math formula when the JLatexMath library is present on the classpath; otherwise it falls back gracefully to plain text.

Closes #125.


Affected chart elements

  • Chart title
  • Axis titles (X and Y)
  • Axis tick labels (X and Y, including category labels)
  • Legend entries

Usage

Wrap any label string in $...$:

XYChart chart = new XYChartBuilder()
    .title("$\\frac{E}{mc^2}$")
    .xAxisTitle("$\\alpha$ (radians)")
    .yAxisTitle("$\\sigma$ (Pa)")
    .build();

chart.addSeries("$\\beta_1$", xData, y1Data);
chart.addSeries("$\\gamma_2$", xData, y2Data);

Optional dependency

JLatexMath is declared optional so consumers who don't need TeX rendering don't need the jar on their classpath:

<dependency>
  <groupId>org.scilab.forge</groupId>
  <artifactId>jlatexmath</artifactId>
  <version>1.0.7</version>
</dependency>

If JLatexMath is absent, all strings render as plain text — no exceptions, no warnings.

Licensing

JLatexMath is licensed under GPL v2 + Classpath Exception (exception added in commit f173337 on 2016-01-19). The Classpath Exception permits use as a library from non-GPL code, making it compatible with XChart's Apache 2.0 license.


Implementation notes

  • TexRenderer.java — new central utility; all TeX measurement and painting goes through here
    • isTeX(text) — returns true when JLatexMath is available and string is $...$
    • getBounds() — returns (0, -iconH, iconW, iconH) mimicking TextLayout conventions
    • render() — paints at baseline-equivalent position; 2× supersampling for crispness
    • renderRotated() — for Y-axis titles
  • Axis_Y.getYAxisWidthHint() — replaced character-count heuristic with proper per-label width measurement via TexRenderer.getBounds()
  • JLatexMath availability detected once via Class.forName() in a static initializer; zero overhead when absent

Files changed

File Change
xchart/src/main/java/…/TexRenderer.java New — central TeX rendering utility
xchart/src/main/java/…/ChartTitle.java TeX branch in getBoundsHint() and paint()
xchart/src/main/java/…/AxisTitle.java TeX branch for X and Y axis titles
xchart/src/main/java/…/AxisTickLabels.java TeX branch for X and Y tick labels
xchart/src/main/java/…/Axis_Y.java Fixed width-hint measurement for TeX labels
xchart/src/main/java/…/Legend_.java TeX branch in bounds + paint
pom.xml (root) jlatexmath in <dependencyManagement> with <optional>true</optional>
xchart/pom.xml jlatexmath as groupId/artifactId only
xchart-demo/pom.xml jlatexmath with version (demo app, not optional)
xchart-demo/…/TestForIssue125.java New — runnable demo
xchart/src/test/…/TexRendererTest.java New — 10 unit tests
.github/copilot-instructions.md Added demo run instructions

Running the demo

mvn install -pl xchart -DskipTests -q && \
mvn compile exec:java -pl xchart-demo \
  -Dexec.mainClass=org.knowm.xchart.standalone.issues.TestForIssue125

… tick labels, and legend entries

Add JLatexMath (org.scilab.forge:jlatexmath:1.0.7) as an optional dependency.
Any string wrapped in $...$ is rendered as a LaTeX math formula using JLatexMath.
All other strings continue to use the existing TextLayout rendering path — no
behaviour change for existing charts.  If JLatexMath is not on the classpath the
TeX strings fall back gracefully to plain-text display.

### Licensing — fully clear

JLatexMath is GPL v2 with the Classpath Exception (linking exception).  This
exception permits JLatexMath to be linked from software distributed under any
licence, including XChart's Apache 2.0.

The exception was added in commit f173337015d89e29b8b2fa05de2c1f0e03ff8e3f
on 2026-01-19, with the commit message:

  "Change licence by adding linking exception.
   Discussed here: https://forge.scilab.org/index.php/p/jlatexmath/issues/1566/"

That scilab issue (#1566) is exactly the one that timmolter referenced in
XChart issue #125 on 2016-01-07 when he said:

  "They need to add the part about 'GPL with classpath exception' to their
   LICENSE… Until that exists, the license is incompatible with XChart's."

The condition was met ten years ago, and JLatexMath 1.0.7 has been on Maven
Central ever since.

### Changes

* pom.xml — add jlatexmath 1.0.7 to dependencyManagement (optional)
* xchart/pom.xml — declare jlatexmath optional dependency
* TexRenderer.java — new utility: isTeX(), getBounds(), render(), renderRotated()
  with Class.forName guard for graceful runtime fallback
* ChartTitle.java — delegate title sizing + paint to TexRenderer
* AxisTitle.java — delegate X and Y axis title sizing + paint to TexRenderer;
  Y-axis rotation handled via BufferedImage intermediate
* AxisTickLabels.java — delegate per-tick sizing + paint to TexRenderer for both
  X and Y axes (Y-axis uses a parallel String map alongside TextLayout map)
* Legend_.java — delegate getSeriesTextBounds() + paintSeriesText() to TexRenderer
* TestForIssue125.java — demo: XY chart with TeX title/axis-titles/legend entries
  + category chart with TeX tick labels
* TexRendererTest.java — 10 unit tests covering detection, bounds, and fallback

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Comment thread xchart/src/test/java/org/knowm/xchart/TexRendererTest.java
@timmolter timmolter merged commit dbbfecc into develop May 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TeX formulas in chart elements

2 participants