## Saturday, February 13, 2010

### includegraphics with pdflatex and filenames with dots

I never truly understood how to use the includegraphics directive when writing in LaTeX, and maybe because of that I've always had problems in that regard. I still do not understand anything about it, but now I've found the solution to a couple of problems.
1. Compiling latex documents with pdflatex (without converting/renaming .eps files)
(source: http://kile.sourceforge.net/Documentation/html/build_epsgraphics.html, and many others)

Most of the time I'm sharing a LaTeX document with other authors, that compile it with latex, but since I use TexShop and compile with pdflatex it has become a problem to manage the graphics inclusions. So I'd like to compile with pdflatex a document that contains .eps images without changing the includegraphics directive nor converting explicitly the image to .pdf, .png or .jpeg.

The epstopdf package handles this situation, by adding a couple of lines to the preamble and compiling with the option 'pdflatex --shell-escape' (this is a default setting in TexShop)

\usepackage{graphicx}

\usepackage{ifpdf}
\ifpdf
\DeclareGraphicsRule{.eps}{pdf}{.pdf}{`epstopdf #1}
\usepackage{epstopdf}
\epstopdfsetup{suffix=-\SourceExt-converted-to}
\pdfcompresslevel=9 % 0: no compression
\fi
...
\includegraphics[width=0.2\textwidth]{fig/qwerty.eps}

this will automatically convert each .eps file to .pdf during the compilation with pdflatex.

2. Handling filenames with dots
(source: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn)

Suppose you want to include a graphics file home.bedroom.eps using the dvips driver; the package will conclude that your file’s extension is .bedroom.eps, and will complain.

The latest
grffile package deals with the last problem (and others — see the package documentation); using the package, you may write:

\usepackage{graphicx}
\usepackage{grffile}
...
\includegraphics{home.bedroom.eps}

or you may even write

\includegraphics{home.bedroom}

and graphicx will find a .eps or .pdf (or whatever) version, according to what version of (La)TeX you’re running.

In conclusion combining these changes it is possible to include .eps images in the document and compile with pdflatex or latex without any special treatment. If necessary the conversion of the file is done automatically by epstopdf, even if the extension is not specified in the includegraphics directive.
My preamble looks like this (ATTENTION I'm using epstopdf package 2010/02/09 v2.5) :

\usepackage{graphicx} % allows for inclusion of EPS files

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Running 'pdflatex --shell-escape...' will automatically recognize
% the eps files in the includegraphics and convert them to pdf.
% No file rename nor change to the document is needed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{ifpdf}
\ifpdf
\DeclareGraphicsRule{.eps}{pdf}{.pdf}{`epstopdf #1}
\usepackage{epstopdf}
\epstopdfsetup{suffix=-\SourceExt-converted-to}
\pdfcompresslevel=0 %\pdfcompresslevel=9
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This line loads the latest grffile.sty : 2010/01/28 v1.11
% Multidot improves the parsing of the filenames and in includegraphics.
% The extension of a file (ej kk.c1.eps), can be omitted in the
% includegraphics directive (ej kk.c1) and the compiler will guess the
% extension based on the known file types for the current mode.
% To change the preferred extension order use grfext
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[multidot]{grffile}