# How to merge several tex files so that they have one table of contents/List of tables & figures

I have three chapters which I combine into a thesis using pdfpages: as shown in this link

Merging several papers

This gets me 95% of what I need the only thing I still need is a table of contents including all sub-sections for each chapter, and similarly a table listing all the tables and figures across all three chapters.

What is the simplest way to so this? All three chapters compile individually no problem.

I also tried using:

When should I use \input vs. \include?

\input and \include

\documentclass{article}
\begin{document}
\input{Introduction}
\end{document}


but when I tried this it included all the pre-ample from the tex file in the document?

Also some of the equations were displayed larger and hence were off the page! In the file generated using \input, but they appear fine when the tex file is compiled as a stand alone document?

Please note I'm really not good with Latex so the simplest solution please.

Happy to post what ever code you like but the tex files are rather large.

@ Jean

I tired your code it's a step in the right direction. There is a table of contents. However only chapter one appears in the output, both in the TOC and as a file, Chapter Two does not appear anywhere. And all the \usepackage preamble is printed in the document between the contents and the beginning of chapter 1

\documentclass{article}
\begin{document} \tableofcontents
\chapter{Chapter One} \input{Introduction.tex}
\chapter{Chapter Two} \input{rv-NSS.tex}
\end{document} 


\documentclass{article}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small}
\DefineVerbatimEnvironment{example}{Verbatim}{fontsize=\small}
\usepackage{natbib}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{bm}
\usepackage{grffile}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{longtable}
\usepackage{graphicx}
\usepackage{url}
\usepackage{graphics}
\usepackage{titlesec}
\def\UrlBreaks{\do\/\do-}
\usepackage[%
font=small,
labelfont=bf,
figurewithin=section,
tablewithin=section,
tableposition=top
]{caption}
\numberwithin{equation}{section}

\makeatletter
\def\env@matrix{\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{*\c@MaxMatrixCols l}}
\makeatother

\begin{document}
\input{title.tex}
\tableofcontents
\clearpage
\end{document}

• \addcontentsline{toc}{chapter}{My Chapter}? – cfr Sep 18 '14 at 18:18
• the simplest way is to make a single document and not have \documentclass and \begin{document} in each chapter, and just \input or \include each chapter into the main document – David Carlisle Sep 18 '14 at 18:30
• So the only way to do it is by manually putting in each section sub section and subsection from each chapter? – Bazman Sep 18 '14 at 18:34
• @David so for each tex file that I had previously been using to create the pdf's I should cut and paste then into a new tex file (one each) that doesn't have \documentclass or \begin document? I assume that I should remove the \usepackages from these new tex files as well? But how then will the final document look correct? – Bazman Sep 18 '14 at 18:42
• It's possible to combine separate pdf files, but really I'd only do that for things like conference proceedings joining articles by different authors. For a single document like a thesis it is much easier to automate numbering and cross references between sections in different chapters if it is a single document. – David Carlisle Sep 18 '14 at 20:04

The method of compiling only one "main" document does not work for you?

Like the mainTexFile.tex...that should have the \begin{document} argument etc....the rest would be simple tex files with sections, chapters, etc...

Like this...

Code for a "Main" tex file.

\begin{document}
\tableofcontents

\chapter{Chapter One}
\input{\...path_to_a_file...\chapterOne.tex}

\chapter{Chapter Two}
\input{\...path_to_a_file...\chapterTwo.tex}

\chapter{Chapter Three}
\input{\...path_to_a_file...\chapterThree.tex}

\end{document}


This should be working...It worked for me.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – alandella Sep 18 '14 at 20:53
• Hi Jean please see edit added above. – Bazman Sep 18 '14 at 20:57
• OK I removed the \begin{document}/ \end{document} from the tex files and now both chapters are included. However it's still printing the preamble from each tex file. I guess I need to remove those too? And add them to the main file? – Bazman Sep 18 '14 at 21:12
• Of course :) ..... remember "One Main File". I usually name the main file the...title of my article, book, whatever. Any style any package....everything must be...before \begin{document}! Simple... – Jean Mentz Sep 18 '14 at 21:14
• \includegraphics or whatever should be addressed in regard always to the Main.tex directory. If that is what you are asking... Like Folder_main(working dir)->/chapters ->/images ->/whatever Something like this can be used also to minimise paths...but it's not a necessity. '\newcommand{\CHAPTERDIR}{\WORKDIR chapters/}' – Jean Mentz Sep 18 '14 at 21:45

Here is an example using docmute. This avoids the need to delete the preamble from the included files which means they can still be compiled independently when appropriate. Essentially the docmute package causes the preambles of files which are included or input into the main file to be entirely ignored.

