[Tex/LaTex] Manually create a table of contents (instead of automatically generated)

table of contents

Is there a way to manually create a Table of Contents (by writing each item one by one), instead of using \tableofcontents which automatically generates one?

Here is why I need this. I need to compile several different PDF files into one single one. And those files are written by different document classes and in different settings; it is hard to put them into one single LaTeX file. So, I will put those PDF files together and only change page numbers. But then I need a table of contents page and I need to create this one out of nothing.

Best Answer

Assume you have files file1.pdf (3 pages), file2.pdf (6 pages) and file3.pdf (9 pages) that you want to compile into a single PDF containing 19 pages, where the first page is a Table of Contents.

Construct the contents using a set of manual function for (say) \sections and \subsections. The idea would be to write directly to the .toc file the sectional unit number, title and page number:

enter image description here

\documentclass{article}

\usepackage[margin=1in]{geometry}
\pagestyle{empty}

% file1.pdf: pages  1- 3
% file2.pdf: pages  4- 9
% file3.pdf: pages 10-18

\newcommand{\addsection}[3]{\addtocontents{toc}{\protect\contentsline{section}{\protect\numberline{#1}#2}{#3}}}
\newcommand{\addsubsection}[3]{\addtocontents{toc}{\protect\contentsline{subsection}{\protect\numberline{#1}#2}{#3}}}

\begin{document}

% Construct contents
\addsection{1}{File 1-1}{1}
\addsubsection{1.1}{File 1-1.1}{2}
\addsection{2}{File 1-2}{3}
\addsection{3}{File 2-1}{4}
\addsubsection{3.1}{File 2-1.1}{7}
\addsubsection{3.2}{File 2-1.2}{8}
\addsection{4}{File 2-2}{9}
\addsection{5}{File 3-1}{10}
\addsubsection{5.1}{File 3-1.1}{10}
\addsubsection{5.2}{File 3-1.2}{11}
\addsubsection{5.3}{File 3-1.3}{12}
\addsection{6}{File 3-2}{17}
\addsubsection{6.1}{File 3-2.1}{18}
\addsubsection{6.2}{File 3-2.2}{18}

\tableofcontents

\end{document}

You can expand the structure to lower levels, if needed.

Related Question