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

**URL:** <https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373>\
**Category:** Questions & Answers\
**Tags:** question\
**Created:** [December 7, 2021, 4:38pm UTC](https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373 "2021-12-07T16:38:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marc](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@marc](https://racket.discourse.group/u/marc)\
**Post date:** [December 7, 2021, 4:38pm UTC](https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373/1 "2021-12-07T16:38:30Z")

</div>

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...

```scheme
(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](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/4d2a4f7bd909b6db81f57e10aec15223bd6694ea.png)

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [December 7, 2021, 5:17pm UTC](https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373/2 "2021-12-07T17:17:02Z")

</div>

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

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/f910c592d2dacec82b643ae67a9a899ab1c9feaa.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [December 7, 2021, 5:24pm UTC](https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373/3 "2021-12-07T17:24:21Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![marc](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@marc](https://racket.discourse.group/u/marc)\
**Post date:** [December 7, 2021, 8:18pm UTC](https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373/4 "2021-12-07T20:18:06Z")

</div>

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):

```scheme
#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](https://docs.racket-lang.org/latex-pict/index.html), 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.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [December 7, 2021, 9:32pm UTC](https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373/5 "2021-12-07T21:32:46Z")

</div>

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?

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

\begin{document}

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

\end{document}

```

---

<div class="post-metadata">

**Author:** ![marc](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@marc](https://racket.discourse.group/u/marc)\
**Post date:** [December 8, 2021, 4:51am UTC](https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373/6 "2021-12-08T04:51:25Z")

</div>

> [@soegaard](#):
>
> Save this in `foo.tex` and then try `latexpdf foo.tex` in the terminal.

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](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/23bffc5b782412756f2cea6ebb46b40011efebfb.png)

Thanks a lot for this awesome package!

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [December 8, 2021, 10:54am UTC](https://racket.discourse.group/t/plot-with-subscripts-superscripts-and-more-general-latex-in-labels/373/7 "2021-12-08T10:54:09Z")

</div>

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