October 28, 2022

gnuplot and latex

I have two scripts (one of them is a silly one) to only use latex on the plots. You can download them from github:

Gnuplot.sh
GpTex.sh

Make sure you have these two scripts on a folder which is on your path. Just to give you an example, this is what I have on my .zshrc (the syntax is identical for .bashrc)

export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$HOME/bin

The main script is the second one, which contains this information in the header

# This script is meant to quickly and silently produce an eps
# from a File.tex and File.eps created with gnuplot. 
#
#  Usage:  GpTex.sh [-p] filename
#
#  -p  optional, to convert to pdf and crop the file.  
#      This runs epstopdf and pdfcrop if -p is present.
#
#  filename  -- input file.  Presumed to be filename.tex
#
#
# You have to use the epslatex in gnuplot. This is an
# example:
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# set terminal epslatex          <---                                     %
# set output "MyFile.tex"        <---                                     %
# set xlabel "Time ($10^9$ sec)"                                          %
# set ylabel "$e_{\\bullet}$"                                             %
# set mxtics 5                                                            %
# set mytics 5                                                            %
# set nokey                                                               %
#                                                                         %
# plot './orbital_elements.dat' u ($1/1e+9):2 ls 1 w l smooth csplines, \ %
#      'orbital_elements2.dat' u ($1/1e+9):2 ls 1 w l smooth csplines     %
#                                                                         %
# set terminal x11                                                        %
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#
# All latex symbols need to be protected with a second backslash: \\
#
# Write the above script as MyScript.gnuplot and run
# $ gnuplot MyScript.gnuplot
# This produces a .tex file, which is the argument of GpTex.sh
# 
# Pau Amaro Seoane, 20/09/2011, Berlin 
#
#  Note:
#  I've used redirect to /dev/null to quiet LaTeX and dvips output
#  To restore the noise, simply remove those redirects.

The second script is just doing what is explained in the header

# This is a dummy script which uses
# gnuplot and GpTex -p assuming that
# gnuplot exports always to out.tex
# to then open it with a pdf viewer

This way, you just have to run from your terminal

$ Gnuplot.sh YourScript.gnu

This will create the figure with latex and open a viewer to have a look at the outputted (and trimmed) pdf which, by default, is going to be called out.pdf.