我想要引用所含 PDF 文档的最后一页。
\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\label{pg:mydoc}
\includepdf[pages=-]{mydoc.pdf}
The included pdf is on pages
\pageref{pg:mydoc} -- x.
\end{document}
x 应该是 PDF 的最后一页。我有一个解决方案,但我想知道是否有更简单的解决方案。
\documentclass{article}
\usepackage{pdfpages,refcount}
\newcommand*{\pdfnumberofpages}[1]{%
\directlua{%
local doc = epdf.open("\luatexluaescapestring{#1}")
local pages
if (doc) then
pages = doc:getCatalog():getNumPages()
else
pages = 0
end
tex.write(pages)
}%
}
\newcommand\pagerefoffset[2][0]{%
\edef\tmp{\getpagerefnumber{#2}}%
\the\numexpr#1+\tmp\relax}
\begin{document}
\label{pg:mydoc}
\includepdf[pages=-]{mydoc.pdf}
The included pdf is on pages
\pageref{pg:mydoc} -- \pagerefoffset[\pdfnumberofpages{mydoc.pdf}-1]{pg:mydoc}.
\end{document}
答案1
您可以尝试类似这样的方法。我认为它应该也可以与更复杂的pages
选项一起使用。它假设文档仅包含一次(如果不是,您将获得多个标签)。
\documentclass[12pt]{article}
\usepackage{pdfpages}
\usepackage{etoolbox}
\makeatletter
\patchcmd\AM@output{\setboolean{AM@endoflist}{false}}
{\newpage
\label{start-\AM@currentdocname}%
\setboolean{AM@endoflist}{false}}
{}{\fail}
\patchcmd\AM@output{\global\let\@deferlist\AM@deferlist}
{\global\let\@deferlist\AM@deferlist
\label{stop-\AM@currentdocname}}
{}{\fail}
\makeatother
\begin{document}
The included pdf is on pages
\pageref{start-test-ansinew.pdf} -- \pageref{stop-test-ansinew.pdf}
\includepdf[pages={1-2,4}]{test-ansinew}
\end{document}
答案2
这是一个解决方案
\documentclass{book}
\usepackage{pdfpages}
\usepackage{hyperref}
\newcounter{mt}
\begin{document}
last page is \pageref{mydoc} and last page \pageref{yourdoc}
\includepdf[pages=-, pagecommand={%
\refstepcounter{mt}%
\ifnum\value{mt}=\the\pdflastximagepages
\phantomsection% if hyperref
\label{mydoc}
\fi}]{a}
\setcounter{mt}{0}
\includepdf[pages=-, pagecommand={%
\refstepcounter{mt}%
\ifnum\value{mt}=\the\pdflastximagepages
\phantomsection% if hyperref
\label{yourdoc}
\fi}]{a}
\end{document}