Skip to content

Fix #125: TeX/LaTeX formula rendering in chart titles, axis titles, tick labels, and legend entries#952

Closed
timmolter wants to merge 1 commit into
developfrom
timmolter/issue-125-tex-formulas-chart-elements
Closed

Fix #125: TeX/LaTeX formula rendering in chart titles, axis titles, tick labels, and legend entries#952
timmolter wants to merge 1 commit into
developfrom
timmolter/issue-125-tex-formulas-chart-elements

Conversation

@timmolter
Copy link
Copy Markdown
Member

Summary

Adds LaTeX math formula rendering to all chart text elements via JLatexMath.

Any string wrapped in $...$ is rendered as a display-mode LaTeX formula. Strings without the $ wrapper continue to use the existing TextLayout path unchanged — no behaviour change for existing charts.

XYChart chart = new XYChartBuilder()
    .title("$\\hat{y} = \\beta_0 + \\beta_1 x$")
    .xAxisTitle("$x$ (time, $\\mu s$)")
    .yAxisTitle("$y$ (amplitude)")
    .build();

chart.addSeries("$\\alpha$-channel", xData, yData);

Licensing — why this is safe now

JLatexMath is GPL v2. The original blocker (noted in this issue in 2016) was that the GPL licence is incompatible with XChart's Apache 2.0 without the Classpath Exception (linking exception).

That exception was added to JLatexMath in commit f173337 on 2016-01-19 with the 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 thread @timmolter referenced in his 2016 comment when he wrote:

"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. JLatexMath 1.0.7 has been on Maven Central since then with the Classpath Exception present.


Optional dependency

JLatexMath is declared <optional>true</optional> in xchart/pom.xml — exactly like VectorGraphics2D. Users who don't need TeX rendering do not need to add anything to their build.

If JLatexMath is not on the classpath, TexRenderer.isAvailable() returns false and $...$ strings fall back to plain text (no exceptions).

To opt in, add to your own pom.xml:

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

Implementation details

File Change
pom.xml Add jlatexmath 1.0.7 to <dependencyManagement>
xchart/pom.xml Declare jlatexmath as optional dep
TexRenderer.java (new) Central utility: isTeX(), getBounds(), render(), renderRotated() with runtime guard
ChartTitle.java Delegate sizing + paint to TexRenderer
AxisTitle.java X and Y axis titles; Y-axis rotation via BufferedImage intermediate
AxisTickLabels.java Per-tick sizing + paint for both axes
Legend_.java getSeriesTextBounds() + paintSeriesText()
TestForIssue125.java (new) Demo: XY chart with TeX in all four text areas + category chart with TeX tick labels
TexRendererTest.java (new) 10 unit tests: detection, bounds, fallback

Coordinate system note

TeXIcon uses top-left origin while TextLayout uses a baseline origin. TexRenderer compensates so TeX and plain-text elements align consistently.

Rotated Y-axis title

JLatexMath's paintIcon doesn't compose cleanly with pre-applied AffineTransform rotations. The Y-axis title is therefore rendered to an off-screen BufferedImage, then drawn with a −90° rotation — correct orientation with no rendering artefacts.


Tests

Tests run: 60, Failures: 0, Errors: 0, Skipped: 0

mvn fmt:check passes with no violations.

Closes #125

@timmolter timmolter force-pushed the timmolter/issue-125-tex-formulas-chart-elements branch 5 times, most recently from 5842f0b to edd14d1 Compare May 27, 2026 12:40
… 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>
@timmolter timmolter force-pushed the timmolter/issue-125-tex-formulas-chart-elements branch from edd14d1 to f213dce Compare May 27, 2026 13:16
@timmolter
Copy link
Copy Markdown
Member Author

Closing to rethink integration approach before re-opening. TexRenderer utility and demo are working; need to reconsider the minimal-footprint integration into core rendering classes.

@timmolter timmolter closed this May 27, 2026
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

1 participant