2

I am in a tricky situation. I am using NewEnviron to separate a handful of major parts of my document. Each part has its own numbering scheme and several environment-specific commands and settings. Important for my question, all numbering schemes follow the principle page_current of page_total

If I now, however, insert pdf-documents using \includepdf, the label for page_total breaks if \includepdf is executed on the last page of my document.

Here an mwe, which probably explains it a bit better.

\documentclass[fontsize=10pt]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mwe}

\usepackage{pdfpages}
\usepackage{environ}
\usepackage{scrlayer-scrpage}

\NewEnviron{myenvi}{%
    \clearscrheadfoot%
    \cfoot{\normalfont\thepage\ / \pageref{lastpage}}%
    \clearpage%
    \BODY%
    \label{lastpage}%
}

\newcommand{\myincludepdf}[1]{\includepdf[scale=0.5, frame, pagecommand={}]{#1}}
%\newcommand{\myincludepdf}[1]{\includepdf[scale=0.5, frame, pagecommand={\label{lastpage}}]{#1}}

\begin{document}

    \begin{myenvi}
        \lipsum[1-15]
        \myincludepdf{example-image-a4}
    \end{myenvi}

\end{document}

I also tried to add my label to the post-code of includepdf. That doesn't help.

I found two ways to get it to work. But both are not satisfactory. The first one is simply to add another page to the document. Because then the \label{lastpage} can execute again. Moreover, one can also add the \label{lastpage} to the pagecommand-option of \includepdf. But then I get a lot of "lastpage multiply defined" warnings.

Anyone an idea how to solve this issue. I was considering using a counter or gdef or something. But I think it is not really possible to get around this problem without the help of a sidecar-file.

Is there maybe an option to directly manipulate the aux-file so that I don't produce multiple labels and only the last call of \label{lastpage} remains?

3
  • Do you get the multiply labels because the pdf has more than one page (which is hasn't in your example)? Jan 4, 2018 at 21:35
  • If you call includepdf more than once, the warnings occur even if you don't use the pagecommand={\label{lastpage} option. Just ignore them - warnings are warnings and not errors for a reason. Jan 4, 2018 at 22:27
  • Yes, the warnings only occur if I call \includepdf multiple times. But that is the idea. I want to use that to add certificates to my CV. And I, unfortunately, can not ignore the warnings. In the end, I would like to put everything in a document class and share it with my colleagues. Therefore, I would like to keep random warnings to a minimum. Jan 5, 2018 at 9:41

1 Answer 1

1

Ok, found a solution. Probably also not the most elegant one, but it works. I use the lastpage-package to check inside \includepdf whether the current page is the last one of the document or not. If that is the case, I execute \label{endenvi} and, hence, only define it once. And if a page comes after all \includepdf's the label-command inside my environment-definition executes.

Here the code:

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mwe}

\usepackage{pdfpages}
\usepackage{environ}
\usepackage{lastpage}
\usepackage{xifthen}
\usepackage{scrlayer-scrpage}


\NewEnviron{myenvi}{%
    \clearscrheadfoot%
    \cfoot{\normalfont\thepage\ / \pageref{endenvi}}%
    \clearpage%
    \BODY%
    \label{endenvi}
}


\newcommand{\myincludepdf}[1]{\includepdf[scale=0.5, frame, pagecommand={%
        \ifthenelse{\equal{\pageref{LastPage}}{\thepage}}{\label{endenvi}}{}}]{#1}}

\begin{document}

    \begin{myenvi}
        \lipsum[1-15]
        \myincludepdf{example-image-a4}
        \myincludepdf{example-image-a4}
    \end{myenvi}

\end{document}
1
  • 1
    If you only want to set \label{endenvi} if you are on the last page, why don't you the LastPage label directly? Mar 6, 2018 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .