To read the toc
-file twice, you have to avoid to open it to write before the second reading. LaTeX's kernel macro \@starttoc
, which is used by \tableoofcontents
, usually also opens the file for writing. Loading scrwfile
as shown in esdd's answer already avoids opening files for writing when \@starttoc
is used. Also local redefinition of \@starttoc
as show in TeXnician's answer can be used to avoid the opening for writing. Another suggestion would be to deactivate LaTeX's opening of auxiliary files for writing, e.g.:
\documentclass{scrbook}
\usepackage[english]{babel}
\usepackage{scrwfile}
\usepackage{mwe}% provides \Blinddocument
\makeatletter
\newcommand*{\shorttableofcontents}{%
\begingroup
\value{tocdepth}=\z@ % set tocdepth locally to zero
\@fileswfalse % deactivate opening of auxiliary files
\listoftoc[Short Contents]{\ext@toc}%
\endgroup
}
\makeatother
\begin{document}
\shorttableofcontents% must be before \tableofcontents
\tableofcontents
\Blinddocument
\end{document}
Here KOMA-Script's \listoftoc
is used to show the short version of the ToC. See the manual for more information.
However this suggestion would fail, if \shorttableofcontents
would be used after \tableofcontents
! And you cannot add extra entries to the short table of contents, that are not shown in the normal table of contents. If you need such things, esdd's answer would be the better suggestion.
However you could use another KOMA-Script macro to clone the main ToC:
\documentclass{scrbook}
\usepackage[english]{babel}
\usepackage{scrwfile}
\usepackage{mwe}% provides \Blinddocument
\DeclareNewTOC[%
listname={Short Contents},
]{stoc}
\makeatletter
\renewcommand{\addtocentrydefault}[3]{%
\expandafter\tocbasic@addxcontentsline\expandafter{\ext@toc}{#1}{#2}{#3}%
\tocbasic@addxcontentsline{stoc}{#1}{#2}{#3}%
}
\makeatother
\BeforeStartingTOC[stoc]{\value{tocdepth}=0\relax}
\let\shorttableofcontents\listofstocs
\begin{document}
\shorttableofcontents
\tableofcontents
\Blinddocument
\end{document}
KOMA-Script uses \addtocentrydefault
to generate the ToC entries of all headings. But this does not work, if a package generates ToC entries without using \addtocentryefault
e.g. using \addcontentsline
directly.
Instead of redefining \addtocentrydefault
you can try to patch it as shown in esdd's second suggestion.