Open miguelfm opened 4 years ago
Due to how GR manages text strings, you should escape the minus sign, e.g. like this:
using GR
x=[0.001,0.1,1]; y=x
function prettyexp(x)
s = Base.Printf.@sprintf("%0.1e", x)
replace(s, "-"=>"\\-")
end
xticklabels(prettyexp)
plot(x,y)
Thank you very much. It is working well now. To use within plots and other backends I have to add the following code:
if occursin("GR",string(backend()))
s=replace(s, "-"=>"\\-") # Needed to Escape "-" in GR
end
Anyway, having to add this code just to "support" the minus sign in exponential notation is quite ugly. Is there any other way to fix this in the GR source code?
It is not just for the minus sign, but for other symbols that might be confounded with LaTeX-like math codes, e.g. _
(subscript), ^
(superscript), /
(fraction), different types of brackets, etc. This applies to all texts: in ticks, axis labels, titles, legends, etc.
(What I don't know is what is the full set of symbols that have such formatting meaning. I just found them by trial and error.)
So, it is an actual issue working with common used mathematical symbols in GR. I didn't know that. Thank you anyway!
I think I found where the math symbols are defined:
https://github.com/sciapp/gr/blob/2692d04fb4b286e17a34bbed12dcd3b151d00d67/lib/gr/text.c#L245
It has to be something more. The issue happens to the "-" symbol but, for instance, there is no problem with "+".
I would suggest to replace the '-' (minus) sign by an '–' (emdash) internally (in GR). What's your opinion?
Maybe the specific issue of extra space with the minus thing is this (in the GR framework, not in the Julia package):
https://github.com/sciapp/gr/blob/2692d04fb4b286e17a34bbed12dcd3b151d00d67/lib/gr/text.c#L761-L763
case Minus:
/* Minus.len < Plus.len => mLen = pLen */
len = textwidth("+", font, prec);
There the space reserved for the -
sign is equated to the space taken by the +
sign, even though the symbol may be smaller.
About using the emdash instead of the minus sign, I don't have any special preference.
I'm afraid I can't remember what caused this hack. But since it is "commented", there must have been a reason ... We should check it.
I would suggest to replace the '-' (minus) sign by an '–' (emdash) internally (in GR). What's your opinion? In my system replacing by emdash
s=replace(s,"e-0"=>"e–")
shows "â" instead of the minus character :(.
In case that this could be helpful, the same issue in Plots was already solved here https://github.com/JuliaPlots/Plots.jl/pull/2317 .
Due to how GR manages text strings, you should escape the minus sign, e.g. like this:
using GR x=[0.001,0.1,1]; y=x function prettyexp(x) s = Base.Printf.@sprintf("%0.1e", x) replace(s, "-"=>"\\-") end xticklabels(prettyexp) plot(x,y)
I have just tested this example and doesn't work exporting plot as pdf (i.e. with: savefig("test.pdf"))
test.pdf
Positive exponents look fine in exponential notation, but in the negative ones an extra blank space appears after the minus sign, ie. "8e- 01" instead of "8e-01". For instance:
It is funny that using the gr backend within Plots this issue shows only in logarithmic scales. This strange behavior doesn't happen with other backends like pyplot.
While the y axis is printed fine, in the x one (logarithmic scale) appears an extra blank space after the "e-".