如何正确地将标签添加到 pdf 附录中

如何正确地将标签添加到 pdf 附录中

这是一个相当具体的问题,我之前一直在寻找将 PDF 文件添加到附录的方法。我在这个帖子中找到了解决方案:附录 - 添加 PDF,但它也造成了我的下一个问题。这个宏确实在附录中创建了新部分,但当用这个宏向附录设置添加标签时,它指向最后一个“正常”附录部分。

以下是来自其他线程的代码示例:

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{pdfpages}
\usepackage{xparse}

\makeatletter
\NewDocumentCommand\headerspdf{ O {pages=-} m }{% [options for includepdf]{filename.pdf}
\includepdf[%
    #1,
    pagecommand={\thispagestyle{fancy}},
    scale=.7,
    ]{#2}}
\NewDocumentCommand\secpdf{somO{1}m}{% [short title]{section title}[page specification]{filename.pdf} --- possibly starred
  \clearpage
  \thispagestyle{fancy}%
  \includepdf[%
    pages=#4,
    pagecommand={%
      \IfBooleanTF{#1}{%
        \section*{#3}}{%
        \IfNoValueTF{#2}{%
          \section{#3}}{%
          \section[#2]{#3}}}},
    scale=.65,
    ]%
    {#5}}
\makeatother

\pagestyle{fancy}

\begin{document}

As you can see, Appendix \ref{1},  \ref{2} and \ref{3} all say they're appendix A

\newpage
\appendix

\secpdf*{PDF on Starred Section Page}[3]{mypdf.pdf}
  \kant[2]

\section{Include PDF after Section Page}
\headerspdf[pages=1-2]{mypdf.pdf}
\label{1}
\secpdf{PDF on Section Page}[4]{mypdf.pdf}
\label{2}
\secpdf[Short Title]{PDF on Section Page with Short Title}{mypdf.pdf}
\label{3}
\headerspdf[pages=2-3]{mypdf.pdf}

\end{document}

第一页的内容如下:

提前致谢!

答案1

问题是,标签未在选项内全局设置pagecommand,即\@currentlabel仍使用旧的扩展\thesection。可以通过\@currentlabel在 pagecommand 中明确设置值来解决此问题。

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{pdfpages}
\usepackage{blindtext}
\usepackage{xparse}

\usepackage{hyperref}
\makeatletter


\NewDocumentCommand\headerspdf{ O {pages=-} m }{% [options for includepdf]{filename.pdf}
  \includepdf[%
  #1,
  pagecommand={\thispagestyle{fancy}},
  scale=.7,
  ]{#2}}
\NewDocumentCommand\secpdf{somO{1}m}{% [short title]{section title}[page specification]{filename.pdf} --- possibly starred
  \clearpage
  \thispagestyle{fancy}%
  \includepdf[%
    pages=#4,
    pagecommand={%
      \IfBooleanTF{#1}{%
        \section*{#3}}{%
        \IfNoValueTF{#2}{%
          \section{#3}}{%
          \section[#2]{#3}}}
     \protected@xdef\@currentlabel{\csname p@section\endcsname\csname thesection\endcsname}%
    },
    scale=.65,
    ]%
    {#5}}
\makeatother

\pagestyle{fancy}

\begin{document}

As you can see, Appendix \ref{fooone},  \ref{footwo} and \ref{foothree} -- they're not saying appendix only A any longer

\newpage
\appendix

\secpdf*{PDF on Starred Section Page}[3]{mypdf.pdf}
\blindtext[2]

\section{Include PDF after Section Page}
\headerspdf[pages=1-2]{mypdf.pdf}
\label{fooone}
\secpdf{PDF on Section Page}[4]{mypdf.pdf}
\label{footwo}
\secpdf[Short Title]{PDF on Section Page with Short Title}{mypdf.pdf}
\label{foothree}
\headerspdf[pages=2-3]{mypdf.pdf}

\end{document}

在此处输入图片描述

这是假人的代码mypdf.tex

\documentclass{article}

\usepackage{blindtext}
\begin{document}
\blindtext[50]
\end{document}

相关内容