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.

  • You can't do this? \setcounter{figure}{<ORIGINAL NUM -1>}\begin{figure}....\end{figure} – touhami Feb 15 '16 at 7:45
  • I've managed to find a better (but still not ideal) solution using Adobe Acrobat Pro. Essentially I just load up my original document, delete the excess pages and crop the image. I haven't been able to find a latex equivalent. \includegraphics[page=x,trim=l b r t,clip]{file} looked hopeful, but this wouldn't display the image, just the text, so I'm still looking for a solution. – Ulysses Feb 15 '16 at 10:48
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – user36296 Nov 2 '16 at 21:58
  • None of the answers provided a viable solution, hence why I have not marked any as accepted. I have opted to instead use a non-Latex solution, detailed above. – Ulysses Nov 3 '16 at 8:58
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

  • 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 3:57
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.

  • 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 3:33
  • No, you can see tex.stackexchange.com/questions/291639/… – touhami Feb 16 '16 at 6:03

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.