3

I am setting up appendices like suggested in the answer to this question:

Appendix - Adding PDF, using the MWE suggested by cfr:

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

It correctly delivers Appendix A in appendix, but now I am having troubles with the cross-referencing. I tried a reference like shown in the example but it does not work properly.

There is a similar case, which I cannot manage to transfer on my case: How to correctly label an included PDF?

  • 1
    I did not work with xparse so far, but I recognize an unnumbered \section* command by far ;-) This command does not issue an \refstepcounter command, and that's possibly the reason why the label points to some other place. – user31729 Aug 3 '14 at 20:34
  • 1
    You should add a \end{document} command ;-) – user31729 Aug 3 '14 at 22:10
3

The source of the problem is pagecommand, as it seems to set the commands into a group, such that \refstepcounter does work for the section counters, but consequent calls to \label can not refer to the correct place.

A quick work around is to reduce the correctly increased section number first with\addtocounter{section}{-1}and then call\refstepcounter{section}outside of the\includepdf` command.

Note, that this is not an option if \section*{} is used, but in this case labelling is of no use anyway, so I did not add extra code to catch this.

\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}
| improve this answer | |
  • perfect solution, works fine – Jens Aug 3 '14 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.