3

How do I insert the table of contents of a document into another LaTeX document? If this is even possible. In the first document, the ToC is created using \tableofcontents

edited from comments: (partial) MWE:

\documentclass[%
 paper=A4, twoside=true, openright, parskip=full, chapterprefix=true,
 11pt, headings=normal, bibliography=totoc, listof=totoc,
 titlepage=on, captions=tableabove, draft=false,
 ]{scrreprt}
\usepackage[utf8]{inputenc}     
\usepackage[english]{babel}   
\usepackage[                    
    figuresep=colon,
    sansserif=false,
    hangfigurecaption=false,
    hangsection=true,
    hangsubsection=true,
    colorize=full,
    colortheme=bluemagenta,
]{cleanthesis}

\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{tabularx}
\usepackage{epstopdf}
\usepackage{placeins}
\usepackage{float}
\usepackage{amssymb}
\usepackage{color}
\restylefloat{figure}
\usepackage{tablefootnote}
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\lstset{breakatwhitespace=false}
\usepackage{courier}
\usepackage{appendix}
\usepackage{booktabs}
\usepackage{eso-pic}
\usepackage{pbox}
\usepackage{ragged2e}
\usepackage{adjustbox}
\usepackage{graphicx} %allows for including images
\usepackage{biblatex}
\addbibresource{bib-refs}
\usepackage{pdfpages}
\usepackage{subcaption}
\usepackage{xcolor,colortbl}
\usepackage{multirow}

.toc file:

\boolfalse {citerequest}
\boolfalse {citetracker}
\boolfalse {pagetracker}
\boolfalse {backtracker}\relax 
\defcounter {refsection}{0}\relax
\select@language {english}
\defcounter {refsection}{0}\relax 
\contentsline {chapter}{\numberline {1}Introduction}{1}{chapter.1} 
\defcounter {refsection}{0}\relax 
\contentsline {section}{\numberline {1.1} Expansion Project}{1}{section*.11} 
\defcounter {refsection}{0}\relax 
\contentsline {section}{\numberline {1.2} Safety Management}{2}{section*.12} 
\defcounter {refsection}{0}\relax
12
  • 2
    welcome to tex.sx. when the first document is processed, a .toc file is created. this contains the information for the table of contents. if you want to insert this toc into another document, the approach i would take is to copy it under another name, and input that explicitly to the second document. some additional maneuvering might be needed, such as giving it an appropriate heading, but to be able to say exactly what's necessary, we need more information, preferably in the form of a small example file, starting with \documentclass and ending with \end{document}. Aug 24, 2015 at 20:20
  • 1
    please add a small example file plus the .toc file (just a few lines of it) that you're trying to insert. without knowing what document class you're using, we'd just be guessing, which isn't a very profitable use of time. Aug 24, 2015 at 20:59
  • 1
    You could always use pdfpages. Aug 25, 2015 at 3:37
  • 1
    i've taken the liberty of inserting the code from your comments in the question itself; i hope i've interpreted it correctly. (you can always edit your own question, and that's the preferred approach.) but this still doesn't give sufficient information. what we really need is example code that we can cut-and-paste and use directly for experimental compilation. this is a useful topic (i've faced it and implemented it before), but it really depends on the packages being used; for example, the hyperref elements in the toc file won't transfer successfully. Aug 25, 2015 at 13:28
  • 1
    is there \usepackage[<language>]{babel} in both files?
    – touhami
    Aug 25, 2015 at 18:02

1 Answer 1

2

It's possible to include an external .toc file, but tedious and the usability is limited, since the page numbers etc. are not consistent, most likely.

The shown .toc file was made with hyperref, which complicats the whole issue.

But here's a way:

\documentclass[%
 paper=A4, twoside=true, openright, parskip=full, chapterprefix=true,
 11pt, headings=normal, bibliography=totoc, listof=totoc,
 titlepage=on, captions=tableabove, draft=true,final=false
 ]{scrreprt}
\usepackage[utf8]{inputenc}     
\usepackage[english]{babel}   
\usepackage[                    
    figuresep=colon,
    sansserif=false,
    hangfigurecaption=false,
    hangsection=true,
    hangsubsection=true,
    colorize=full,
    colortheme=bluemagenta,
]{cleanthesis}

\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{epstopdf}
\usepackage{placeins}
\usepackage{float}
\usepackage{amssymb}
\restylefloat{figure}
\usepackage{tablefootnote}
%
\usepackage{courier}
\usepackage{appendix}
\usepackage{booktabs}
\usepackage{eso-pic}
\usepackage{pbox}
\usepackage{ragged2e}
\usepackage{adjustbox}
\usepackage{graphicx} %allows for including images
\usepackage{biblatex}
\addbibresource{bib-refs}
\usepackage{pdfpages}
\usepackage{subcaption}
\usepackage{xcolor,colortbl}
\usepackage{multirow}

\usepackage{hyperref}
\usepackage{cleveref}


%\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
%\lstset{breakatwhitespace=false}


\newcommand{\othercontentsname}{%
  Secondary Contents%
}

% now some code that resembles the `\@starttoc` command 
\makeatletter
\newcommand{\usetocfromothersource}[1]{%
  \begingroup
  \makeatletter
  \IfFileExists{#1}{%
    \chapter*{\othercontentsname}%
    \@input{#1}% 
    \@nobreakfalse
  }{}%
  \makeatother
  \endgroup
}
\makeatother


\begin{document}
\tableofcontents

\usetocfromothersource{otherdoc.toc}

\part{Some part}
\chapter{Foo}




\end{document}

And the otherdoc.toc:

\boolfalse {citerequest}%
\boolfalse {citetracker}
\boolfalse {pagetracker}
\boolfalse {backtracker}\relax 
\defcounter {refsection}{0}\relax
\select@language {english}
\defcounter {refsection}{0}\relax 
\contentsline {chapter}{\numberline {1}Introduction}{1}{chapter.1} 
\defcounter {refsection}{0}\relax 
\contentsline {section}{\numberline {1.1} Expansion Project}{1}{section*.11} 
\defcounter {refsection}{0}\relax 
\contentsline {section}{\numberline {1.2} Safety Management}{2}{section*.12} 
\defcounter {refsection}{0}\relax

Please note that mcode changes the section headings etc and seems to be incompatible with KOMA.

enter image description here

1

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.