timbod7 / haskell-chart

A 2D charting library for haskell
429 stars 85 forks source link

subscripts and superscripts in labels #169

Open fedeinthemix opened 7 years ago

fedeinthemix commented 7 years ago

Hi,

I'm wondering if it is possible to insert sub- and superscripts in text labels, such a axis labels. For example in gnuplot using "v_{a1}^2" produces an output similar to the one produced by LaTeX in math mode.

timbod7 commented 7 years ago

No, not currently.

danielsmw commented 6 years ago

I've had some limited success in achieving this by using runBackendWithGlyphs on the Diagrams backend. Once it's a Diagram with native V2.Text objects sitting around, you can just render the diagram using the diagrams-pgf backend (which constructs an elaborate LaTeX document, rendering the diagram through PGF/TikZ).

With that method, I've successfully gotten all manner of math mode stuff into a Chart. Unfortunately, though, this leads to some issues with alignment of said text boxes:

offsetexample.pdf

I tried playing some tricks with inserting \hphantom{} into my various strings to no avail, so this may not be totally workable at the moment. But it's... something.

If anyone knows roughly what problem needs to be solved to handle those alignment issues, I'd be happy to try my hand at it. I'm semi-proficient with Diagrams, but don't really know enough about the internals of Chart at the moment to know how the diagram is produced from Chart.

timbod7 commented 6 years ago

Hey - that's cool. I guess the problem is that the alignment is being based upon the size of all of the characters in the latex input not just this visible symbols. If you are interested in experimenting, you might want to try tweaking the dTextSize function in Diagrams.hs such that it somehow calculates approximately based upon the displayed symbols.

danielsmw commented 6 years ago

Hey, thanks. I took your advice and played around a bit with dTextSize. Essentially I called out to the texrunner library, partially with direct calls and partially through some utilities provided by diagrams-pgf. The result was pretty satisfactory to me,

pgf-dia.pdf

c.f. the results from my previous post. Unfortunately, the code is a little slow right now because I'm making a separate call to pdflatex for every single hbox I want to measure the size of. texrunner provides a monadic interface for keeping pdflatex open which should dramatically speed things up, but I haven't had the time to think about interleaving that with the types in the Chart backend. Hopefully I can work that out soon.

(edit: I had originally not worked out the ascent/descent math correctly, but fixed it in the now-posted pdf.)