Ruby Gnuplot and Backslash
I recently stumbled upon a distracting issue when trying to get the gnuplot gem to output LaTeX-syntax.
Ideally I would like:
plot.ylabel "$\\mu$"
plot.xlabel "$\\epsilon$"
to produce something like:
\put(160,2853){\rotatebox{-270}{\makebox(0,0){\strut{}$\mu$}}}%
\put(3023,140){\makebox(0,0){\strut{}$\epsilon$}}%
but it produces:
\put(160,2853){\rotatebox{-270}{\makebox(0,0){\strut{}$mu$}}}%
\put(3023,140){\makebox(0,0){\strut{}$epsilon$}}%
Notice how the \ is omitted? Thats frustrating when you try to automate things…
The solution is to escape the backslashes some more:
plot.ylabel "$\\\\mu$"
plot.xlabel "$\\\\epsilon$"
which produces the desired result.
Consulting the source one can find out why. Anyhow, I think I know my “client”, and the default x/y-label placement will not cut it – hence the following (the dataset is just mumbojumbo):
require "gnuplot"
filename = "2015_1"
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
plot.terminal "epslatex size 10cm,10cm standalone 10 monochrome dashed "
plot.output "./#{filename}.tex"
plot.lmargin 6
plot.bmargin 4
plot.unset 'border'
plot.unset 'tics'
plot.unset 'key'
x_min = 1
x_max = 500
y_min = 0
y_max = 70
plot.set "style arrow 1 head filled size screen 0.02,15,10 ls 1"
#explicit arrow on x-axis
plot.set "arrow from #{x_min} to #{x_max+10} as 1"
#explicit arrow on y-axis
plot.set "arrow from #{x_min},#{y_min} to #{x_min},#{y_max+2} as 1"
plot.xrange "[#{x_min}:#{x_max}]"
plot.yrange "[#{y_min}:#{y_max}]"
plot.title "Lorem ipsum"
# x-label
plot.label "'$\\varepsilon$' at #{x_max-20}, -5"
# y-label
plot.label "'$\\mu$' at -25, #{y_max-5}"
plot.data << Gnuplot::DataSet.new( "45*sin((x/8)**2)" ) do |ds|
ds.with = "lines"
ds.linewidth = 1
end
end
end
puts "created #{filename}.tex"
%x(rubber #{filename}.tex)
%x(dvips #{filename}.dvi -o #{filename}.ps)
%x(ps2eps #{filename}.ps -f)
%x(dvipdf #{filename}.dvi #{filename}.pdf)
%x(dvipng #{filename}.dvi -o #{filename}.png)
%x(gs -dNOPAUSE -q -dBATCH -sDEVICE=tiff24nc -r300 -sOutputFile=#{filename}.tiff #{filename}.ps)
Comments