Export Figures and Tables to Seperate Files Automatically

I saw a similar question few years ago here, however there is no effective answer.

The question is it possible to automatically export every figure and table in a specific document to separate files (with or without the figure/table caption). Following are a more specific list of requirements.

1. Export figures created with tikz/pgfplots, to a separate pdf files (with and without the caption, and to be numbered as in the document)
2. Export tables that uses tabular, tabularx, and longtable (with and without the caption)
3. When extracted is there a way to remove/hide (and its caption) the figure/table from the document while preserving references to that figure/table.

I have created a sample document below which contains a figure and a table so contributers can use it in their answers.

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{tabularx}

\title{example}
\author{John Doe}

\begin{document}
\maketitle
\begin{abstract}
\lipsum[1]
\end{abstract}

\section{a Section}
In Figure \ref{fig:one} \lipsum[1]
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\node(a)[draw, rectangle, ultra thick] at (0,0) {Test};
\node(b)[draw, rectangle, ultra thick] at (2,2) {Test};
\draw[-latex](a)--(b);
\end{tikzpicture}
\caption{Sample caption for figure 1}
\label{fig:one}
\end{figure}

\section{another Section}
\begin{table}
\begin{tabularx}{\textwidth}{llX}
\hline
colA&colB&colC \\
\hline
test&test&test\\
\hline
\end{tabularx}
\caption{Table A}
\label{table:A}
\end{table}
\end{document}

• Most of what you want could be done by endfloat and standalone, but you need to externalize to generate extra PDFs. – John Kormylo Feb 16 '18 at 15:26
• I tried endfloats but it just put the figures in the end of the document not separate file. Right? – NuminousName Feb 16 '18 at 15:28
• The file is there. It has an .fff extension. – John Kormylo Feb 16 '18 at 15:29
• I checked, there are two files (.fff for figures) and (.ttt for tables). The files include the raw latex code for the figures/tables. This not what i meant, i want it to be extracted as pdf. because the user will convert each of these figures manually to pdf it can be difficult process (imagine you have 50 figures and 50 tables). I want an automated process to have pdfs (e.g., fig1.pdf, table1.pdf, etc) – NuminousName Feb 16 '18 at 15:36
• You can't generate two PDFs with one LaTeX, unless you start another process to perform a completely different LaTeX job (which is what TikZ externalize does). What I meant was you could run \documentclass{standalome}[multi={figure}], although you would need to define a figure environment wrapper. – John Kormylo Feb 16 '18 at 15:45

This demonstrates how one could use standalone to process a .fff file.

The 5in width was a guess. You can use \the\textwidth to print what you should use for each document.

\begin{filecontents*}{test2.fff}
\begin{figure}
\centering
\includegraphics{example-image}
\caption{A figure without subfigures}
\end{figure}
\efloatseparator

\begin{figure}[htb]
\centering
\begin{subfigure}{.5\textwidth}
\centering
\includegraphics[width=.9\textwidth]{example-image}
\caption{first part}
\end{subfigure}%
%
\begin{subfigure}{.5\textwidth}
\centering
\includegraphics[width=.9\textwidth]{example-image}
\caption{(second part)}
\end{subfigure}%
\caption{A figure with subfigures}
\end{figure}
\efloatseparator
\end{filecontents*}

\documentclass[multi={figure,table}]{standalone}
\usepackage{graphicx}
\usepackage{subcaption}

\setlength{\textwidth}{5in}
\def\efloatseparator{\relax}
\makeatletter
\renewenvironment{figure}[1][]{\minipage{\textwidth}\def\@captype{figure}}{\endminipage}
\renewenvironment{table}[1][]{\minipage{\textwidth}\def\@captype{table}}{\endminipage}
\makeatother

\begin{document}
\input{test2.fff}
\end{document}