Search code examples
latexgnuplot

double text when using label with boxes in gnuplot (cairolatex)


I am trying to use gnuplot with cairolatex as output along with a boxed label. Without the box around the label, the output is fine. However, as soon as I put in box around the label, the text appears twice slightly offset from each other. MWE is as follows:

reset session

set terminal cairolatex pdf color standalone font ',9'
set output "erfx.tex"

set key box bottom spacing 1.4

set zeroaxis
set xrange [-3:3]
set yrange [-1:1]

textstr = '$\displaystyle' \
      . '\textrm{erf}(x) = \frac{2}{\sqrt{\pi}}' \
      . '\int_0^x\, e^{-t^2} \, dt$'

set style textbox 1 opaque fillcolor "white" border linecolor "black" \
    margins 2,2
# Without "boxed bs 1" it works fine, but without the surrounding box
set label 1 textstr at graph 0.05, 0.9 left \
    boxed bs 1

set xlabel '$x \rightarrow$'
set ylabel '$\textrm{erf}(x) \rightarrow$'

plot erf(x) title 'erf$(x)$'

unset output
unset terminal

I then use the commands to get the output

gnuplot erfx.plt && pdflatex -interaction=batchmode erfx.tex

The result is as follows:

enter image description here

Any help will be appreciated.


Solution

  • The problem comes from this command

    set style textbox 1 opaque
    

    Gnuplot implements the "opaque box" property by drawing the content once, using that to determine the exact area covered, drawing a solid rectangle that fills the determined area, and then finally writing the text again so that it shows up on top of the solid-fill rectangle.

    Unfortunately that sequence doesn't work for the {eps/ps/pdf}latex terminals, because they collect all the text output into a separate layer for LaTeX to render. So both the pre- and post- rectangle text gets sent to LaTeX.

    Your label should work as expected if you switch that to

    set style textbox 1 transparent
    

    The program could probably be smarter about this, but that's what is happening here.