21

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.

  • Do you want only a single pdf with all figures? Or do you want a single pdf with all figures inserted as in the document, with captions and numbers and margins, etc...? – Sigur Oct 26 '13 at 17:05
  • @Sigur I think your second option includes the first one. but in my case both options are good. – sajjadG Oct 26 '13 at 17:16
  • 2
    I asked because: 1. if you use only pdf figures and 2. if you use linux then you can simply copy all figures to one tempo folder or rename them staring with fig and use the terminal to run pdftk fig*.pdf cat output all-figs.pdf for example. Thus you will have all pdf glued together. – Sigur Oct 26 '13 at 17:21
  • 3
    Make the tables in separate .tex files and include with \input{table1} between the main text and in a empty document. Is this way the main text is more readable for humans and the document only with tables is very easy to do. Of course, thi is also valid for for the figures. – Fran Oct 26 '13 at 18:03
  • 1
    @sajjadG. I agree with jon. If worth make subfiles even with a big document already well advanced. I take only a seconds search \begin{table}, add \input{tableXXX} just before, select until \end{table}, cut the whole float and paste in the new document tableXXX.tex. With a good editor may be you can automatize most of this procedure with a macro. – Fran Oct 26 '13 at 20:47
15

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!

  • More complex, but awesome. – Fran Oct 26 '13 at 23:19
  • @Fran: It is still not optimal. I will edit it later. – kiss my armpit Oct 26 '13 at 23:27
  • @Marienplatz I compiled your code and it works. But after I applied your code to my source ( \begin{filecontents*}{template.tex} \input{my_file.tex} \end{filecontents*}) it isn't working! it generates the whole document twice! Am I doing it wrong? – sajjadG Oct 27 '13 at 7:53
  • @sajjadG: Read my edit please. – kiss my armpit Oct 27 '13 at 8:11
  • 1
    @Marienplatz Thanks you very much. But I have a little problem with step4. step 3 works fine. but when I use \usepackage[active,tightpage,floats]{preview} to create a pdf of all the tables I get a pdf of all the tables and figures together in a single pdf file. and another problem is that tables are not centered! – sajjadG Oct 27 '13 at 9:21
4

Everything between \begin{Export}{<type>} and \end{Export} is written into an external file with name \jobname-<type>#.tex. This one is run from within the document and the created pdf is then used by \includegraphics. The <type> parameter is not really important, it simplifies only the handling if there are a lot of tabulars and/or figures. This example creates the external files

test.tex         the main document
test-tab1.tex
test-tab1.pdf
test-tab2.tex
test-tab2.pdf
test-fig1.tex
test-fig1.pdf

and, of course, the .log and .aux files, too. The command \newcommand\WritePreamble{... must be extended if you need other or more packages for your tabulars and figures.

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{graphicx}

\newcounter{extern}
\makeatletter
\renewcommand\FVB@VerbatimOut[1]{%
\@bsphack%
\begingroup
\FV@UseKeyValues%
\FV@DefineWhiteSpace%
\def\FV@Space{\space}%
\FV@DefineTabOut%
\def\FV@ProcessLine##1{%
\toks@{##1}\immediate\write\FV@OutFile{\the\toks@}}%
\immediate\openout\FV@OutFile #1\relax%
\WritePreamble%<<=== write preamble
\let\FV@FontScanPrep\relax
\let\@noligs\relax%
\FV@Scan}
\renewcommand\FVE@VerbatimOut{%<<=== write postamble
\WriteLine{\string\end{document}}% <<
\immediate\closeout\FV@OutFile\endgroup\@esphack}

\newcommand\WriteLine[1]{%
\begingroup%
\let\protect\@unexpandable@protect%
\edef\reserved@a{\immediate\write\FV@OutFile{#1}}%
\reserved@a%
\endgroup}
\makeatother

\newcommand\WritePreamble{%
\WriteLine{\string\documentclass[border=0pt]{standalone}}%
\WriteLine{\string\usepackage{array}}%
\WriteLine{\string\begin{document}}%
}

\newcommand\Export[1]{%
\stepcounter{extern}%
\gdef\externExt{#1}%
\VerbatimEnvironment%
\begin{VerbatimOut}{\jobname-#1\theextern.tex}}

\def\endExport{%
\end{VerbatimOut}
\IfFileExists{\jobname-\externExt\theextern.tex}
  {\typeout{>>>running pdflatex with file \jobname-\externExt\theextern.tex}%
   \immediate\write18{pdflatex --interaction=nonstopmode \jobname-\externExt\theextern}}
  {\fbox{Error in Export!}}%
\IfFileExists{\jobname-\externExt\theextern.pdf}
    {\includegraphics{\jobname-\externExt\theextern}}
    {\fbox{Error in running \jobname-\externExt\theextern.tex~ Try again}}%
}

\begin{document}

foo

\begin{Export}{tab}
\begin{tabular}{ccc}\hline
foo & bar & baz \\\hline
f   & b   & b   \\\hline
\end{tabular}
\end{Export}

bar

\begin{Export}{fig}
\setlength\unitlength{1cm}
\begin{picture}(3,3)
\put(1.5,1.5){\circle*{1}}
\end{picture}
\end{Export}

\begin{table}[!htb]
\centering
\caption{A caption of a tabular}
\begin{Export}{tab}
\begin{tabular}{ccc}\hline
f   & b   & b   \\\hline
foo & bar & baz \\\hline
\end{tabular}
\end{Export}
\end{table}

\end{document}

enter image description here

  • Thank you Herbert. If we want to use this method to make a separate file of figures and tables, we should go through the main file and place all the figures and tables between \begin{export} and \end{export} right? If this is the case, it's a good alternative for separating tables codes to files and \input then in the text. but for editing a long document, like \input it will take so much time to do this. – sajjadG Oct 27 '13 at 12:28
  • 1
    You do not need the \input command. The table/figure is included by default as a pdf image. – user2478 Oct 27 '13 at 16:19
  • This is a cool idea, but I ran into the problem that the page width was not carried over. – Geoff Jun 19 '15 at 20:49
  • 1
    @Geoff: modify the line \WriteLine{\string\documentclass[border=0pt]{standalone}}% to use the article class instead. And add more commands if you have a specific page setting. – user2478 Jun 20 '15 at 6:46
  • @Herbert: How to convert at the same time the PDFs to svg or png. I've changed the line you mentioned above to \WriteLine{\string\documentclass[border=0pt,convert=pdf2svg]{standalone}}% but no conversion is done. – Hamed Oct 2 '15 at 10:26
1

For little documents (articles or so) with all the figures and tables as floats, one possibility is to fix the rules for floats so that these rules cannot be accomplished. For example:

\renewcommand{\textfraction}{1.1} % that is 110% (too much)
\renewcommand{\floatpagefraction}{1}

So all the floats must move to the end of text. To return to floats placed within the text, simply comment these two lines.

A MWE to test that this approach could work:

\documentclass{article}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{mwe}
\title{A MWE}
\author{Fran}
\renewcommand{\textfraction}{1.1}
\renewcommand{\floatpagefraction}{1}
\begin{document}
\maketitle

\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]

% Table and figures come here 

\renewcommand{\textfraction}{1.1}
\renewcommand{\floatpagefraction}{1}

\end{document}

However, this surely will fail in big documents with many floats (typical "Too many unprocessed floats" error).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.