正文中指定的附录和交叉引用

正文中指定的附录和交叉引用

我正在按照这个问题的答案中的建议设置附录:

附录 - 添加 PDF,使用 cfr 建议的 MWE:

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

\makeatletter
\NewDocumentCommand\headerspdf{ O {pages=-} m }{% [options for include pdf]{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}

Reference to my appendix \ref{secpdf:Test}.

\newpage
\appendix

\secpdf[Short Title]{PDF on Section Page with Short Title}{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\label{secpdf:Test}
\kant[3]
\headerspdf[pages=2-3]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\end{document}

它正确地提供了附录 A,但现在我在交叉引用方面遇到了麻烦。我尝试了示例中所示的引用,但它无法正常工作。

有一个类似的案例,我无法将其转移到我的案例中:如何正确标记包含的 PDF?

答案1

问题的根源是pagecommand,因为它似乎将命令设置成一个组,这样\refstepcounter对部分计数器确实有效,但随后的调用却\label无法引用正确的位置。

一个快速的解决方法是使用reduce the correctly increased section number first with\addtocounter{section}{-1} and then call\refstepcounter{section} outside of the\includepdf` 命令。

请注意,如果使用的话,这不是一个选项\section*{},但在这种情况下标记无论如何都是没用的,所以我没有添加额外的代码来捕获这个问题。

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

\makeatletter
\NewDocumentCommand\headerspdf{ O {pages=-} m }{% [options for include pdf]{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}
    \addtocounter{section}{-1}%
    \refstepcounter{section}%
  }%
\makeatother

\pagestyle{fancy}

\begin{document}
%\tableofcontents

Reference to my appendix \ref{secpdf:Test}, whereas in \ref{secpdf:other}, it is shown that bla bla and in \ref{secpdf:evenanother} there is even evidence that...

\newpage
\appendix


\secpdf[Short Title]{A PDF on Section Page with Short Title}{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\label{secpdf:Test}%
\kant[3]

\headerspdf[pages=2-3]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}


\secpdf[Short Title]{B PDF on Section Page with Short Title}{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\label{secpdf:other}%
\kant[3]

\headerspdf[pages=2-3]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}

\secpdf[Short Title]{C PDF on Section Page with Short Title}{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\label{secpdf:evenanother}%
\kant[3]

\headerspdf[pages=2-3]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}


\end{document}

相关内容