How can I change the settings of my table of contents, so that the sections and subsections are not indented as shown in the second picture. I would also like to center the title of the Contents. I am using the article
class.
[Tex/LaTex] formatting of the table of contents
formattingtable of contents
Related Solutions
My initial answer was written using etoc.
The main idea though, was taken over from karlkoeller answer: using afterpage
. The difference is that the method now is compatible with a TOC on many pages.
I figured next that it could be done in a simpler manner. Thus, I provide here two codes: one not using etoc
at all. But the formatting of the toc will have to be done by some package, like tocloft
. The second uses etoc
to facilitate both the OP's constraint on the chapter headline and some formatting customisation. This second code sample has a somewhat more complicated document body. And on this occasion one sees something is not perfect for the last (fourth) toc page. I have not yet investigated which aspect of afterpage
is causing this.
% without etoc
\documentclass{book}
\usepackage[textheight=10cm]{geometry}% small page height for testing!
\usepackage{hyperref}
\usepackage{afterpage}
\newif\ifintoc
\newcommand*{\PrintChapterTocHeadline}
{\hbox to \linewidth{\textbf{Chapter}\hfill\textbf{Page}}\bigskip
\SetupNextPage}
\newcommand*{\SetupNextPage}
{\afterpage {\ifintoc\PrintChapterTocHeadline\fi}}
\addtocontents{toc}{\protect\PrintChapterTocHeadline}
\begin{document}
\begingroup\intoctrue
\tableofcontents
\endgroup
\chapter{Test}
\section{Test}
\section{Test}
\chapter{Test}
\section{Test}
\section{Test}
\chapter{Test}
\section{Test}
\section{Test}
\chapter{Test}
\section{Test}
\section{Test}
\chapter{Test}
\section{Test}
\section{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\end{document}
As this is done with Roman Numerals for chapters, some rather big spaces are used on the left.
% solution using etoc
\documentclass{book}
\usepackage[textheight=10cm]{geometry}
\usepackage{hyperref}
\usepackage{etoc}
\usepackage{afterpage}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% doing the thing for the chapter headline one each page.
%%%% here, using etoc.
\newif\ifintoc
\newcommand*{\PrintChapterTocHeadline}
{\hbox to \linewidth{\textbf{Chapter}\hfill\textbf{Page}}\bigskip
\afterpage {\ifintoc\PrintChapterTocHeadline\fi}}
% after title hook will take care of the first chapter headline
% nothing is written to the .toc file, hence other TOCs in the
% document can be done, not using that extra thing. Naturally
% the hook will have to be redefined
\renewcommand{\etocaftertitlehook}
{\parindent 0pt
\leftskip 0cm
\rightskip \tocright
\parfillskip -\rightskip
\PrintChapterTocHeadline
\intoctrue }
% etoc always typesets TOCs in a group. Thus the \ifintoc boolean
% will be false after the TOC completes.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Now using etoc also to customize the formatting itself.
%%%% I take this opportunity to write down some rather general
%%%% etoc line style definitions, for a look somewhat like the
%%%% standard looks, but customisable.
%%%% some dimensions are hard-coded, but for some I already define
%%%% associated lengths
\newlength{\tocright}
\setlength{\tocright}{0.75cm}
\newlength{\toclefti}
\setlength{\toclefti}{1.25cm}
\newlength{\tocleftii}
\setlength{\tocleftii}{2.75cm}
\newlength{\tocleftiii}
\setlength{\tocleftiii}{4.25cm}
% Code for page numbers on the right side.
\renewcommand*\etoctoclineleaders
{\hbox{\normalfont\normalsize\hbox to .6ex {\hss.\hss}}}
\newcommand*{\EndParWithPagenoInMarginAndLeaders}
{\nobreak\leaders\etoctoclineleaders\hfill
\nobreak\makebox[\tocright][r]{\mdseries\normalsize\etocpage}%
\par }
% version with no dot leaders:
\newcommand*{\EndParWithPagenoInMargin}
{\nobreak\hfill
\nobreak\makebox[\tocright][r]{\mdseries\normalsize\etocpage}%
\par}
\etocsetstyle{chapter}
{}
{\leavevmode\leftskip \toclefti }
{\llap{\makebox[\toclefti][l]{\bfseries\etocnumber.}}%
\etoclink{\bfseries\MakeUppercase{\etocthename}}% roman uppercase
\EndParWithPagenoInMargin}
{}
\etocsetstyle {section}
{}
{\leavevmode\leftskip \tocleftii }
{\llap{\makebox[\dimexpr\tocleftii-\toclefti\relax][l]{\etocnumber.}}%
\textsc{\etocname}% small caps
\EndParWithPagenoInMarginAndLeaders}
{\medskip}
\etocsetstyle {subsection}
{}
{\leavevmode\leftskip \tocleftiii }
{\llap{\makebox[\dimexpr\tocleftiii-\tocleftii\relax][l]{\etocnumber.}}%
\etocname
\EndParWithPagenoInMarginAndLeaders}
{\smallskip}
%\renewcommand*{\etoctoprule}{\hrule height 0pt\relax}
%\etocruledstyle [1]{\MakeUppercase{\contentsname}}
\etocsettocstyle
{\hbox to \linewidth{\hss \MakeUppercase{\contentsname}\hss}\bigskip}
{}
\renewcommand{\thechapter}{\Roman{chapter}}
\begin{document}
\tableofcontents
\chapter{Test}
\section{Test}
\subsection{subsection}
\subsection{subsection}
\section{Test}
\subsection{subsection}
\subsection{subsection}
\chapter{Test}
\section{Test}
\subsection{subsection}
\subsection{subsection}
\section{Test}
\subsection{subsection}
\subsection{subsection}
\chapter{Test}
\section{Test}
\section{Test}
\chapter{Test}
\section{Test}
\section{Test}
\chapter{Test}
\section{Test}
\section{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\subsection{subsection}
\subsection{subsection}
\chapter{Test}
\section{Test}
\subsection{subsection}
\subsection{subsection}
\section{Test}
\subsection{subsection}
\subsection{subsection}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\chapter{Test}
\section{Test}
\end{document}
Here's one possible solution using the titletoc
package for the partial ToCs and a variation of this answer
to Chapterwise page range in the TOC to generate the page ranges in the main ToC.
Use the \MiniToC
after the \chapter
command for chapters in which you want to typeset a partial ToC.
The code has to be compiled three times to get the right numbers for the page ranges in the main ToC.
\documentclass{report}
\usepackage{titletoc}
\usepackage{letltxmacro}
\usepackage{etoolbox}
\usepackage{lipsum}
\newcommand\MiniToC{%
\setcounter{tocdepth}{2}
\startcontents
\printcontents{}{1}{\section*{\contentsname}\vskip-3.5ex\hrulefill\vskip1ex}
\vskip-0.5ex\noindent\hrulefill
\setcounter{tocdepth}{0}
}
\setcounter{tocdepth}{0}
\makeatletter
\newif\if@chap@enddc
\@chap@enddctrue
\LetLtxMacro\ltx@@chapter\@chapter
\renewcommand\@chapter[2][]{%
\ltx@@chapter[#1]{#2}
\expandafter\label{chap:\thechapter}
}
\let\ltx@toc\tableofcontents
\renewcommand\tableofcontents{%
\ltx@toc
\let\ltx@chapter\chapter
\renewcommand\chapter{%
\expandafter\label{prenextchap:\thechapter}
\ltx@chapter
}%
}
\let\ltx@enddocument\enddocument
\renewcommand\enddocument{%
\if@chap@enddc\expandafter\label{prenextchap:\thechapter}\fi
\ltx@enddocument
}
\def\chaprange{%
\expandafter\pageref{chap:\thechapter}--\expandafter\pageref{prenextchap:\thechapter}}
\let\ltx@addcontentsline\addcontentsline
\def\CR@addcontentsline#1#2#3{%
\addtocontents{#1}{\protect\contentsline{#2}{#3}{\chaprange}}
}
\def\ToggleChaprange{\let\addcontentsline\CR@addcontentsline}
\def\BypassChaprange{\let\addcontentsline\ltx@addcontentsline}
\def\BreakChaprange{%
\expandafter\label{prenextchap:\thechapter}
\let\addcontentsline\ltx@addcontentsline
\@chap@enddcfalse
}
\makeatother
\pretocmd{\section}{\BypassChaprange}{}{}
\pretocmd{\chapter}{\ToggleChaprange}{}{}
\begin{document}
\tableofcontents
\chapter{Test chapter one}
\MiniToC
\section{A test section}
\subsection{A test subsection}
\subsubsection{A test subsubsection}
\lipsum[1-30]
\chapter{Test chapter two}
\MiniToC
\section{A test section}
\subsection{A test subsection}
\subsubsection{A test subsubsection}
\lipsum[1-20]
\chapter{Test chapter three}
\MiniToC
\section{A test section}
\subsection{A test subsection}
\subsubsection{A test subsubsection}
\lipsum[1-10]
\end{document}
An image of the main ToC:
An image of the partial ToC for the first chapter:
Best Answer
This is an approach which uses the »tocstyle« package with the
tocflat
option along with thestandard
style.You can compare with the
tocfullflat
option for equidistant spaces between numbers and heading entries. More details can be found in the package manual.N.B.: You will get a warning about the alpha status of the package. So far is has not caused any major problems for me.