# Processing images individually in Latex

Posted on 13 December 2013

When publishing a paper, it is often required to generate a single file for each image in the manuscript, which can be rather technically difficult when complicated figures are composed of separate plots or additional latex annotations have been added using overpic or similar. An example of a particularly complicated figure from a recent paper is

This figure would need stitching together somehow before submission. Previously I had done this by hand in Inkscape but for this paper I decided to try and write a script to automate things. The following script automatically locates figures in a latex document (here called mixing.tex) and renders them as separate latex documents using pdflatex. It then crops the padding using gs resulting in a sequence of pdf image files named figure01.pdf, figure02.pdf, etc. A new latex document is also produced (here mixing_pub.tex) where suitable \includegraphics{figure01} etc replace the figure latex in the original document.

This setup works well, and cuts out a lot of otherwise tedious work. It requires ghostscript and should work with any latex distribution (I’m using TexLive 2012 on Fedora 17.) You might need to add stuff to the preamble in the convert_image function if you use other packages.