1

The solution for including multiple pdf pages in your main document discussed at https://tex.stackexchange.com/a/125212/41856 works for me, however, the code listed there, and below, will not reference test.pdf as a figure in the main text as Figure (1) below, but by Figure (22), or however many pages are in test.pdf

\getpdfpages{test.pdf}
\foreach \x in {1,...,\value{pdfpages}} {
% your code for page \x of t.pdf goes here
\begin{table}[htpb]    \caption{Figure 1}  \label{fig:Figure 1}
 \centering
\includegraphics[page=\x,width=1\textwidth]{test.pdf}
 \end{table} }
\clearpage

(\ref{fig:Figure 1})

The prior \ref commands yields Figure (22), or however many pages are in test.pdf, in the main document when I would like it to be Figure (1).

How can the above code be modified to increment each \ref in the document by 1 and not the number of pages in test.pdf?

This is even more important when you are including more documents, example test1.pdf, test2.pdf, et cetera in the main text.

  • 1
    Change Figure 1 in the caption with Figure \x and similarly in the label. – egreg Nov 28 '13 at 15:33
  • This did not work since how can the command (\ref{fig:Figure 1}) in the above code reference \label{\x} ? – user110261 Nov 28 '13 at 16:06
3

This works for me:

\documentclass{article}
\usepackage{graphicx}
\usepackage{pgffor}

\newcounter{pdfpages}
\newcommand*{\getpdfpages}[1]{%
  \begingroup
    \sbox0{% 
      \includegraphics{#1}%
      \setcounter{pdfpages}{\pdflastximagepages}%
    }%
  \endgroup
}

\begin{document}
\getpdfpages{gptest.pdf}
\foreach \x in {1,...,\value{pdfpages}} {
  \begin{figure}[htpb]
  \caption{Figure \x}\label{fig:Figure \x}
  \centering
  \includegraphics[page=\x,width=1\textwidth]{gptest.pdf}
  \end{figure}
}

\clearpage

\ref{fig:Figure 2}

\end{document}

It's important that \getpdfpages is issued before the first usage of the file in some other \includegraphics command, or it won't work. A good place for it might be in the document preamble. (Thanks to Stephan Lemke for noticing.)

  • Your coding \ref{fig:Figure 2} explicitly hardcodes the link to Figure 2. (defeats the purpose of using references in LaTeX, particularly when one wishes to add additional figures in the document later on. My question regards how one might use a global reference to a figure generated using the code: – user110261 Nov 30 '13 at 0:42
  • \getpdfpages{test.pdf} \foreach \x in {1,...,\value{pdfpages}} { % your code for page \x of t.pdf goes here \begin{table}[htpb] \caption{Figure 1} \label{fig:Figure 1} \centering \includegraphics[page=\x,width=1\textwidth]{test.pdf} \end{table} } \clearpage when generated in a loop! – user110261 Nov 30 '13 at 0:50
  • Furthemore, the \x command increments and labels each page of test.pdf by 1, so \x has nothing to do with my original question. Please read original question again. The loop in the program above, autoincrements the figure # by the number of pages in the loop. I need a solution that increments the figure number by unity. – user110261 Nov 30 '13 at 4:15
  • @user110261 Sorry, but I don't understand. Can you please edit your question showing better what you're trying to achieve? – egreg Nov 30 '13 at 11:06
  • Note that it is risky to assume that \pdflastximagepages will work after \includegraphics. It will not always issue a \pdfximage command. – Stephan Lehmke Dec 3 '13 at 14:23
1

A light weight approach. The #1 being a \numexpr, it is prefixed by \the where its value must be printed.

ABC

\documentclass{article}
%\usepackage{graphicx}% not needed here
\usepackage{xinttools}% in TeX installations updated 2013/11/22 or later
\begin{document}
\xintFor #1 in \xintintegers \do {% #1 is a \numexpr <digits>\relax
  \begin{figure}[htpb]
  \caption{Figure \the#1}\label{fig:Figure \the#1}
  \centering
    \mbox {\pdfximage width .2\textwidth page #1 {testgraph.pdf}%
           \pdfrefximage\pdflastximage }%
% or with graphicx
%    \includegraphics [page=#1,width=.2\textwidth]{testgraph.pdf}
  \end{figure}
\ifnum\pdflastximagepages=#1\expandafter\xintBreakFor\fi
}
\end{document}

Code for production of the pdf file testgraph.pdf with 3 pages:

\documentclass[multi=test,crop,border=1pt]{standalone}
\begin{document}
\begin{test}%
A%
\end{test}
\begin{test}%
B%
\end{test}
\begin{test}%
C%
\end{test}
\end{document}
0

My original question involved how can one reference, using the command (\ref{fig:Figure 1}), a multipage pdf (e.g. test.pdf that is say, 22 (or any number) of pages long) to be included at the end of your document.

The "trick" is to label test1.pdf outside the \foreach loop so that the reference to test1.pdf is NOT incremented by the loop as in the following code which includes three different length pdf files at the end of a document:

\section{Exhibit \ref{figure:Exhibit 1}} \label{figure:Exhibit 1}
\getpdfpages{test1.pdf}
%test1.pdf is 22 pages long
\foreach \x in {1,...,\value{pdfpages}} {
% your code for page \x of t.pdf goes here
\begin{figure}[H]  \caption{Exhibit \ref{figure:Exhibit 1}}
 \centering
\includegraphics[page=\x,width=1.1\textwidth]{test1.pdf}
\end{figure}}
\clearpage

\section{Exhibit \ref{figure:Exhibit 2}} \label{figure:Exhibit 2}
\getpdfpages{test2.pdf}
%test2.pdf is 17 pages long
\foreach \x in {1,...,\value{pdfpages}} {
% your code for page \x of t.pdf goes here
\begin{figure}[H]  \caption{Exhibit \ref{figure:Exhibit 2}}
 \centering
\includegraphics[page=\x,width=1.1\textwidth]{test2.pdf}
\end{figure}}
\clearpage

\section{Exhibit \ref{figure:Exhibit 3}} \label{figure:Exhibit 3}
\getpdfpages{test3.pdf}
%test3.pdf is 37 pages long
\foreach \x in {1,...,\value{pdfpages}} {
% your code for page \x of t.pdf goes here
\begin{figure}[H]  \caption{Exhibit \ref{figure:Exhibit 3}}
 \centering
\includegraphics[page=\x,width=1.1\textwidth]{test3.pdf}
\end{figure}}
\clearpage

Now we can reference (really these are referenced as sections) any of these "figures" from within the LaTeX document, and we can easily add more figures with an arbitrary number of pages, at will, since they are now correctly incremented by 1 (unity). There are probably other ways to do this as well...the trick was to label the "figure" outside the loop, not within it!

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.