# Extract floats and read an external aux file to get hyperref infos in captions

My purpose is to use the endfloat package to extract all figures (with their captions) in a separate pdf file. I want to do it with a script combining bash command line and emacs-lisp.

I want to do that because, in my copy editor work, I need to compare the figures (and their captions) of the accepted version with the formatted version ones. I know this is not the perfect solution (but I think it should works in most cases) so different/better ideas are welcome.

This is my MWE (the mydoc.tex file):

\documentclass[11pt]{article}
\pagestyle{empty}
\usepackage{graphicx}
\usepackage{blindtext}
,citecolor=blue

\begin{document}
\section{Section 1}
\label{section1}
\blindtext

\begin{figure}[t]
\centering
\includegraphics[width=.49\columnwidth]{example-image-a}
\caption{Dummy caption \cite{1} (see figure~\ref{fig3} in
section~\ref{section3}).\label{fig1}}
\end{figure}

\section{Section 2}
\label{section2}
\blindtext

\begin{figure}[t]
\centering
\includegraphics[width=.49\columnwidth]{example-image-b}
\caption{Dummy caption \cite{2} (see figure~\ref{fig1} in
section~\ref{section1}).\label{fig2}}
\end{figure}

\section{Section 3}
\label{section3}
\blindtext
\blindtext

\begin{figure}[t]
\centering
\includegraphics[width=.49\columnwidth]{example-image-c}
\caption{Dummy caption \cite{3} (see figure~\ref{fig2}
section~\ref{section2}).\label{fig3}}
\end{figure}

\begin{thebibliography}{9}

\bibitem{1}
Dummy bibibtem

\bibitem{2}
Dummy bibibtem

\bibitem{3}
Dummy bibibtem

\end{thebibliography}
\end{document}


1. First of all I want to generate and backup the .aux file. I'll compile with:

latexmk -pdf mydoc.tex; cp mydoc.aux mydoc-bk.aux

2. Then I'll compile with:

pdflatex "\nofiles"\
"\AtBeginDocument{\usepackage[figuresonly,nolists]{endfloat}}"\
"\nonstopmode"\
"\input{mydoc.tex}"


to generate the mydoc.fff file that contains:

\begin{figure}[t]
\centering
\includegraphics[width=.49\columnwidth]{example-image-a}
\caption{Dummy caption \cite{1} (see figure~\ref{fig3} in
section~\ref{section3}).\label{fig1}}
\end{figure}
\efloatseparator

\begin{figure}[t]
\centering
\includegraphics[width=.49\columnwidth]{example-image-b}
\caption{Dummy caption \cite{2} (see figure~\ref{fig1} in
section~\ref{section1}).\label{fig2}}
\end{figure}
\efloatseparator

\begin{figure}[t]
\centering
\includegraphics[width=.49\columnwidth]{example-image-c}
\caption{Dummy caption \cite{3} (see figure~\ref{fig2}
section~\ref{section2}).\label{fig3}}
\end{figure}
\efloatseparator


Now I create a new file: mynewdoc.tex (this is meant to be a temporary solution):

\documentclass[11pt]{article}
\pagestyle{empty}
\usepackage{graphicx}
\usepackage{blindtext}
,citecolor=blue

\def\efloatseparator{\clearpage}

\begin{document}

\input{mydoc.fff}

\end{document}


That I'll compile with:

pdflatex mynewdoc.tex


My question is: can I force (pdf)latex to compile using the backup .aux file so to get the correct refs (obviously without working links) in my final pdf?

I think that should be disabled the writing of the .aux file in some way because some of the labels are already defined in the external .aux file.

My solution. I resolved by renaming the .aux I want to be read in mynewdoc.aux and putting \nofiles in the preamble.

Alternative solution. I also found this alternative solution using the xcomment package. After generating the correct .aux file I will compile with:

pdflatex -jobname=mydoc \
"\RequirePackage{xcomment} \xcomment{figure}"\
"\nofiles"\
"\AtBeginDocument{\let\oldendfigure\endfigure"\
"\def\endfigure{\oldendfigure\clearpage}}"\
"\input{mydoc.tex}"


I'll have a warning becouse the \nofiles command does not take effect (can anybody help me with that?) but the generated pdf file will be ok.

• This should be something the xr package could do. – John Kormylo Jul 25 '17 at 2:50
• @JohnKormylo Thank you. I didn't know about xr package. Unfortunately it doesn't work because same of the labels defined in my new file (e.g. "fig1") are already defined in the external .aux file. Furthermore xr package doesn't handle \cite/\bibtem commands (I think). – Gabriele Nicolardi Jul 25 '17 at 5:36
• "some" of the labels... – Gabriele Nicolardi Jul 25 '17 at 5:50
• @GabrieleNicolardi: The xr package provides \externaldocument[foo:]{foofile} command, where foofile is an external .tex file that has been compiled already. You can use the same label names (if needed) in all files, say, fig:supernova and refer to it with \ref{fig:supernova} in the main document and \ref{foo:fig:supernova}, i.e. the foo: from the optional argument of \externaldocument macro is prepended with this prefix foo:. Basically, the prefix can be chosen arbitrarily – user31729 Jul 25 '17 at 17:54
• @GabrieleNicolardi: Your document was about \label and \ref, not about \cite, wasn't it? Anyway, see the package xcite for this – user31729 Jul 25 '17 at 18:44

Ok, I wrote two Bash scripts that do the same thing.

1. Using the endfloat package (I named it ExtractFiguresEndfloat.sh):

#!/bin/bash
TexFile=$1 latexmk -pdf$TexFile

pdflatex "\nofiles"\
"\AtBeginDocument{\usepackage[figuresonly,nolists]{endfloat}}"\
"\nonstopmode"\
"\input{$TexFile}" pdflatex "\def\efloatseparator{\clearpage}"\ "\nofiles"\ "\let\olddocument\document"\ "\renewcommand{\document}{\olddocument\input{\jobname.fff}\enddocument}"\ "\nonstopmode\input{$TexFile}"


usage:

ExtractFiguresEndfloat.sh file.tex

2. Using the xcomment package (I named it ExtractFiguresXcomment.sh):

#!/bin/bash
TexFile=$1 PREPRINT_ID=basename$TexFile .tex

latexmk -pdf $TexFile pdflatex -jobname=$PREPRINT_ID \
"\RequirePackage{xcomment} \xcomment{figure}"\
"\nofiles"\
"\AtBeginDocument{\let\oldendfigure\endfigure"\
"\def\endfigure{\oldendfigure\clearpage}}"\
"\nonstopmode"\
"\input{\$TexFile}"


usage:

ExtractFiguresXcomment.sh file.tex


I tested on a few files and they work (the first one has a limitation because endfloat wants the \end{figure}` string to be placed at line beginning...).

I get: