# Plots.jl: labels overlap if using LaTeXStrings

**URL:** <https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936>\
**Category:** General Usage\
**Tags:** plotting, latex, plots\
**Created:** [October 17, 2021, 5:22pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936 "2021-10-17T17:22:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [October 17, 2021, 5:22pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/1 "2021-10-17T17:22:57Z")

</div>

Hi,

There seems to exist an od behavior of Plots when we use LaTeXStrings to format labels: they overlap. This problem is new because I am revisiting some teaching material, and it did not happen before.

I am on a Windows 10 machine, Julia 1.6.3, using Plots.jl v1.22.5, GR v0.61.0, LaTeXStrings v1.2.1. Please find an example below (it works the same way, even in simple plots with just two labels).

Help would be very much appreciated. I came across a turnaround to this problem [here](https://discourse.julialang.org/t/increase-space-between-legend-labels-in-plots/62474). Unfortunately, it is too elaborate to pass on to students, who have to produce a lot of plots. Thanks.

 ![a](https://global.discourse-cdn.com/julialang/original/3X/e/a/ea189bbc9980affe788da72cf225f2a020da4c7f.jpeg)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 17, 2021, 6:57pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/2 "2021-10-17T18:57:09Z")

</div>

The safest solution if using LaTeXStrings is to switch the backend to PGFPlotsX. But I reported an issue here: [https://github.com/jheinen/GR.jl/issues/434](https://github.com/jheinen/GR.jl/issues/434)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 17, 2021, 7:42pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/3 "2021-10-17T19:42:40Z")

</div>

@VivMendes, if the linked PR hasn’t been implemented yet, please find variations on the same theme below, with a different type of burger sandwich:

```julia
using LaTeXStrings, Plots; gr()
f(x, n) = x^n
x = 0:0.01:1;
clrs = [:red :blue :green :cyan :magenta]
fig = plot(xlims=(0, 1), ylims=(0, 1), xlabel="x", ylabel="y", legend=:outerright)
plot!(x, NaN*x, lc=:white, label=" ")
for n in 1:5
    plot!(x, f.(x, n), lc=clrs[n], label=L"y_{%$n}(x) = x^{%$n}")
    plot!(x, NaN*x, lc=:white, label=" ")
end
fig

```

![Plots_gr_LaTeXString_spaces](https://global.discourse-cdn.com/julialang/original/3X/5/5/55c011a23a7a7374127edd100e36d9a806188248.png)

Of course, you may define a function to automate the production of burgers.

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [October 17, 2021, 7:57pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/4 "2021-10-17T19:57:10Z")

</div>

@lmiq, that’s an option, and if the problem is not solved by the Plots team, possibly a good one. But I have a lot of plots and the arguments of Plots and PGFPlotsX are sometimes quite different. There was a similar problem in the past and it was solved (it was not about overlapping; symbols would not display correctly). Confusing that this LaTeXStrings problem keeps coming up. Thanks for the suggestion.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 17, 2021, 8:02pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/5 "2021-10-17T20:02:27Z")

</div>

Yeah, the rendering of latex within GR is a work in progress.

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [October 17, 2021, 8:07pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/6 "2021-10-17T20:07:00Z")

</div>

@rafael.guerra, you are a magician! That definitely solves the problem. But I need to keep the plotting issues as simple as possible. This is for master students in economics that are learning some programming techniques for the first time … in an economics course. If I do not keep it simple, they will get frustrated … and my Matlab colleagues will tell me “I told you so”. Thanks anyway.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 17, 2021, 8:10pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/7 "2021-10-17T20:10:23Z")

</div>

Uhm… IMHO, learning LaTeX syntax is much more complex than cooking the simple burger, but you know your students better.

Btw, a good lesson from Confucius: _Better a diamond with a flaw than a pebble without._

_ **NB:** _ _the diamonds here are Julia and Plots.jl_

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 17, 2021, 11:21pm UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/8 "2021-10-17T23:21:26Z")

</div>

I got this answer in the issue:

You can use legend\_hfactor \> 1 to fine tune the spacing

[https://github.com/JuliaPlots/Plots.jl/pull/3598#issuecomment-945200520](https://github.com/JuliaPlots/Plots.jl/pull/3598#issuecomment-945200520)

Didn’t try it.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 18, 2021, 11:48am UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/9 "2021-10-18T11:48:16Z")

</div>

This works:

```julia
julia> plot(rand(10),label=L"2\pi")

julia> plot!(rand(10),label=L"2\pi",legend_hfactor=1.5,extra_kwargs=:subplot)

```

Yet I agree that it is not very friendly to pass to students.

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [October 18, 2021, 11:52am UTC](https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936/10 "2021-10-18T11:52:02Z")

</div>

@lmiq, thanks. The `extra_kwargs=Dict(:subplot=>Dict(:legend_hfactor=>1.25))` indeed does the trick. That is good news.

The computer in my office had an older version of Plots and GR, and the plots produced last year look perfect (see an example below). Don’t know why upgrades keep coming up with new bugs as far as LaTeXStrings is concerned.

@rafael.guerra, yes, you are right: Julia and Plots are two diamonds. And thank you very much for your speedy and immaculate solutions. They always work. Thanks for your kind help.

 ![aa](https://global.discourse-cdn.com/julialang/original/3X/c/4/c4029514cf1197f1f7f37de30c9cfb3a2ab595bd.png)
