0

currently i am in a situation where i have inserted some pages via includepdf, followed by additional \includepdf calls that use the addtotoc function, as follows:

\includepdf{pdffile1}
\includepdf{pdffile2}
\includepdf{pdffile3}
\includepdf{pdffile4}
\includepdf{pdffile5}
\includepdf{pdffile6}

\addcontentsline{toc}{part}{Part 1. Part 1}
\includepdf[pages=-,pagecommand={},addtotoc={1, subsection, 2, somename, somenamelabel,
...
n, subsection, 2, somenameN, somenameNlabel}]{./part1docs}

\addcontentsline{toc}{part}{Part 2. Part 2}
\includepdf[pages=-,pagecommand={},addtotoc={1, subsection, 2, somenamepart2, somenamep2label,
...
n, subsection, 2, somenamepart2N, somenamep2Nlabel}]{./part2docs}

i would like to now add a new section heading, called "Part 3", where the subsections would refer to the pages in the \includepdf{pdffile1}..\includepdf{pdffile6} calls.

edit: i found someone's macro that allows me to put in entries

\newcommand*{\NoPageAddContentsLine}[3]{%
  \begingroup
    \newpage
    \let\protected@write\protected@immediate@write
    \addcontentsline{#1}{#2}{#3}%
  \endgroup
  \stepcounter{page}%
}

via \NoPageAddContents line, so if i add this code:

\setcounter{page}{1}
\NoPageAddContentsLine{toc}{part}{Part 3. Transcripts}
\NoPageAddContentsLine{toc}{subsection}{pdffile1name}
...
\NoPageAddContentsLine{toc}{subsection}{pdffile6name}

it looks right but the linking is still an issue. even if i add a link=true, linkname=x to each of the pdffiles, it does not work.

any tips on getting a working link to a contents entry added in this way, if the link is created through pdfpages?

3
  • Your question is confusing. To clarify: You are talking about two different documents? You want to link from one PDF to another, similar to using \href{foo.pdf#section}{...}. And the difficulty is, that you would like this \href command to be inside a TOC entry. Is this what you want? – Andreas Matthias Sep 25 '20 at 13:28
  • hi matthias. i reworked my question. i think it's much easier to understand now. this is all for a single tex file. i just want to include some pdf files in a section that is after some pdf files that call addtotoc (and addcontentsline), if that makes sense. – user41816 Sep 25 '20 at 14:12
  • hi matthias, i am almost there, but i still have an issue with linking. hoping you can check out the additions and help me conjure a solution to hacking a link to each of pages included through pdfpages – user41816 Sep 25 '20 at 15:21
0

Do you really want a TOC that does not follow the structure of your document? I suppose this will be highly confusing for everyone reading your document.

Maybe a simple table with references to the other pages might be a better solution than putting it into the TOC.

Anyway, here's a quick solution without page numbers. Adding page numbers would be much more cumbersome.

\documentclass[a4paper]{article}
\usepackage{pdfpages}
\usepackage{hyperref}

\makeatletter
\newcommand\mysection[2]{%
  \stepcounter{section}
  \addtocontents{toc}{%
    \protect\contentsline{section}%
    {\protect\numberline {\thesection}#1}%
    {}%
    {#2}%
    \protected@file@percent}}
\makeatother

\begin{document}
\includepdf[pages=-, link, linkname=AAA]{dummy.pdf}

\tableofcontents
\section{First}
\mysection{Let's see}{AAA.2}
\section{Next}

\end{document}
1
  • thanks for this solution matthias, but one thing i've noticed is that, for my example, this will not show up since "Part 3" is technically an empty "part" (i.e. no \part*{} or \part{} has been declared). if the section is created "normally" (with some declaration prior to adding the entry), then it works. – user41816 Sep 25 '20 at 17:23
0

by using

\newcommand*{\NoPageAddContentsLine}[3]{%
  \begingroup
    \newpage
    \let\protected@write\protected@immediate@write
    \addcontentsline{#1}{#2}{#3}%
  \endgroup
  \stepcounter{page}%
}

and then adding \label{x} after each includepdf call that's being indexed out-of-order:

\includepdf{pdffile1}\label{whatiwant}

and then using hyperref in the nopageaddcontentsline,

\NoPageAddContentsLine{toc}{subsection}{\hyperref[whatiwant]{pdffile1 header name}}

it will work. it's not the prettiest solution, but it's generated so i'll take it.

and i agree matthias, it's not the best way to do things.

but The < Vaunted Entity > is The < Vaunted Entity >. your documents must meet their criteri prior to arguing with them ;)

Your Answer

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