# Separating figures from a LaTeX document ![-](imgs/header.png) Some academic journals, like [PLoS One](https://journals.plos.org/plosone/s/figures), require that you submit a final document as a single self-contained LaTeX file. All figures need to be submitted as separate image files, with any figures having multiple panels merged into a single image. If you have a complex LaTeX source with a lot of `\include`s and figures, this is a pain to do by hand. It is possible to do this automatically. ## Merging the file into one `.tex` file Assume your paper is called `paper.tex` and currently has many `\include` or `\input` elements (e.g. `\include{intro.tex}`, `\include{theory.tex}`, ...) First: ``` latex paper.tex bibtex paper.tex ``` to generate `paper.bbl`. To make sure the bibliography is included in the source, change your source of `paper.tex` to replace `\bibliography{...}` with `\input{paper.bbl}`. Then use [latexpand](https://ctan.org/pkg/latexpand?lang=en) to include all `\input{}` and `\include{}` files, as well as the bibliography into one file. ``` latexpand paper.tex > paper_merged.tex ``` * Windows users using MikTex will first need to [install Perl](http://strawberryperl.com/) to get `latexpand` to work. ## Separating the figures The following is based on [this StackOverflow answer by John Kormylo](https://tex.stackexchange.com/questions/423109/export-each-figure-as-a-separate-pdf-file). We can use the `endfloat` package to split out all floats into a separate file. In the preamble of `paper_merged.tex`, just after `\documentclass`, add: ```latex \usepackage{endfloat} \usepackage{graphicx} \makeatletter \efloat@openpost{fff} \efloat@iwrite{fff}{\string\textwidth=\the\textwidth} \makeatother ``` Run `pdflatex paper_merged` and you will get a new file `paper_merged.fff` that has all the code for floating figures in it. ### The figure document Create a new, blank document `figures_separated.tex` with the following contents: ```latex \documentclass[multi=figure]{standalone} \usepackage{graphicx} \usepackage{amsmath} % packages I used in figures \usepackage{subfig} %% Include all custom macros used in figures here \renewenvironment{figure}{\ignorespaces}{\unskip} \renewcommand{\caption}[2][]{\ignorespaces} \newcommand{\Figure}[1]{\ignorespaces} \let\efloatseparator=\empty \newcommand{\titlecaption}[2]{} \begin{document} \input{paper_merged.fff} \end{document} ``` If you've used any custom macros or packages inside your floats, you'll need to include the relevant packages and add any of your own macros from `paper_merged.tex` into the preamble of `separated_figures.tex`. Then: ``` pdflatex separated_figures.tex ``` to produce `separated_figures.pdf` which has each figure as a separate page. #### Making TIFFs PLoS One recommends all figures to be submitted as TIFF (unfortunately EPS is the only vector format PLoS support and it is not recommended...). [imagemagick](https://imagemagick.org/script/download.php) can convert this multi-page PDF to individual TIFF files in one go -- if you configure it correctly! * Create a directory called `output_images` * Then, run: ``` magick convert -density 700 -scene 1 -background white -alpha remove \ -alpha off separated_figures.pdf "output_figures/fig%d.tiff" ``` * on Windows, use `%%d` instead of `%d` to avoid shell escaping * If you don't have [https://www.ghostscript.com/](GhostScript) installed, you need to install this first. You should now have one TIFF file per figure, named `output_images/fig1.tiff` etc. You may need to adjust `density` if the images are too high or too low resolution -- `density 700` looked about right for my configuration. That's it, you're done! John H Williamson [GitHub](https://github.com/johnhw) / [@jhnhw](https://twitter.com/jhnhw)