6

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.

  • 1
    auto-pst-pdf is more recommended for running PSTricks with pdflatex and pstool is better recommended for psfrag with pdflatex. See Will Robertson's answer, author for both packages. – texenthusiast Nov 15 '13 at 14:37
  • Please see your questions. None has an accepted answer. It is not good practically to leave too many questions with no accepted answers as if no answer satisfies you and you don't leave any comment for improvement. You should reconsider which answer helps and satisfies you best and accept it. Accepting is simply clicking the check mark button below the score of any answer. – kiss my armpit Nov 16 '13 at 7:04
  • Thanks, I was not aware of this. I have accepted those responses that were satisfactory and will continue to do this. – per Nov 17 '13 at 4:18
7

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!

  • Thanks. I am looking for a way to run latex on the original to generate all the graphics in the document and save each in a filename.pdf file where filename is the name of the original eps file. Then I can include these pdf files in the original by deleting the eps extension. This way I do not have to run latex on each individual graphics. auto-pst-pdf does something like this but stores all pdf files in a single pdf file. Maybe a modification of it to store the graphics in individual pdf's is possible (or maybe already exists) – per Nov 15 '13 at 15:21
  • I have a latex file (root of a book) with each chapter \input'd and each chapter has many eps files with different names, many of them with psfrag fragments. Say the name of .eps files are 1.eps, 2.eps,.., 600.eps. Is there a way that I can generate 1.pdf, 2.pdf,...600.pdf easily, short of running lated+dvips+ps2pdf 600 times on each figure? – per Nov 15 '13 at 18:34
  • I tried it, it puts all the three pdf graphics in a single file not in three different files. What I want here are three files example-image-a.pdf, example-image-b.pdf and example-image-c.pdf. The way that your suggestion works is very similar to using auto-pst-pdf. I want each graphic to be saved in one pdf file. – per Nov 16 '13 at 16:39
  • @per: If you are using legacy Windows (older than Windows 7), use # instead of =. What is your OS? Answer has been edited. – kiss my armpit Nov 17 '13 at 8:06
  • It did not work as suggested, but after I removed the -dAutoRotatePages=/None option in ps2pdf, it works. But the main problem still remains. This technique generated pdf files for each .eps file but does not include the psfrag substitutions in the resulting pdf file. The alternative solution should read \includegraphics contents as well as all \psfrag contents that come between the corresponding \begin{figure} and \end{figure} and generate the pdf file accordingly. Then, in the final run, to generate main.pdf with pdflatex, all \psfrag commands should be removed. – per Nov 17 '13 at 8:42
0

I don't know if you intend to write LaTex on a huge amount of pictures, but I'm using Inkscape to do that. Basically when saving an image into .pdf format, there is a box to tick which allows for extracting all the texts and creates an ancillary tex file which places it automatically in the right place. In your .tex files, you replace \includegraphics{mypix.pdf} by \input{mypix.pdf_tex} (the file created by Inkscape).

Since it is just an open-and-save operation, I guess it may be possible to write some scripts to do it automatically on all files of a given folder

  • I don't know how this solves the problem. Could you show some example? – user31729 Dec 12 '14 at 15:37
  • @ChristianHupfer Assume you have a .pdf you intend to write latex on, you open your image with Inkspace, specify "load text as text". Then Inkspace recognises text fields. When you save your file again, you tick the box "pdf+Latex" and it creates a pdf without text and a tex files with all text fields, placed with commands such : \put(0.09714286,0.14448413){\rotatebox{90}{\makebox(0,0)[lb]{\smash{"the content of the text field"}}}}% Here Latex compiles the text, and you can modify it to put math... some more help here : [tug.ctan.org/tex-archive/info/svg-inkscape] – clemlaflemme Dec 15 '14 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.