5

I have a large document with several images, and I'd like to duplicate some of these images in individual pdf documents on their own, while preserving the way they display, figure numbering, and reference numbering of citations, tables, etc. So for example, if the figure number is 1.3 in the main document, it would still be 1.3 in the other document, despite the fact that figures 1.1 and 1.2 don't exist in the other document. Likewise for any reference in the caption, i.e.:

\caption{Something referencing Table \ref{tab:example} and citing \cite{examplecite}.}

would display as "Figure 1.3: Something referencing Table 1.2 and citing [15]." in both documents, despite the absence of Table 1.1, or citations 1-14 in the latter document.

A very manual way to achieve this would be to duplicate my preamble in a new tex document, copy over only the relevant figure, then replace instances of \cite{Example}, etc, with its actual number so as to preserve the numbering, then use this to generate the pdf that I'm after. This would work, but it's a terrible solution.

I'd like to know how to best go about this. I'm just after ideas of things to try, not a fully coded working example, as I don't know how to make a MWE for this.

Ulysses
  • 1,933

2 Answers2

4

You want to try xr. This is a schematic “long” file, say ulysseslong.tex:

\documentclass{article}

\begin{document}

\section{Test}\label{sec:test}

\begin{figure}[htp]
\centering
\fbox{\rule{0pt}{3cm}\rule{3cm}{0pt}}
\caption{A caption\label{fig:A}}
\end{figure}

\begin{figure}[htp]
\centering
\fbox{\rule{0pt}{3cm}\rule{3cm}{0pt}}
\caption{B caption\label{fig:B}}
\end{figure}

\begin{figure}[htp]
\centering
\fbox{\rule{0pt}{3cm}\rule{3cm}{0pt}}
\caption{C caption with a reference to Section~\ref{sec:test}\label{fig:C}}
\end{figure}

\begin{figure}[htp]
\centering
\fbox{\rule{0pt}{3cm}\rule{3cm}{0pt}}
\caption{D caption\label{fig:D}}
\end{figure}

\begin{table}[htp]
\centering
\caption{A table caption\label{tab:A}}
\medskip
\begin{tabular}{cc}
a & b \\
c & d
\end{tabular}
\end{table}

\begin{table}[htp]
\centering
\caption{B table caption\label{tab:B}}
\medskip
\begin{tabular}{cc}
a & b \\
c & d
\end{tabular}
\end{table}

\end{document}

Here is the “short” version, say ulyssesshort.tex:

\documentclass{article}

\usepackage{xr}
\externaldocument{ulysseslong}

\makeatletter
\newcommand{\extref}[1]{%
  \@namedef{the\@captype}{\ref{#1}}%
}
\makeatother

\begin{document}

\begin{figure}[htp]
\centering\extref{fig:C}
\fbox{\rule{0pt}{3cm}\rule{3cm}{0pt}}
\caption{C caption with a reference to Section~\ref{sec:test}}
\end{figure}

\begin{figure}[htp]
\centering\extref{fig:D}
\fbox{\rule{0pt}{3cm}\rule{3cm}{0pt}}
\caption{D caption}
\end{figure}

\begin{table}[htp]
\centering\extref{tab:B}
\caption{B table caption}
\medskip
\begin{tabular}{cc}
a & b \\
c & d
\end{tabular}
\end{table}

\end{document}

Provided all cross references in ulysseslong.tex are solved, this is the output of processing ulyssesshort.tex:

enter image description here

egreg
  • 1,121,712
  • Thanks for your help. I'm struggling to correctly implement this though. For some reason I can't get \ref to give me anything other than ??, and I can't seem to get citations in the captions to work either. This solution addresses the numbering issue (or probably will once I get it working), but still requires me to copy and paste over the entire preamble and image code manually, so I'm going to work on automating that. From the sound of things though this may be a problem that I'll just have to file away in the 'too hard' basket. I at least have the Acrobat Pro solution to fall back on. – Ulysses Feb 16 '16 at 03:57
  • I have posted a question about how to implement this with non-floating figures and tables, using the cleveref type, here – Rico Picone Aug 04 '23 at 22:37
3

If i understand correctly, one can do some thing like this:

Main document

\documentclass{article}
\usepackage{mwe} % just for the example


\let\mtincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
\mtincludegraphics[#1]{#2}%
\immediate\write\mt{\string\mtfigureinserted{#2}{\thefigure}}}

\newwrite\mt
\AtBeginDocument{\immediate\openout\mt=\jobname.img}
\AtEndDocument{\immediate\closeout\mt}

\begin{document}
\lipsum[1-2]
\begin{figure}
\includegraphics{example-image-a}
\caption{example-image-caption}
\label{imga}
\end{figure}
\lipsum[1-2]
\begin{figure}
\includegraphics{example-image-b}
\caption{example-image-caption}
\label{imgb}
\end{figure}
\lipsum[1-2]
\begin{figure}
\includegraphics{example-image-c}
\caption{example-image-caption}
\label{imgc}
\end{figure}
\end{document}

This will write informations to external file mainfilename.img New files

\documentclass{article}
\usepackage{mwe}

\newcommand*{\myimage}{}
\newcommand*{\myfile}{example-image-c}
\newcommand*{\mtfigureinserted}[2]{%
\renewcommand*\myimage{#1}%
\ifx\myimage\myfile
\setcounter{figure}{#2}%
\fi}
\input{newtest.img}

\begin{document}
\lipsum[1-2]
\begin{figure}
\includegraphics{\myfile}
\caption{example-image-caption}
\label{imga}
\end{figure}
\end{document}

One need only to set \newcommand*{\myfile}{example-image-c}.

\mtfigureinserted will set the counter figure to the correct value.

touhami
  • 19,520
  • If my caption is something like \caption{Something referencing Table \ref{tab:example} and citing \cite{examplecite}.} is it possible to get this to display the right numbers with this method? – Ulysses Feb 16 '16 at 03:33
  • No, you can see http://tex.stackexchange.com/questions/291639/how-to-print-out-only-statements-theorems-lemmata-etc-from-a-tex-file/291682#291682 – touhami Feb 16 '16 at 06:03