4

I'd like to have a new section in line with the usual numbering, without it showing in the text. I include multiple PDF files using the \includepdf command. Each of them is a subsection which I'd like to subordinate under the section. However, I can't just use \section because that makes a new headline in the document and I do not want this, especially because I have no other text except the files and an empty site with just the headline on it looks silly.

Example:

\documentclass{scartcl}
\usepackage{pdfpages}

\begin{document}
\tableofcontents

\includepdf[addtotoc={1,subsection,2,title1,1}]{1.pdf}

\includepdf[addtotoc={1,subsection,2,title2,2}]{2.pdf}

\end{document}

I want it to look like

1.Section
   1.1 title1
   1.2 title2

without having a nearly empty page because \section makes a new headline

  • 3
    Welcome to the site! You can use the addcontentsline command - there are lots of instances of its use on our site :) – cmhughes Jul 3 '14 at 16:57
  • unfortunately, \addcontentsline does not produce a number in front of the entry – Martin Jul 3 '14 at 17:04
  • 2
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Adam Liter Jul 3 '14 at 17:11
  • I added an example. Hopefully, it's more clear what I want now – Martin Jul 3 '14 at 17:39
5

I do not know whether this is what is requested, but it is quite easy to add a non-existing section (or anything) to the TOC and use a section number in front of it.

User cmhughes already pointed to \addcontentsline

Please change the title and the corresponding section number.

\documentclass{article}
\usepackage{pdfpages}



\begin{document}
\tableofcontents

\setcounter{section}{1}
\addcontentsline{toc}{section}{\protect\numberline{\thesection}{YourSectionTitle}}


\includepdf[addtotoc={1,subsection,2,title1,1}]{1.pdf}

\includepdf[addtotoc={1,subsection,2,title2,2}]{2.pdf}

\end{document}

enter image description here

Updated with some support for hyperref such that no stupid link is generated

hyperref adds a 4th {} argument to \contentsline which is meant for the hyperanchor. If empty, no anchor and link is generated. If hyperref is not loaded, the empty 4th argument with {} does not harm any way, it can remain in the .toc file.

\documentclass{article}
\usepackage{pdfpages}

\usepackage{hyperref}

\newcommand{\phantomsectiontotoc}[1]{%
  \addtocontents{toc}{\protect\contentsline{section}{\protect\numberline{\thesection}#1}{}{}}%
}

\begin{document}
\tableofcontents


\setcounter{section}{1}
\phantomsectiontotoc{YourSectionTitle}

\includepdf[addtotoc={1,subsection,2,title1,1}]{1.pdf}

\includepdf[addtotoc={1,subsection,2,title2,2}]{2.pdf}

\end{document}
  • that's exactly what I was looking for, thank you – Martin Jul 3 '14 at 18:03
  • @Martin: Alright, please test and give feedback ;-) – user31729 Jul 3 '14 at 18:05
  • 1
    How does hyperref react to adding contentlines manually? – 1010011010 Jul 3 '14 at 18:07
  • 1
    @1010011010: Good question: Normal sections behave well, but a click on YourSectionTitle will lead to the TOC, at least that is what I guess -- I just checked. It is exactly as I wrote. I suppose, a manual \hypertarget can cure this problem – user31729 Jul 3 '14 at 18:09
  • A special command in conjunction with \phantomsection should work. I will check later on – user31729 Jul 3 '14 at 18:27
2

Only an addendum to Christian's first example:

To have a solution with correct automatic bookmark for the \addcontentsline with hyperref you need to use \refstepcounter to increase the section counter. And you should insert \clearpage before adding the section entry to the table of contents, because otherwise it wouldn't refer to the page of the first included PDF page but to the page before:

\documentclass{article}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}
\tableofcontents

\clearpage% to have the section on the correct page
%\stepcounter{section}{0}% If you need section number n instead of the next one, set section number n-1 here, because you still need:
\refstepcounter{section}
\addcontentsline{toc}{section}{\protect\numberline{\thesection}{YourSectionTitle}}


\includepdf[addtotoc={1,subsection,2,title1,1}]{example-image-a.pdf}

\includepdf[addtotoc={1,subsection,2,title2,2}]{example-image-b.pdf}

\end{document}

Note, this solutions works independent from using hyperref.

  • One could argue that your solution leaves a page entry in the ToC for a 'fake' section which isn't there. If this is requested, everything is fine... – user31729 Apr 18 '17 at 18:54
  • @ChristianHupfer One could argue that your solution misses the page number for the fake section in the ToC. If this is requested, everything is fine … ;-) And one could ague that a ToC should contain only headings that are shown in the document. So one should do whatever he/she likes best. – Schweinebacke Apr 18 '17 at 19:01
  • Of course, and in fact, my old answer still has the page number, in retrospective I think a fake section should not a have page number at all, but it's up to the O.P. (which did not return for more than year ago) – user31729 Apr 18 '17 at 19:04

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.