[Tex/LaTex] Generating a separate pdf file of tables and figures from a latex file

floatspdfpdftextable of contentstables

Is it possible to generate a PDF of figures and tables of a paper from LaTeX itself?

I saw Journals and Conferences asking for a separate file of tables and figures.

In this question, questioner proposed a method that uses \usepackage{endfloat} to push all the figures and tables to the last pages and then use a PDF editor to cut the last pages of PDF. This is a solution but not a good one. LaTeX should be able to do this by its own.

What I have in mind is to compile the LaTeX file and it generates a complete PDF output and beside that a separate file that contains all the figures and tables of former PDF.

Best Answer

The following code will extract all graphics and floats. To prove it, I load the extracted objects again with \includepdf (see the animation).

% this file name is extractor.tex
% compile it with pdflatex -shell-escape extractor
\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{template.tex}
\documentclass{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{mwe}
\usepackage[active,tightpage,\placeholder]{preview}
%\PreviewEnvironment{\placeholder}
\PreviewBorder=12pt\relax


\begin{document}


\lipsum[1]

\begin{table}[htb]
\centering
\begin{tabular}{|lll|}
\hline
    11 & 12 & 13\\
    21 & 22 & 23\\
    31 & 32 & 33\\
    41 & 42 & 43\\
    51 & 52 & 53\\
\hline
\end{tabular}
\caption{A table}
\end{table}


\begin{figure}[htp]
\includegraphics{example-image-a}
\caption{A}\label{a}
\end{figure}

\lipsum[6-10]

\begin{figure}[htp]
\includegraphics{example-image-b}
\caption{A}\label{a}
\end{figure}

\begin{figure}[htp]
\includegraphics{example-image-c}
\caption{A}\label{a}
\end{figure}

\lipsum[16-20]


\end{document}
\end{filecontents*}

\usepackage{pgffor,pdfpages}

\begin{document}

\foreach \x in{graphics,floats}{%
    \immediate\write18{pdflatex -jobname=template-\x\space "\def\noexpand\placeholder{\x} \noexpand\input{template}"}%
    \includepdf[pages=-]{template-\x}%
}

\end{document}

enter image description here

The code above just simulates your scenario. To apply it in your real scenario, do the following.

Step 1

Assume that your input file is as follows.

\documentclass{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{mwe}

\begin{document}

\lipsum[1]

\begin{table}[htb]
\centering
\begin{tabular}{|lll|}
\hline
    11 & 12 & 13\\
    21 & 22 & 23\\
    31 & 32 & 33\\
    41 & 42 & 43\\
    51 & 52 & 53\\
\hline
\end{tabular}
\caption{A table}
\end{table}

\begin{figure}[htp]
\includegraphics{example-image-a}
\caption{A}\label{a}
\end{figure}

\lipsum[6-10]

\begin{figure}[htp]
\includegraphics{example-image-b}
\caption{A}\label{a}
\end{figure}

\begin{figure}[htp]
\includegraphics{example-image-c}
\caption{A}\label{a}
\end{figure}

\lipsum[16-20]

\end{document}

Step 2

Insert

\usepackage[active,tightpage,graphics]{preview}
\PreviewBorder=12pt\relax

in your input file. So your input file becomes as follows.

\documentclass{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{mwe}
\usepackage[active,tightpage,graphics]{preview}
\PreviewBorder=12pt\relax

\begin{document}

\lipsum[1]

\begin{table}[htb]
\centering
\begin{tabular}{|lll|}
\hline
    11 & 12 & 13\\
    21 & 22 & 23\\
    31 & 32 & 33\\
    41 & 42 & 43\\
    51 & 52 & 53\\
\hline
\end{tabular}
\caption{A table}
\end{table}

\begin{figure}[htp]
\includegraphics{example-image-a}
\caption{A}\label{a}
\end{figure}

\lipsum[6-10]

\begin{figure}[htp]
\includegraphics{example-image-b}
\caption{A}\label{a}
\end{figure}

\begin{figure}[htp]
\includegraphics{example-image-c}
\caption{A}\label{a}
\end{figure}

\lipsum[16-20]

\end{document}

Save this input file as anyfilename-graphics.tex

Step 3

Invoke pdflatex anyfilename-graphics to obtain a PDF file containing all extracted graphics.

Step 4

Repeat Step 2 but replace graphics with floats, save the file as anyfilename-floats.tex. Now compile with pdflatex anyfilename-floats to get a PDF file containing all extracted floats (figure or table).

Step 5

Done!

Related Question