# Combining LaTeX with variables in PyPlot title

**URL:** https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128
**Category:** New to Julia
**Tags:** pyplot, latex
**Created:** [June 10, 2020, 2:23pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128 "2020-06-10T14:23:37Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [June 10, 2020, 2:23pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/1 "2020-06-10T14:23:37Z")

</div>

This is somewhat similar to [PyPlot: how to include a variable in a title](https://discourse.julialang.org/t/pyplot-how-to-include-a-variable-in-a-title/41121), but with the added complication of including LaTeX in the title too. See I have this code:

```julia
PyPlot.title(L"Plot of the 1st eigenfunction, corresponding to $\lambda=$ $(eigenvalue_approx[1])")

```

and as you can probably guess, I want lambda to be rendered as the appropriate lower case Greek letter and the first element of eigenvalue\_approx to be displayed after the equal sign. But currently I just get:

 ![Screenshot_20200611_002158](https://global.discourse-cdn.com/julialang/original/3X/2/f/2fe138357a4029b9f701bdfc2203d6fd9d1c2cff.png)

. Any ideas how to get around this issue?

---

<div class="post-metadata">

### Author: ![maxmouchet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxmouchet/32/7834_2.png) [@maxmouchet](https://discourse.julialang.org/u/maxmouchet)
#### Post date: [June 10, 2020, 2:31pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/2 "2020-06-10T14:31:47Z")

</div>

You can use `latexstring(args...)`:

```julia
PyPlot.title(latexstring(
"Plot of the 1st eigenfunction, corresponding to \$\\lambda=\$ $(eigenvalue_approx[1])"
))

```

Note that you need to escape the LaTeX commands in this case.  
From [https://github.com/stevengj/LaTeXStrings.jl#usage:](https://github.com/stevengj/LaTeXStrings.jl#usage:)

> You can also use the lower-level constructor `latexstring(args...)` , which works much like `string(args...)` except that it produces a `LaTeXString` result and automatically puts `$` at the beginning and end of the string if an unescaped `$` is not already present. Note that with `latexstring(...)` you _do_ have to escape `$` and `\` : for example, `latexstring("an equation: \$1 + \\alpha^2\$")` . One reason you might want to use `latexstring` instead of `L"..."` is that only the former supports [string interpolation](http://docs.julialang.org/en/latest/manual/strings/#interpolation) (inserting the values of other variables into your string).

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [June 10, 2020, 3:35pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/3 "2020-06-10T15:35:36Z")

</div>

You can also supply a mixture of `String` and `LaTeXString` args to `latexstring` to avoid the need for multiple escape characters:

```julia
julia> latexstring("Plot of the ", L"1^\mathrm{st}", " eigenfunction, corresponding to ", L"\lambda = ", rand(1:.1:10))

L"Plot of the $1^\mathrm{st}$ eigenfunction, corresponding to $\lambda = $4.5"

```

---

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [June 10, 2020, 3:38pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/4 "2020-06-10T15:38:49Z")

</div>

Is there a way to do what I asked originally AND specify the number of digits in eigenvalue\_approx[1] that are shown? eigenvalue\_approx[1] is only accurate to at most 10 digits, so showing 16 isn’t particularly helpful.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [June 10, 2020, 3:41pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/5 "2020-06-10T15:41:29Z")

</div>

```julia
julia> using Printf

julia> latexstring("Plot of the ", L"1^\mathrm{st}", " eigenfunction, corresponding to ", L"\lambda = ", 
                           @sprintf("%.10f", eigenvalue_approx[1]))

```

…or you could round the value: `round(eigenvalue_approx[1], sigdigits=10)`

---

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [June 10, 2020, 3:47pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/6 "2020-06-10T15:47:20Z")

</div>

And how do I incorporate that in my plot title? I ask because I tried the obvious, that is:

```julia
PyPlot.title(latexstring("Plot of the ", L"1^\mathrm{st}", " eigenfunction, corresponding to ", L"\lambda = ", 
                           @printf("%.10f", eigenvalue_approx[1])))

```

and got this plot:  
'

 ![Figure_7](https://global.discourse-cdn.com/julialang/original/3X/9/1/91e566b7a325a361056af232e20b1a54019c4aea.png)

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [June 10, 2020, 3:49pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/7 "2020-06-10T15:49:50Z")

</div>

Sorry, I added a ninja-edit fix to my earlier response - you need to use `@sprintf`, not `@printf`.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [August 26, 2021, 4:31pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/8 "2021-08-26T16:31:13Z")

</div>

`PyPlot` supports `LaTeXStrings`, and according to its [`README`](https://github.com/stevengj/LaTeXStrings.jl), the solution is as simple as

```julia
title(L"Plot of the 1st eigenfunction, corresponding to $\lambda = %$(eigenvalue_approx[1])$")

```

The point is to use `%$` for string interpolation in order to suppress `$` from being used as LaTeX equation markers.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [August 26, 2021, 4:51pm UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/9 "2021-08-26T16:51:31Z")

</div>

Yep, that’s been an option for about a year now thanks to this PR: [https://github.com/stevengj/LaTeXStrings.jl/pull/40](https://github.com/stevengj/LaTeXStrings.jl/pull/40)

---

<div class="post-metadata">

### Author: ![Van\_Sy\_Tnut](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/van_sy_tnut/32/217602_2.png) [@Van\_Sy\_Tnut](https://discourse.julialang.org/u/Van_Sy_Tnut)
#### Post date: [December 14, 2024, 12:30am UTC](https://discourse.julialang.org/t/combining-latex-with-variables-in-pyplot-title/41128/10 "2024-12-14T00:30:57Z")

</div>

Thanks for your sharing!
