# Using zpageref in includepdf

I wanted to extract the appendix from an external pdf document automatically by using \zpageref to get the page number specified by the label sec:appendix in paper.tex (the source of paper.pdf). However, the following code didn't work:

\documentclass{article}
\usepackage{pdfpages}
\usepackage{zref-xr,zref-user}
\zexternaldocument*{paper}
\begin{document}
\includepdf[pages=\zpageref{sec:appendix}-last]{paper.pdf}
\end{document}


I also followed the solution from a similar question as below but still got errors:

\documentclass{article}
\usepackage{pdfpages}
\usepackage{zref-xr,zref-user}
\zexternaldocument*{paper}
\newcommand{\eincludepdf}[1][]{%
\begingroup\edef\x{\endgroup\noexpand\includepdf[#1]}\x}
\newcommand{\pagenum}{\zpageref{sec:appendix}}
\begin{document}
\eincludepdf[pages={\pagenum}-last]{paper.pdf}
\end{document}


How to get this to work? Alternatively, are there any better solution to automate this particular task? Thanks.

You can do it with xr and refcount.

Here's the file steve-paper.tex:

\documentclass{article}

\usepackage{kantlipsum}

\begin{document}

\section{One}
\kant[1-20]
\section{Two}
\kant[21-40]

\clearpage
\appendix

\section{APP}\label{sec:appendix}
\kant[41-60]

\end{document}


And here's the file for the inclusion:

\documentclass{article}
\usepackage{pdfpages}
\usepackage{xr,refcount}

\externaldocument{steve-paper}

\begin{document}

\includepdf[pages=\getpagerefnumber{sec:appendix}-last]{steve-paper.pdf}

\end{document}

• Thanks, @egreg. It works like a charm! By the way, based on the suggestion, I find this might also be useful if someone wants to extract the pages before the appendix: \includepdf[pages=1-{\the\numexpr\getpagerefnumber{sec:appendix}-1}]{paper.pdf} – Steve L. May 24 '18 at 23:57