[Tex/LaTex] psfrag substitute when using pdflatex

auto-pst-pdfpdftexpsfrag

My standard way of including math in figures so far was to use psfrag and latex+dvips+ps2pdf.

I am now moving to pdflatex and tend to prepare my graphics in pdf format. I was wondering if there exists a way to include math in pdf graphics similar to psfrag. I am aware of auto-pst-pdf package but prefer not to use it since on large projects it runs much slower than pdflatex and I am not sure if it can handle microtype as well as pdflatex.

Best Answer

Objectives and constraints

  • Extracting all EPS images imported in the main input file without modifying the main input file heavily.
  • Converting each of imported EPS images to PDF one and save it with its original file name.

Assumption

  • For the sake of best practice, I assume that you put all of your EPS images in a sub directory called Images. It means that the directory structure is defined as follows.

    other parents/project/Images/
    other parents/project/main.tex
    other parents/project/myextractor.sty
    

    You have to follow this convention as the remaining code uses this structure. Of course you can change this directory structure but you also need to modify the code a bit (not much). main.tex and myextractor.sty will be discussed shortly.

  • You are using Windows. If you are non-Windows users, please disabled the cleaning code mentioned in myextractor.sty.

  • You know that you must compile the main.tex with

    latex -shell-escape main
    dvips main
    
    ps2pdf -dAutoRotatePages#/None main.ps
    

Notes: For non-Windows users, replace # with =.

Step 1

Create a package called myextractor.sty as follows. Save it as mentioned in the directory structure above.

% myextractor.sty

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{myextractor}[2013/10/09 v0.01 LaTeX package for my own purpose]

\RequirePackage{filecontents}
\begin{filecontents*}{template.tex}
\documentclass[preview,border=0pt,graphics]{standalone}
\usepackage{graphicx}
\graphicspath{{Images/}}
% Active the following code (between \makeatletter and \makeatother) 
% if you want to cancel the effect of 
% width, height and/or scale defined in \includegraphics
%\makeatletter
%\define@key{Gin}{width}{}
%\define@key{Gin}{scale}{}
%\define@key{Gin}{height}{}
%\makeatother
\let\ea\expandafter
\begin{document}
%\edef\z{\noexpand\includegraphics[\varone]{\vartwo}}\z
\ea\includegraphics\ea[\varone]{\vartwo}
\end{document}
\end{filecontents*}

\RequirePackage{graphicx}
\RequirePackage{pgffor}

\let\temp\includegraphics

\renewcommand\includegraphics[2][]{%
    \temp[#1]{#2}%
    \immediate\write18{latex -jobname=#2 -output-directory=Images \unexpanded{"\def\varone{#1} \def\vartwo{#2} \input{template}"} && cd Images && dvips #2 && ps2pdf -dAutoRotatePages=/None #2.ps}%
    % disable the following if you are not Windows users.
    \foreach \ext in {dvi, ps, log, aux}{\immediate\write18{cd Images && cmd /c del #2.\ext}}%
}

\endinput

Read the comments given in the code carefully. They are as follows.

% Active the following code (between \makeatletter and \makeatother) 
% if you want to cancel the effect of 
% width, height and/or scale defined in \includegraphics
%\makeatletter
%\define@key{Gin}{width}{}
%\define@key{Gin}{scale}{}
%\define@key{Gin}{height}{}
%\makeatother

and

% disable the following if you are not Windows users.
\foreach \ext in {dvi, ps, log, aux}{\immediate\write18{cd Images && cmd /c del #2.\ext}}%

Step 2

Modify your main.tex as follows

% main.tex
\documentclass{book}
%\usepackage{graphicx}
\usepackage{myextractor}% automatically load graphicx
\graphicspath{{Images/}}


\begin{document}
\chapter{A}
\begin{figure}[hbtp]
\centering
\includegraphics[scale=.5]{A}
\caption{A}
\label{fig:A}
\end{figure}
A \ldots

\chapter{B}
\begin{figure}[hbtp]
\centering
\includegraphics[scale=.75]{B}
\caption{B}
\label{fig:B}
\end{figure}
B \ldots

\chapter{B}
\begin{figure}[hbtp]
\centering
\includegraphics[scale=1]{C}
\caption{C}
\label{fig:C}
\end{figure}
C \ldots

\end{document}

The important notes are

%\usepackage{graphicx}
\usepackage{myextractor}% automatically load graphicx
\graphicspath{{Images/}}
  • Load myextractor package before graphicx to prevent graphicx overrides myextractor definition. As myextractor loads graphicx internally, you actually can disable graphicx in main.tex.
  • \graphicspath must be specified as given above.

Step 3

Compile main.tex with latex-dvips-ps2pdf explained above. Afterwards, check Images folder, you will find a PDF version for each EPS image. Done!