13

I have:

\chapter{My first chapter}
\chapter{My second chapter}
\phantomsection
\addcontentsline{toc}{chapter}{Appendixes}
\appendix
\makeatletter
\def\toclevel@chapter{1}\def\toclevel@section{2}
\makeatother
\chapter{My first appendix}
\chapter{My second appendix}

So my hyperref table of contents looks like:

- My first chapter
- My second chapter
+ Appendixes
  - My first appendix
  - My second appendix

But my \tableofcontents still looks like:

1 My first chapter
2 My second chapter
Appendixes
A My first appendix
B My second appendix

And what I want is that the appendixes look like sections:

1 My first chapter
2 My second chapter
Appendixes
  A My first appendix
  B My second appendix

I can figure out, that there is a really small trick to do the job, but I can't find it...

2
  • how about \addcontentsline{toc}{section}{Appendixes}? `
    – cmhughes
    Commented Jun 16, 2012 at 2:00
  • Which document class are you using? Commented Jun 16, 2012 at 3:22

4 Answers 4

9

set \l@chapter to l@section

\documentclass{book}
\usepackage{hyperref}

\begin{document}

\tableofcontents
\chapter{My first chapter}
\chapter{My second chapter}

\phantomsection
\addcontentsline{toc}{chapter}{Appendixes}
\appendix
\def\toclevel@chapter{1}\def\toclevel@section{2}
\addtocontents{toc}{\string\let\string\l@chapter\string
\l@section}
\chapter{My first appendix}
\chapter{My second appendix}

\end{document}
1
  • 1
    Unfortunately this MWE does not work for me. The appendix chapters appear on the same line of the toc and the strings pop up, here is what I copied out of the pdf generated: @section lA My rst appendix7 lB My second appendix9. Any suggestions?
    – ted
    Commented Aug 10, 2013 at 12:26
7

You can redefine \@chapter (as implemented in the document class used) to use \addcontentsline{toc}{section}{...} instead of \addcontentsline{toc}{chapter}{...}. Here's an example of such redefinition for book.cls:

\documentclass{book}
\usepackage{hyperref}

\begin{document}

\tableofcontents
\chapter{My first chapter}
\chapter{My second chapter}

\phantomsection
\addcontentsline{toc}{chapter}{Appendixes}
\appendix
\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{section}%
                                   {\protect\numberline{\thechapter}#1}%
                       \else
                         \addcontentsline{toc}{section}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{section}{#1}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
\makeatother

\chapter{My first appendix}
\chapter{My second appendix}

\end{document}

Here's the resulting ToC:

enter image description here

and the bookmarks pabel:

enter image description here

1
  • I want it class independant (like Herbert's answer) Commented Jun 18, 2012 at 0:04
4

The following is a condensed version of Gonzalo's answer, using regexpatch. It patches the first appearance of {toc}{chapter} in \Hy@org@chapter with {toc}{section}, yielding the same result:

\documentclass{book}
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch

\begin{document}

\tableofcontents
\chapter{My first chapter}
\chapter{My second chapter}

\phantomsection
\addcontentsline{toc}{chapter}{Appendixes}
\appendix
\makeatletter
\xpatchcmd{\Hy@org@chapter}{{toc}{chapter}}{{toc}{section}}{}{}%
\makeatother

\chapter{My first appendix}
\chapter{My second appendix}

\end{document}

If you wish to not only print the \chapters at section level, but push the entire hierarchy down a level, things get a little more messy. Specifically because the sectional units are all governed by a single macro (perhaps since they're common to all basic document classes) called \@startsection. So, one could redefine or patch the "type" or "level" of the macro for each sectional unit. This has no effect on hyperref. For example:

\xpatchcmd{\section}{section}{subsection}{}{}
\xpatchcmd{\subsection}{subsection}{subsubsection}{}{}
\xpatchcmd{\subsubsection}{subsubsection}{paragraph}{}{}
\xpatchcmd{\paragraph}{paragraph}{subparagraph}{}{}

Of course, there's no level to demote \subparagraph, but hopefully that level of detail is not included in your ToC.


If all of the above should be localized so you can easily revert back to the default layout when needed, then I would suggest making a copy of any of the macros before patching them:

\let\storedchapter\Hy@org@chapter
\let\storedsection\section
\let\storedsubsection\subsection
\let\storedsubsubsection\subsubsection
\let\storedparagraph\paragraph

This way you can revert the process later, if needed.

3
  • how do I extend this for lower hierarchy levels (section, subsection, subsubsection,...). Especially what do I do with the lowest hierarchy level to push it one level down? (fortunately the last part is pure curiosity since I dont use it in my toc). Also: can I reverse this effect if I would like to add a chapter with the default hierarchy to the toc afterwards?
    – ted
    Commented Aug 10, 2013 at 12:52
  • @ted: I've added some detail.
    – Werner
    Commented Aug 11, 2013 at 4:08
  • 1
    While one should refrain from tahnk you notes, I have to use one here, since I have already upvoted there is no other way to let you know the effort has not gone unnoticed and is appreciated.
    – ted
    Commented Aug 11, 2013 at 7:46
0

Here is another solution. It uses the addcontentsline to insert the chapter on the level you choose, and useses the starred variant to typeset the title with the chapter formatting without adding anything to the table of contents. letltxmacro is used since I get a circular reference otherwise.

\documentclass{book}
\usepackage{hyperref}
\usepackage{letltxmacro}

\begin{document}

\tableofcontents
\chapter{My first chapter}
\chapter{My second chapter}

\LetLtxMacro{\chapNoToc}{\chapter*}
\renewcommand{\chapter}[1]{%
\chapNoToc*{#1}
\phantomsection
\markboth{#1}{#1}
\addcontentsline{toc}{section}{#1}%
}

\chapter{My first appendix}
\chapter{My second appendix}

\end{document}

This solution could be exteneded to support chapters optional argument, however I do not know how to insert the first argument or an otpional argument depending on the presence of the later of the top of my head.

It might be a good idea to save \chapter with \LetLtxMacro so it can be restored later. Add \LetLtxMacro{\oldChapter}{\chapter} before you redfine \chapter and use \LetLtxMacro{\chapter}{\oldChapter} to restore the default chapter behaviour.

1
  • @lockstep: did I get it right now? (You commented that I was missing the title markings). However my knowledge is from google, so I am unsure if this covers all cases now.
    – ted
    Commented Aug 10, 2013 at 13:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .