使用 cleveref 和 pdfpages 交叉引用单个页面

使用 cleveref 和 pdfpages 交叉引用单个页面

我尝试实现的是将按部分组织的多页 pdf 插入到附录中。

插入.pdf文件的方式正如我希望的那样。

此外,我希望每个插入的页面都贴上标签,但存在一个问题。

以下是 MWE:

\documentclass{article}

\usepackage{polyglossia}    %multilingual typesetting
    \setdefaultlanguage{english}

\usepackage{cleveref}
\usepackage{appendix}
\usepackage[unicode=true, colorlinks=true]{hyperref}
\usepackage{pdfpages}



 \newcounter{includepdfnumber}  %create counter for labeling every inserted pdf
 \newcounter{pdfpagenumber} %create counter for labeling every inserted pdf-page
 \newcounter{startpage} %create counter for incrementing startpage

 \newcommand{\includepdfwithlabel}[6]{% usage: \includepdfwithlabel{'startpage'}{'endpage'}{\chapter,\section...}{additional arguments}{'labelname'}{'filename'}
    \setcounter{pdfpagenumber}{0}
    \refstepcounter{includepdfnumber}           
    \setcounter{startpage}{#1}  %set startpage to argument #1
    \includepdf[pages=\thestartpage,#4,link,linkname=#5,pagecommand={#3\stepcounter{pdfpagenumber}\label{#5.\thepdfpagenumber}\large{#5-\theincludepdfnumber.\thepdfpagenumber}}]{#6} %
    \addtocounter{startpage}{1} %
    \includepdf[pages=\thestartpage-#2,#4,link,linkname=#5,pagecommand={\stepcounter{pdfpagenumber}\label{#5.\thepdfpagenumber}\large{#5-\theincludepdfnumber.\thepdfpagenumber}}]{#6} %

 }


 \begin{document}


    as seen in \cref{pdf1.1}\\
    as seen in \ref{pdf1.1}\\
    as seen in \cref{pdf1.2}\\
    as seen in \ref{pdf1.2}\\
    \\
    as seen in \cref{pdf2.1}\\
    as seen in \ref{pdf2.1}\\
    as seen in \cref{pdf2.2}\\
    as seen in \ref{pdf2.2}\\

    \newpage

    \begin{appendices}

    \includepdfwithlabel{1}{4}{\section{PDF A}}{height=0.6\textheight}{pdf1}{test.pdf}
    \includepdfwithlabel{1}{4}{\section{PDF B}}{height=0.6\textheight}{pdf2}{test.pdf}

    \end{appendices}

 \end{document}

\large{#5-\theincludepdfnumber.\thepdfpagenumber}

部分仅用于调试。

我编译时得到的结果是:

as seen in ??
as seen in A
as seen in ??
as seen in 1
as seen in ??
as seen in B
as seen in ??
as seen in 2

因此\ref至少可以起作用,如果仍然不正确,则\cref根本无法执行任何操作。

我希望它的方式是:

as seen in Appendix A, page 1
as seen in Appendix A, page 2
.
.
.

有人可以帮帮我吗?

答案1

zref请参阅底部的(更好的?)方法。

这里有几个错误:

  1. cleveref应该在之后加载hyperref,而不是在中间某个地方
  2. 选项\label中的调用必须以 为前缀。 要么是,要么是,前者会导致相同的标签,而这将是“错误的”(或者更好的说法是:无用的)pagecommand\includepdf\refstepcounter\refstepcounter{includepdfnumber}\stepcounter{pdfpagenumber}
  3. cleveref需要参考哪个名称的信息,即应该用什么来打印\cref或者\Cref何时使用标签pdfpagenumber,因此使用\crefname\Crefname来告知cleveref这一点。

\documentclass{article}

\usepackage{polyglossia}    %multilingual typesetting
    \setdefaultlanguage{english}

\usepackage{pdfpages}

\usepackage[unicode=true, colorlinks=true]{hyperref}
\usepackage{appendix}

\usepackage{cleveref}



\newcounter{includepdfnumber}  %create counter for labeling every inserted pdf
\newcounter{pdfpagenumber} %create counter for labeling every inserted pdf-page
\newcounter{startpage} %create counter for incrementing startpage


\crefname{pdfpagenumber}{file page}{file pages}
\Crefname{pdfpagenumber}{File page}{File pages}


\newcommand{\includepdfwithlabel}[6]{% usage: \includepdfwithlabel{'startpage'}{'endpage'}{\chapter,\section...}{additional arguments}{'labelname'}{'filename'}
  \setcounter{pdfpagenumber}{0}
  \refstepcounter{includepdfnumber}           
  \setcounter{startpage}{#1}  %set startpage to argument #1
  \includepdf[pages=\thestartpage,#4,link,linkname=#5,pagecommand={#3\refstepcounter{pdfpagenumber}\label{#5.\thepdfpagenumber}\large{#5-\theincludepdfnumber.\thepdfpagenumber}}]{#6} %
  \addtocounter{startpage}{1} %
  \includepdf[pages=\thestartpage-#2,#4,link,linkname=#5,pagecommand={\refstepcounter{pdfpagenumber}\label{#5.\thepdfpagenumber}\large{#5-\theincludepdfnumber.\thepdfpagenumber}}]{#6} %

}


\begin{document}
as seen in \cref{pdf1.1}

as seen in \ref{pdf1.1}

as seen in \cref{pdf1.2}

as seen in \ref{pdf1.2}

as seen in \cref{pdf2.1}

as seen in \ref{pdf2.1}

as seen in \cref{pdf2.2}

as seen in \ref{pdf2.2}


\begin{appendices}

  \includepdfwithlabel{1}{4}{\section{PDF A}}{height=0.6\textheight}{pdf1}{7.pdf}
  \includepdfwithlabel{1}{4}{\section{PDF B}}{height=0.6\textheight}{pdf2}{7.pdf}

\end{appendices}

\end{document}

带 zref 的版本

首先,存储部分属性\zlabel,并且宏\appref提取页面引用来自哪个附录的信息。

为了创建正确的超锚点,\theHpdfpagenumber宏也必须重新定义以使用正确的附录编号。

由于无论如何都会在这里使用,因此我省略了是否完全加载的hyperref测试。hyperref

\documentclass{article}

\usepackage{polyglossia}    %multilingual typesetting
\setdefaultlanguage{english}

\usepackage{pdfpages}

\usepackage[user,counter,hyperref]{zref}
\usepackage[unicode=true, colorlinks=true]{hyperref}
\usepackage{appendix}

\makeatletter
% Storing the property section and adding it to the main property list
\zref@newprop{section}{\thesection}
\zref@addprop{main}{section}


\newcommand{\appref}[1]{%
  % Extracting the label
  \zref@ifrefundefined{#1}{%
  }{%
    \appendixname\ \zref@extract{#1}{section}, \hyperlink{\zref@extract{#1}{anchor}}{page \zref@extract{#1}{default}}%
  }%
}
\makeatother



\newcounter{includepdfnumber}  %create counter for labeling every inserted pdf

\newcounter{pdfpagenumber} %create counter for labeling every inserted pdf-page
% Providing correct hyper anchors since the pdfpagecounter is reset within every \includepdfwithlabel
\renewcommand{\theHpdfpagenumber}{appendix.\thesection.\thepdfpagenumber}
\newcounter{startpage} %create counter for incrementing startpage


\newcommand{\includepdfwithlabel}[6]{% usage: \includepdfwithlabel{'startpage'}{'endpage'}{\chapter,\section...}{additional arguments}{'labelname'}{'filename'}
  \setcounter{pdfpagenumber}{0}
  \refstepcounter{includepdfnumber}           
  \setcounter{startpage}{#1}  %set startpage to argument #1
  \includepdf[pages=\thestartpage,#4,link,linkname=#5,pagecommand={#3\refstepcounter{pdfpagenumber}\label{#5.\thepdfpagenumber}\zlabel{#5.\thepdfpagenumber}\large{#5-\theincludepdfnumber.\thepdfpagenumber}}]{#6} %
  \addtocounter{startpage}{1} %
  \includepdf[pages=\thestartpage-#2,#4,link,linkname=#5,pagecommand={\refstepcounter{pdfpagenumber}\label{#5.\thepdfpagenumber}\zlabel{#5.\thepdfpagenumber}\large{#5-\theincludepdfnumber.\thepdfpagenumber}}]{#6} %
}


\begin{document}
as seen in \appref{pdf1.1}

as seen in \ref{pdf1.1}

as seen in \appref{pdf1.2}

as seen in \appref{pdf1.2}

as seen in \appref{pdf2.1}

as seen in \ref{pdf2.1}

as seen in \appref{pdf2.2}

as seen in \ref{pdf2.2}


\begin{appendices}

  \includepdfwithlabel{1}{4}{\section{PDF A}}{height=0.6\textheight}{pdf1}{7.pdf}
  \includepdfwithlabel{1}{4}{\section{PDF B}}{height=0.6\textheight}{pdf2}{7.pdf}

\end{appendices}

\end{document}

在此处输入图片描述

7.tex以下是用于生成的代码7.pdf

\documentclass{article}

\usepackage{blindtext}

\begin{document}
\section{Beginning \jobname}
\blindtext[50]
\end{document}

相关内容