\documentclass{book}
\usepackage{filecontents}% only so I can include the 'separate' chapters here for convenience (LaTeX writes new files with the given names on compilation which are then \input below)
\begin{filecontents}{ch1.tex}
\documentclass{article}
\begin{document}
\kant[1-4]
\end{document}
\end{filecontents}
\begin{filecontents}{ch2.tex}
\documentclass{article}
\begin{document}
\kant[5-9]
\end{document}
\end{filecontents}
\usepackage{kantlipsum}% only to provide filler text for the example
\usepackage{docmute}
\title{Thesis}
\author{You}
\begin{document}
\maketitle
\tableofcontents
\chapter{First Chapter}
\input{ch1}% preamble will be ignored
\chapter{Second Chapter}
\input{ch2}% preamble will be ignored
\end{document}


# Update

appendix can be used to add appendices to each chapter. For example, this adds one appendix to the first chapter and two to the second:

\documentclass{book}
\usepackage{filecontents}
\begin{filecontents}{ch1.tex}
\documentclass{article}
\begin{document}
\kant[1-4]
\begin{subappendices}
\section{Appendix for First Chapter}
\kant[10]
\end{subappendices}
\end{document}
\end{filecontents}
\begin{filecontents}{ch2.tex}
\documentclass{article}
\begin{document}
\kant[5-9]
\begin{subappendices}
\section{First Appendix for Second Chapter}
\kant[11]
\section{Second Appendix for Second Chapter}
\kant[12]
\end{subappendices}
\end{document}
\end{filecontents}
\usepackage{kantlipsum}
\usepackage{docmute,appendix}
\title{Thesis}
\author{You}
\begin{document}
\maketitle
\tableofcontents
\chapter{First Chapter}
\input{ch1}
\chapter{Second Chapter}
\input{ch2}
\end{document}


See the package documentation for details of the ways in which this may be customised.

• Thanks but I have the same question as above each chapter should have it's own appendices and bibliography can this be achieved with this solution? – Bazman Sep 18 '14 at 21:37
• @Bazman Definitely you can have separate bibliographies. If you are using biblatex, this is provided by the package. If you are using bibtex, you need an additional package such as bibunits. Either way, it is definitely possible. I'm not sure about appendices but I imagine this could be managed. Are these formatted as a section? A separate chapter? It depends a bit on how you have configured things. – cfr Sep 18 '14 at 21:41
• @Bazman ctan.org/pkg/appendix should be helpful for the appendices. As I say, the bibliographies depends on how you are managing them. – cfr Sep 18 '14 at 21:45
• @Bazman See my edited answer for an example using per-chapter appendices. For the bibliographies, you'd need to provide details of the tools you are using to manage references. – cfr Sep 18 '14 at 22:05
• really helpful to hear about docmute - many thanks – tom Sep 30 at 0:13

Besides using the packages linked by @cfr, I think this may be achieved properly separating preambles and content into different files. If you write the preamble of a work, and maybe the titlepage and TOC, in work1_main.tex and the content in work1_content.tex you may compile just it alone or include the content in a bigger document.

Say we have n independent works and we want to compile them separately and together. It is sensible to place the files in a folder with a structure similar to:

-main.tex
+work1
--work1_main.tex
--work1_content.tex
--bibliography1.bib
+work2
--work2_main.tex
--work2_content.tex
--bibliography2.bib
+workn
--workn_main.tex
--workn_content.tex
--bibliographyn.bib


The content would be:

main.tex

\documentclass[a4paper]{report}

%all the packages required for the large document (all the works)

\begin{document}

%titlepage, tableofcontents...

\chapter{Introduction} %optional
%Introduction text

\chapter{Work 1}
\input{./work1/work1_content.tex}
\chapter{Work 2}
\input{./work2/work2_content.tex}
\chapter{Work n}
\input{./work2/workn_content.tex}

%globalbibliography

\end{document}


workn_main.tex

\documentclass[a4paper]{article}

%all the packages required for work n

\begin{document}

%titlepage, tableofcontents...

\input{work1_content.tex}

\end{document}


workn_content.tex

\section{Introduction to work n}
%Text,figure, tables, subsections, other sections...

%bibliography of work n


This way, if you compile main.tex you'll get the large document with includes all the works. If you compile workn_main.tex you'll get just work n. Although working with more files may seem tedious at first, I think it helps when having to merge or just change format. To make the loading of all the works as chapters easier, you can define a command. For example:

main_easy.tex

\documentclass[a4paper]{report}

%all the packages required for the large document (all the works)

\newcommand\importchapter[2]{
\chapter{#1}
\input{#2}
}

\begin{document}

%titlepage, tableofcontents...

\importchapter{Work 1}{./work1/work1_content.tex}
\importchapter{Work 2}{./work1/work2_content.tex}
\importchapter{Work n}{./work1/workn_content.tex}

%globalbibliography

\end{document}


Since you said you are not goot at LaTeX, I tried to keep the example simple. If you feel like taking this approach we may add how to keep up with appendices and the bibliography.

In relation to this this similar question you maid, you could create workn_main.tex, workn_preamble.tex and workn_content.tex for each work. If you try to input all the work?_preamble` files in the preamble of the main, it will probably crash. You should check those to load each package just once.