Page LayoutChanging the table of contents indent of appendices

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
bjervell
Posts: 3
Joined: Tue Mar 15, 2011 7:25 pm

Changing the table of contents indent of appendices

Post by bjervell »

Hi all!

I want to modify my table of contents by changing the indent of the appendix entries. The result I am looking for is this:

Code: Select all

1     Section 1
1.1   Subsection
1.1.1 Subsubsection
2     Section 2
3     Section 3
      Appendix A First appendix
So far I have used the tocloft package to customise the TOC, but I have not been able to create the abovementioned effect...

This is in essence what I have tried in my .cls file so far:

Code: Select all

\renewcommand\tableofcontents{
  % indents
  \setlength{\cftsecindent}{0mm}
  \setlength{\cftsubsecindent}{0mm}
  \setlength{\cftsubsubsecindent}{0mm}
  \setlength{\cftparaindent}{0mm}
}

\renewcommand{\appendix}{%
 \setlength{\cftsecindent}{20mm}
 \setlength{\cftsubsecindent}{20mm}
 \setlength{\cftsubsubsecindent}{20mm}
 \setlength{\cftparaindent}{20mm}
}
The idea is to set the original indent (the indent to be used for the (sub)sections in the document) in a renewed \tableofcontents, and then override these values as soon as the \appendix command is encountered in the document.

But this does not work...

Any help will be greatly appreciated :!:

--
Bjorn Jervell Hansen

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: Changing the table of contents indent of appendices

Post by gmedina »

Hi,

Which document class are you using?
1,1,2,3,5,8,13,21,34,55,89,144,233,...
bjervell
Posts: 3
Joined: Tue Mar 15, 2011 7:25 pm

Re: Changing the table of contents indent of appendices

Post by bjervell »

I am using the article document class.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Changing the table of contents indent of appendices

Post by gmedina »

Here's one possible solution, assuming that you are not using and will not use \paragraph. Copy the code between the %%%%%%%%%%%%%%% lines in the preamble of your document:

Code: Select all

\documentclass{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{mysection}
\renewcommand\themysection{\Alph{mysection}}

% Writes the entry in the ToC with the required indentation
\newcommand\ToToC[1]{%
  \addcontentsline{toc}{section}   {\hspace*{4em}\appendixname~\themysection\hspace*{1em}#1}
}

\makeatletter
\newcommand\mysection{\refstepcounter{mysection}%
  \@startsection{paragraph}{4}{\z@}%
  {-3.5ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\normalfont\Large\bfseries}}
\renewcommand*\l@section{\@dottedtocline{1}{0em}{4em}}%
\renewcommand*\l@subsection{\@dottedtocline{2}{0em}{4em}}%
\renewcommand*\l@subsubsection{\@dottedtocline{3}{0em}{4em}}%
\newcommand*\l@mysection{\@dottedtocline{4}{1em}{1em}}%
\renewcommand\appendix{\par
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \renewcommand\theparagraph{\Alph{mysection}}
  \renewcommand\themysection{\@Alph\c@paragraph}}
\makeatother

\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{3}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\tableofcontents
\section{Test section one}
\subsection{Test subsection one one}
\section{Test section two}
\subsection{Test subsection one two}
\subsubsection{Test subsubsection one two one}
\appendix
\mysection{Test appendix one}
\ToToC{Test appendix one}
\mysection{Test appendix two}
\ToToC{Test appendix two}

\end{document}
After the \appendix command, use the \mysection command instead of the standard \section command and immediately after, use the \ToToC command to include the entry in the Table of Contents. This solution will not work well if the appendices have subsections, though.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
bjervell
Posts: 3
Joined: Tue Mar 15, 2011 7:25 pm

Re: Changing the table of contents indent of appendices

Post by bjervell »

Thanks, gmedina!

As both paragraph and subsectioning in the appendices is in use in the documents, this unfortunately doesn't quite work in my case.

Further, I would really like to avoid making new commands for sections and subsections in the appendix. If that is possible.

But at least this is something I can use as a starting point for solving my problem!
Post Reply