# \includepdf for enclosures with its own tableofcontents

I would like to add some certificates to my document using \includepdf and create some kind of tableofcontents for them. However, since I already have a tableofcontents where I don't want them to appear, I cannot rely on addcontentsline{toc}{... .

This is what I came up with for now, but it is not particularly beautiful. It involves a lot of manual work and the risk of making mistakes is huge.

\documentclass[fontsize=10pt]{scrartcl}

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

\usepackage{pdfpages}

\newcommand{\includecertificate}[2][1]{%
\includepdf[pages=#1,frame,pagecommand={\label{#2.#1}},scale=0.5]{#2}%
}

\newcommand{\certificateentry}[3][1]{%
\ifthenelse{\equal{#1}{1}}{%
Page \pageref{#2.1}:\quad #3\newline
}{%
Pages \pageref{#2.1}--\pageref{#2.#1}:\quad #3\newline
}}

\begin{document}

\noindent%
\certificateentry[3]{doc1}{Here you find document 1}
\certificateentry[1]{doc2}{Here you find document 2}
\certificateentry[5]{doc3}{Here you find document 3}

\includecertificate[1]{doc1}
\includecertificate[2]{doc1}
\includecertificate[3]{doc1}

\includecertificate[1]{doc2}

\includecertificate[1]{doc3}
\includecertificate[2]{doc3}
\includecertificate[3]{doc3}
\includecertificate[4]{doc3}
\includecertificate[5]{doc3}

\end{document}


The first command gets the number of pages of the document, the name of the document and its description. The second command gets the page to be loaded and the name of the document. Hence, it has to be called for every page individually. I am doing that for two reasons:

1. When I load a multi-page document with pages=1-5, the label ends up on the last page, which is obviously not what I want.
2. Because I would prefer (though I can give up on that idea if necessary) to have the numbers showing like page begin-end, not just page begin.

In the end, I would prefer something like

\tableofcertificates

\includecertficate[optional page range]{doc1}{Here you find document 1}
\includecertficate{doc2}{Here you find document 2}
\includecertficate{doc3}{Here you find document 3}


Can someone help me to achieve something like that?

Thanks a lot in advance!

## 1 Answer

You can define your own list of certificates using \DeclareNewTOC:

\DeclareNewTOC[%
nonfloat,
type=certificate,
listname={List of Certificates}
]{crt}


Either option float or option nonfloat is needed even if you do not use the environments defines by these options but you want a counter for the certificates and commands for adding entries of the given type to the list fo certificates.

After this you can use, e.g., \addcontentsline{crt}{certificate}{…} to add entries to the list of certificates. pdfpages provides option addtolist to do so:

\documentclass[fontsize=10pt]{scrartcl}

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

\usepackage{pdfpages}
\usepackage{mwe}% using the example pdfs of this package

\DeclareNewTOC[%
nonfloat,
type=certificate,
listname={List of Certificates}
]{crt}

\newcommand{\includecertificate}[4]{%
\includepdf[pages=#1-#2,frame,pagecommand={},scale=0.5,addtolist={#1,certificate,#4,crt:#3.start}]{#3}%
}

\begin{document}
\listofcertificates

\includecertificate{1}{3}{example-image-a4-numbered}{Here you find document 1}

\includecertificate{1}{1}{example-image-a4}{Here you find document 2}

\includecertificate{4}{10}{example-image-a4-numbered}{Here you find
document 3}
\end{document}


This already results in:

But if you want page ranges instead of starting page numbers in this list of certificates, you have to do more. The following quick hack does work for Arabic page numbers only:

\documentclass[fontsize=10pt]{scrartcl}

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

\usepackage{pdfpages}
\usepackage{mwe}% using the example pdfs of this package

\DeclareNewTOC[%
nonfloat,
type=certificate,
listname={List of Certificates}
]{crt}

\usepackage{refcount}
\makeatletter
\newcommand{\includecertificate}[4]{%
\newpage
\begingroup
\edef\@currentlabel{\the\numexpr\value{page}+#2-#1\relax}%
\label{pgend:\thepage}%
\endgroup
\includepdf[pages=#1-#2,frame,pagecommand={},scale=0.5,addtolist={#1,certificate,#4,crt:#3.start}]{#3}%
}
\BeforeStartingTOC[crt]{%
\renewcommand*{\@pnumwidth}{3em}%
\renewcommand*{\@tocrmarg}{3em}%
\let\crt@contentsline\contentsline
\renewcommand*{\contentsline}[3]{%
\ifnum #3=\getrefnumber{pgend:#3}\relax
\crt@contentsline{#1}{#2}{#3}%
\else
\crt@contentsline{#1}{#2}{#3--\ref{pgend:#3}}%
\fi
}%
}

\begin{document}
\listofcertificates

\includecertificate{1}{3}{example-image-a4-numbered}{Here you find document 1}

\includecertificate{1}{1}{example-image-a4}{Here you find document 2}

\includecertificate{4}{10}{example-image-a4-numbered}{Here you find
document 3}
\end{document}


This solution does work with simple arithmetic to calculate the last page number of a page sequence. But you can also do some label tricks and use \protected@immediate@write of scrlfile to create a version that also works for non Arabic page numbers:

\documentclass[fontsize=10pt]{scrartcl}

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

\usepackage{pdfpages}
\usepackage{mwe}% using the example pdfs of this package

\DeclareNewTOC[%
nonfloat,
type=certificate,
listname={List of Certificates}
]{crt}
\usepackage{refcount}
\makeatletter
\newcommand{\includecertificate}[4]{%
\newpage
\edef\thestartpage{\thepage}%
\includepdf[pages=#1-#2,frame,pagecommand={},scale=0.5,addtolist={#1,certificate,#4,pgstart:\thestartpage}]{#3}%
\begingroup
\addtocounter{page}{-1}%
\let\protected@write\protected@immediate@write% do write labels immediately
\label{pgend:\thestartpage}
\stepcounter{page}%
\endgroup
}
\BeforeStartingTOC[crt]{%
\renewcommand*{\@pnumwidth}{3em}%
\renewcommand*{\@tocrmarg}{3em}%
\let\crt@contentsline\contentsline
\renewcommand*{\contentsline}[3]{%
\typeout{\getpagerefnumber{pgstart:#3}=\getpagerefnumber{pgend:#3}?}%
\ifstr{\getpagerefnumber{pgstart:#3}}{\getpagerefnumber{pgend:#3}}{%
\crt@contentsline{#1}{#2}{#3}%
}{%
\crt@contentsline{#1}{#2}{#3--\pageref{pgend:#3}}%
}
}%
}

\begin{document}
\listofcertificates

\includecertificate{1}{3}{example-image-a4-numbered}{Here you find document 1}

\includecertificate{1}{1}{example-image-a4}{Here you find document 2}

\cleardoublepage\pagenumbering{roman}

\includecertificate{4}{10}{example-image-a4-numbered}{Here you find
document 3}
\end{document}


• Hey, thanks a lot for the detailed answer. This will for sure help. Jan 3 '18 at 15:20