Plot with subscripts, superscripts, and more general latex in labels

I use plot to create some mathematical graphs that go into papers otherwise written in Latex. For the graph I used today, all I needed was subscripts and a bar over letters. In Latex, it would be $s_q$ and $\bar{e}_{x}$.

I ended up using pict to create custom text that can be used by #:label in plot. But it was incredibly tedious, see code an image at the end.

My questions are how to do the following in a seamless way with plot, maybe some latex->pict function? In increasing order of difficulty:

  • String with subscript/superscript (the most common case). My answer: if I could figure out how to copy-paste utf-8, that would probably do the job, but seems pretty finicky.
  • Adding extras such as $\bar{e}$
  • Integrate full-blown latex with plots

Here is the code and the image it generates, the $\bar{e}$ is not going to win beauty contests anytime soon...

(require
         pict
         plot)

(define e-bar 8)
(define sq 3)
(define (my-text x)
  (text x (cons 'combine null) 15))
(define (my-subscript x)
  (text x (cons 'subscript null) 15))
(define sq-text (hc-append (my-text "s") (my-subscript "q")))
(define e-bar-text (hc-append
                    (vc-append (hline 7 2) (my-text "e"))
                    (my-subscript "x")))
(define second-term-text
  (hc-append
   (my-text "D'(") e-bar-text (my-text ")")
   (my-text "(") e-bar-text (my-text " - ") sq-text (my-text ")")))

(define D-of-e-bar-text
  (hc-append (text "D(") e-bar-text (text ")")))
(define D sqrt)
(define ((constant c) x) c)
(define sq-point
  (point-pict (vector sq (D sq)) sq-text #:anchor 'top-left))
(define e-bar-point
  (point-pict (vector e-bar (D e-bar)) e-bar-text #:anchor 'bottom-right))

(plot (list
       (function-interval (constant 0) (constant (D e-bar)) sq e-bar #:label second-term-text #:color 1)
       (function-interval D (constant 0) 0 e-bar #:label D-of-e-bar-text #:alpha 0.5)
       (vrule sq)
       (hrule (D sq))
       e-bar-point
       sq-point
       )
      #:x-max (add1 e-bar)
      #:y-max (+ (D e-bar) 0.5)
      #:x-label "e"
      #:y-label "D'"
      #:legend-anchor 'bottom-right
      #:out-file "negative-triple-d-difference.png")

It produces this picture:

negative-triple-d-difference

The package latex-pict can turn a string of latex into a pict,
which can be drawn on top of a plot.

I am not 100% you will be able to see the latex pict in a plot-snip,
but it works fine if you save as an svg or an bmp.

3 Likes

Jens already gave you the answer you probably need, but a couple of microoptimizations for you:

(cons 'foo null) is the same as '(foo) or (list 'foo)

If I understand it correctly then
(hc-append (text "D(") e-bar-text (text ")")))
is the same as (text (format "D(~a)" e-bar-text))

In general:
(format "string (display): ~a. string (print): ~v. symbol (display): ~a. symbol (print): ~v" "bar" "bar" 'bar 'bar)
yields
"string (display): bar. string (print): \"bar\". symbol (display): bar. symbol (print): 'bar". etc

This is beyond awesome Jens !

Except that I can't yet get it to work: I get a cryptic "latex failed" error for the following code (both in Emacs and DrRacket):

#lang at-exp racket

(require latex-pict)

(tex-math @~a{\bar{e}_{x}})

I assume this is some kind of latex misconfiguration on my part? Or is there some setup that I am missing? I read the info on latex-pict, but didn't see anything that I should be doing.

@dstorrs : Yes, thanks for the micro-optimizations, it's still helpful. As for format, that doesn't work. When I do it, I get something like D(#<pict>) instead. I didn't think that format knew what to do with the pict objects, although there may be some way to do it if it is all unicode at least? Or I might be doing it wrong.

Let's see if we can figure out where the problem is.

First let's check whether LaTeX and friends work.

Save this in foo.tex and then try latexpdf foo.tex in the terminal.
Does that produce a "foo.pdf" file with the equation?

\documentclass{standalone}
\usepackage[active,tightpage,lyx,pdftex,textmath]{preview}
\usepackage{amsmath}

\begin{document}

\( x^2+y^2=z^2 \)

\end{document}
1 Like

You probably meant pdflatex foo.tex, no? That throws an error that it can't find standalone.cls, so I installed it with tlmgr install standalone, after which it complains about not knowing preview.sty, so I tlmgr install preview and voila. It works. Reading the documentation for latex-pict, I now realize that I should have checked that both of those are installed, but it was not crystal clear to me that those were requirements. Maybe make it even more obvious by saying "Ensure that you have the following three packages installed ... You can check that your setup works by running pdflatex ... ....

The new image looks much nicer:

negative-triple-d-difference

Thanks a lot for this awesome package!

1 Like

I'll admit that the documentation is rudimentary.
Patches welcome!

1 Like