Changing file extensions in multiple LaTeX files

⇠ Back to Blog:Hacks

That's a pretty concrete problem: I have multiple .tex file which include .eps extensions. I have converted all the postscript (old fashioned) stuff into brand new converted pdf, now calling figure.eps by figure.eps.convto.pdf.

This piece of code says the $\mathrm{\TeX}$ files about that daring move:

perl -pi -w -e 's/\\includegraphics(\S+)pics\/(\S+)\}/\\includegraphics$1fig\/$2\.convto\.pdf\}/g;' *.tex

update on 09:38, 20 November 2014 (EST)

Now pdflatex doesn't like the extension .eps.convto.pdf (which in my mind was to mean "eps converted to pdf"). So I have to to keep only .pdf:

perl -pi -w -e 's/\\includegraphics(\S+)pics\/(\S+)\.e?ps\}/\\includegraphics$1fig\/$2\.pdf\}/g;' *.tex

(doing this for both extensions .eps and .ps by hand) and use the following bash conversion to replace the extension:

convert "$1" ""${1%%.eps}".pdf"

(here again, both for .eps and .ps). The latter is in a shell file called convertme which I call with:

find fig/ -type f -name *.eps -exec ./convertme {} \;

(here again, both for .eps and .ps by hand)

Note that this wasn't a problem before, where I had .ppm.eps or .png.eps files (which I had to convert in similar ways as above, of course), or even more legitimate UNIX filenames:

! LaTeX Error: Unknown graphics extension: .2.pdf.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.420 ...raphics[height=7cm]{fig/2/Figure2.2.pdf}}