feat: TeX/LaTeX formula rendering in chart elements (closes #125)#953
Merged
timmolter merged 1 commit intoMay 27, 2026
Merged
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Usage
Wrap any label string in
$...$:Optional dependency
JLatexMath is declared optional so consumers who don't need TeX rendering don't need the jar on their classpath:
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
f173337on 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 hereisTeX(text)— returns true when JLatexMath is available and string is$...$getBounds()— returns(0, -iconH, iconW, iconH)mimickingTextLayoutconventionsrender()— paints at baseline-equivalent position; 2× supersampling for crispnessrenderRotated()— for Y-axis titlesAxis_Y.getYAxisWidthHint()— replaced character-count heuristic with proper per-label width measurement viaTexRenderer.getBounds()Class.forName()in a static initializer; zero overhead when absentFiles changed
xchart/src/main/java/…/TexRenderer.javaxchart/src/main/java/…/ChartTitle.javagetBoundsHint()andpaint()xchart/src/main/java/…/AxisTitle.javaxchart/src/main/java/…/AxisTickLabels.javaxchart/src/main/java/…/Axis_Y.javaxchart/src/main/java/…/Legend_.javapom.xml(root)<dependencyManagement>with<optional>true</optional>xchart/pom.xmlxchart-demo/pom.xmlxchart-demo/…/TestForIssue125.javaxchart/src/test/…/TexRendererTest.java.github/copilot-instructions.mdRunning the demo
mvn install -pl xchart -DskipTests -q && \ mvn compile exec:java -pl xchart-demo \ -Dexec.mainClass=org.knowm.xchart.standalone.issues.TestForIssue125