3

Quite a specific question, I was looking to add PDF files to an appendix before. Found my solution in this thread: Appendix - Adding PDF, But it also created my next problem. This macro does create new sections in the appendix, but when adding a label to an appendix set-up with this macro it points to the last "normal" appendix section.

Here is an example in the code from the other thread:

\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}

Here is what the first page compiles into:

Thanks in advance!

Rob
  • 33

1 Answers1

3

The problem is, that the label is not set globally within the pagecommand option, i.e. \@currentlabel has still the old expansion of \thesection. This can be cured by explicitly setting the \@currentlabel value in the 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}

enter image description here

Here's the code for the dummy mypdf.tex

\documentclass{article}

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