Tags

Often you generate a graph using Gnuplot or something, and export it as an eps. This eps is then included in your LaTeX document and processed using psfrag and pdflatex (see my other post). A fine method, however, journals generally like you to include the final figure as a pdf graphic. I have written a script that does exactly this, it takes your PSfrag commands, and the eps figure, and turns it into a pdf figure.

This blog post is a guide to the script, and explains both how to use it and how it works.

Overview
There are a few stages to the process, first the script constructs a LaTeX file. Then the script processes this LaTeX file, with all the replacements applied. Then it extracts the pdf figure as the output. Finally it cleans up all the temporary files created.

Usage
The user must provide a LaTeX based input file containing the figure contents. This is standard LaTeX code when the packages graphicx, psfrag and pstricks are loaded. The users input file gets placed inside a postscript environment so the user can include any number of postscript commands, and they use the same figure, and the same coordinate system. For help on these packages see their respective documentation, PSfrag PStricks, graphicx.

Typical usage would just be a few commands of the form,

\psfrag{x}{$\theta$}
\psfrag{y}{$\sin(\theta)$}

followed by including a single eps file,

\includegraphics[width=85mm]{sine.eps}

possibly setting the size of the figure as well. There is a full example usage file at the end of this post.

The Input Files
The script then creates two temporary files to concatenate with the .texpart file to create the temporary .tex file. The contents of these files is as follows:

\documentclass{article}

\usepackage{auto-pst-pdf}
\usepackage{graphicx}
\usepackage{psfrag}
\usepackage{pstricks}

\begin{document}

\pagestyle{empty}

\begin{postscript}

for part 1, and,

\end{postscript}

\end{document}


for part 3, with part 2 being provided by the .texpart file.

This TeX file ensures that the user of the script can use any commands from PStricks or PSfrag, and any resizing commands that may be part of the graphicx package. The postscript environment ensures that anything the user includes is treated as part of the same figure, this is particularly useful when adding PStricks ‘rput’ objects on top of an included eps figure.

Processing the file
I use the methods that the auto-pst-pdf package uses as the way to extract just the figure. Auto-pst-pdf, which is an automated version of pst-pdf, does all the hard work by creating a seperate pdf file just for the pictures that contains one cropped picture on each page. As I ensure there is only one picture processed, this seperate file is exactly what I want the output to be.

The script runs pdflatex using this command,

pdflatex --shell-escape -interaction=batchmode full.temp.tex

Note the options, the --shell-escape is required by the auto-pst-pdf package, and -interaction=batchmode silences the main LaTeX run, and prevents it from stopping on errors or warnings.

The script then renames the file produced by auto-pst-pdf to the output name because this is the cropped pdf figure as required. This is done with,

mv full.temp-pics.pdf ${1%\.*}.pdf Note the parameter expansion ${1%\.*} which removes the last dot and anything after it, then adds .pdf instead.

Finally we delete all of the extra files created by this script using,

rm part1.temp part3.temp full.temp.tex full.temp.aux full.temp.log full.temp.pdf

Be warned that it doesn’t check modification times or anything, it will just remove any file in the current directory with these names.

The full script

#!/bin/bash

#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see .

# This program was written by Dominic Hosler
# There is a description of how it works on my blog https://dominichosler.wordpress.com
# Please ask on the blog if you have any questions, thank you.

# Script to take an input file containing LaTeX code for a figure.
# Input file should be plain Latex commands, including any psfrag, pstricks with a single /includegraphics.
# The script will then remove the files it created (apart from the pdf output file).

#Create temporary files for latex file
echo "Constructing LaTeX file"

echo "\documentclass{article}

\usepackage{auto-pst-pdf}
\usepackage{graphicx}
\usepackage{psfrag}
\usepackage{pstricks}

\begin{document}

\pagestyle{empty}

\begin{postscript}" > part1.temp

echo "\end{postscript}

\end{document}
" > part3.temp

cat part1.temp $1 part3.temp > full.temp.tex echo "Process the LaTeX file" pdflatex --shell-escape -interaction=batchmode full.temp.tex # Extract the figure as a pdf mv full.temp-pics.pdf${1%\.*}.pdf

#Cleanup temp files
echo "Cleanup temporary files"

rm part1.temp part3.temp full.temp.tex full.temp.aux full.temp.log full.temp.pdf

To use the above script you need to copy and paste it into a text file and make the file executable. Then to run, you need to have the file on your PATH variable or in the same directory.

Example usage file
This example file assumes a figure called sine.eps in the same directory. Some experimentation will be required for the coordinates of the PStricks put command.
The example file, example.texpart is this:

\psfrag{x}{$\theta$}
\psfrag{y}{$\sin (\theta)$}
\includegraphics[scale=0.7]{sine.eps}
\rput(-6,5){\scalebox{0.7}{maximum}}
\rput(-2,3){\scalebox{0.7}{$\theta=2 \pi$}}
Advertisements