# Plots.jl: labels overlap if using LaTeXStrings

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. Unfortunately, it is too elaborate to pass on to students, who have to produce a lot of plots. Thanks.

The safest solution if using LaTeXStrings is to switch the backend to PGFPlotsX. But I reported an issue here: overlap of labels with LaTeXStrings · Issue #434 · jheinen/GR.jl · GitHub

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

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 Of course, you may define a function to automate the production of burgers.

2 Likes

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

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

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

1 Like

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

3 Likes

I got this answer in the issue:

You can use legend_hfactor > 1 to fine tune the spacing

Didn’t try it.

1 Like

This works:

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.

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

2 Likes
{"ft_lang_label":"__label__en","ft_lang_prob":0.9609068,"math_prob":0.8045183,"size":1321,"snap":"2022-05-2022-21","text_gpt3_token_len":305,"char_repetition_ratio":0.10250569,"word_repetition_ratio":0.0,"special_character_ratio":0.21650265,"punctuation_ratio":0.13483146,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9521116,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-28T04:22:13Z\",\"WARC-Record-ID\":\"<urn:uuid:2ff791b4-479c-4471-876d-b0ac36e8de61>\",\"Content-Length\":\"50170\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ecccfe0d-71b5-4356-8961-6c9746589514>\",\"WARC-Concurrent-To\":\"<urn:uuid:8feff5cf-0f52-4e16-9675-f2c829e049c8>\",\"WARC-IP-Address\":\"64.71.144.205\",\"WARC-Target-URI\":\"https://discourse.julialang.org/t/plots-jl-labels-overlap-if-using-latexstrings/69936\",\"WARC-Payload-Digest\":\"sha1:HFUL4SRAZUPPMSK3RUHZLOMYWHMMHMQX\",\"WARC-Block-Digest\":\"sha1:3VCFEFYLWH4YVBW7CZUAWXXVUW7XYFMZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652663012542.85_warc_CC-MAIN-20220528031224-20220528061224-00205.warc.gz\"}"}