Saturday, June 23, 2012

Generating latex-style figures using gnuplot

In a figure, it would be nice that the fonts and formula styles are consistent with the main text. For my case, the main text is in LaTeX, and the plot is generated using gnuplot. Here is how to:

In the  gnuplot script (or environment), after command plot, run:
call 'path/plottexpdf' 'figure_name'
where the script plottexpdf is
set terminal epslatex standalone color colortext
set termoption enhanced
set out "$0.tex"
replot
set term pop
! echo $0 > /tmp/plottexpdf.name
The last line just put figure_name to a file, for post processing shell script to refer to. The result is a .tex file, which can be compiled using latex or pdflatex to get the desired figure.

However, it is quite boring to compile and post process each time. Thus I wrote a shell script to do it. Instead of run "gnuplot name.gp", I run "plot name.gp", with a shell script plot as follows:
gnuplot $1

FN=$(cat /tmp/plottexpdf.name)

# modify the length of the dashed line
sed "s/\/LT\([0-9]\) {PL \[4 dl1 2 dl2\] LC1 DL} def/\/LT\1 {PL \[7 dl1 3 dl2\] LC1 DL} def/g" $FN-inc.eps > $FN-inc.eps.tmp

cp $FN-inc.eps.tmp $FN-inc.eps

# pdflatex $FN.tex

latex $FN.tex
dvipdfm $FN.dvi
rm -f $FN.dvi
rm -f $FN.log
rm -f $FN.aux
rm -f $FN-inc.eps.tmp

/usr/bin/pdfcrop $FN.pdf $FN.pdf
Note that the sed "s/.../.../g" is a single line. If you copy / paste, it may be broken into two lines. Fix that if you need.

No comments:

Post a Comment