3

I am trying to get the part number appended to a chapter within that part. E.g. I would like to get:

I Part One
   I.1 Chapter One
   I.2 Chapter Two
II Part Two
   II.3 Chapter Three
   II.4 Chapter Four

I'm using titlesec with the newparttoc option and titletoc. I've read that part should be redefined w/ titlesec if I want titletoc to be able to access its components, so I have done so.

I should mention I do not want to change the format throughout the document (i.e. redefining \thechapter).

I redefined the chapter toc entry as:

\titlecontents{chapter}[1.5em]
{\vspace{1.2em}\bfseries}
{\thepart\thecontentslabel\hspace{1.5em}}
{}
{\hfill\contentspage}

but it doesn't seem to have any effect. The result is: mwe_output

I'd really appreciate any thoughts.

My MWE is:

\documentclass[a4paper, twoside, openright, 12pt]{book}

%% Titles
%
\usepackage[newparttoc]{titlesec}
\titleformat{\chapter}[display]
  {\vspace{3pc}\raggedleft\scshape\huge\bfseries}
  {\chaptertitlename~\thechapter}
  {0.2em}
  {\raggedleft\normalfont\scshape\huge}
  [\vspace{2pc}]

\titleformat{\part}[display]
  {\titlerule[2pt]\centering\scshape\bfseries\Huge}
  {Part \thepart}
  {0.5em}
  {\centering\normalfont\itshape\Huge}
  [\titlerule\vspace{1em}]


\usepackage{titletoc}
\titlecontents{chapter}[1.5em]
{\vspace{1.2em}\bfseries}
{\thepart\thecontentslabel\hspace{1.5em}}
{}
{\hfill\contentspage}%[\hfill]%[ ]

\pagestyle{empty}

\title{ToC Test}

\begin{document}
\frontmatter
\maketitle

\mainmatter
\tableofcontents
\part{Part One}

\chapter{Chapter One}
\chapter{Chapter Two}

\part{Part Two}
\chapter{Chapter Three}
\chapter{Chapter Four}

\end{document}
2
  • Welcome to TeX SX! But you don't want to add the part number to the chapter number in the chapter heading?
    – Bernard
    Sep 4, 2019 at 17:43
  • Thanks! No, I don't want it in the chapter heading. I just need it modified in the ToC. This is because in my working document I have a chapter (say Chapter Five) which is not in Part Two, and therefore would not have the part number appended, making it very clear it's outside of it.
    – inutza
    Sep 4, 2019 at 18:00

3 Answers 3

1

The following does what you want (changes are pointed out with %% <----- arrows)

\documentclass[a4paper, twoside, openright, 12pt]{book}

%% Titles
%
\usepackage[newparttoc]{titlesec}
\let\oldthechapter\thechapter    %% <----------  
\def\thechapter{\thepart.\oldthechapter}  %% <--------
\titleformat{\chapter}[display]
  {\vspace{3pc}\raggedleft\scshape\huge\bfseries}
  {\chaptertitlename~\oldthechapter}  %% <----------
  {0.2em}
  {\raggedleft\normalfont\scshape\huge}
  [\vspace{2pc}]

\titleformat{\part}[display]
  {\titlerule[2pt]\centering\scshape\bfseries\Huge}
  {Part \thepart}
  {0.5em}
  {\centering\normalfont\itshape\Huge}
  [\titlerule\vspace{1em}]


\usepackage{titletoc}
\titlecontents{chapter}[1.5em]
{\vspace{1.2em}\bfseries}
{\thecontentslabel\hspace{1.5em}}   %% <---------
{}
{\hfill\contentspage}%[\hfill]%[ ]

\pagestyle{empty}

\title{ToC Test}

\begin{document}
\frontmatter
\maketitle

\mainmatter

\tableofcontents

\part{Part One}
\chapter{Chapter One}
\chapter{Chapter Two}

\part{Part Two}
\chapter{Chapter Three}
\chapter{Chapter Four}

\end{document}

Explanation: since you didn't redefine \thechapter, when \chapter{Chapter One} writes to the .toc file it writes

\contentsline {chapter}{\numberline {1}Chapter One}{5}%

which when read-back to write the TOC, the value \thecontentslabel is replaced by 1 and \thepart is empty, resulting in what you saw.

In other words: the invocation of \titlecontents control how to format the stuff written in the .toc file, but not how the .toc file contents are written. And when you call \tableofcontents it is before the first \part, and so \thepart returns nothing.

To make it work with the chapter heading displaying different things from the TOC with \titleformat, instead of using \thechapter in \titleformat and ask the TOC to behave differently, we will modify and use \thechapter in the TOC and ask \titleformat to behave differently. Of course, this would mean that any running header with chapter numbers will show it with the parts also. (You should edit those to also use \oldthechapter instead if it bothers you.)

To make your chapter 5 work, you can call \let\thechapter\oldthechapter before \chapter{Chapter Five} and again \def\thechapter{\thepart.\oldthechapter} before the next chapter.

3
  • Depending on your workflow, it may be best to redefine \part so that it calls the \def\thechapter... command, and define a new command that both resets \thechapter and inserts a blank line into the TOC. Sep 4, 2019 at 20:59
  • Thank you! That explains very well why it didn't work. I've managed to edit everything such that it it works as I wanted. Sorted it out for headers, headings, sections, figures, and tables. The only thing I haven't managed to fix is, if I \Cref a chapter which is included in a part, it gets prefixed with the part number (e.g. I.1). It's not a massive issue, but it would be cool to iron that one out too.
    – inutza
    Sep 5, 2019 at 13:09
  • With that info, another approach is needed. I will post a second answer in a moment. Sep 5, 2019 at 17:25
0

A solution with \counterwithin. I didn't test for headers, not knowing how you want them, but if there's any problem, it should be easy to solve.

\documentclass[a4paper, twoside, openright, 12pt]{book}

%% Titles
\usepackage[newparttoc, explicit]{titlesec}
\titleformat{\chapter}[display]
  {\counterwithin{chapter}{part}\vspace{3pc}\raggedleft\scshape\huge\bfseries}
  {\chaptertitlename~\arabic{chapter}}
  {0.2em}
  {\raggedleft\normalfont\scshape\huge #1}
  [\vspace{2pc}]

\titleformat{\part}[display]
  {\titlerule[2pt]\centering\scshape\bfseries\Huge}
  {Part \thepart}
  {0.5em}
  {\centering\normalfont\itshape\Huge#1}
  [\titlerule\vspace{1em}]

\usepackage{titletoc}
\titlecontents{chapter}[1.5em]
{\vspace{1.2em}\bfseries}
{\thecontentslabel\hspace{1.5em}}
{}
{\hfill\contentspage}%[\hfill]%[ ]

\pagestyle{empty}
\author{My Self}
\title{ToC Test}

\begin{document}
\frontmatter
\maketitle

\mainmatter
\tableofcontents
\part{Part One}

\chapter{Chapter One}
\chapter{Chapter Two}

\part{Part Two}
\chapter{Chapter Three}
\chapter{Chapter Four}

\end{document} 

enter image description here

1
  • Thanks! That works well also. I've accepted the other answer because it had the explanation of why it wasn't working, and I was really interested in it. Cheers.
    – inutza
    Sep 5, 2019 at 13:33
0

The OP's comment clarified that the only thing that should be changed should be the ToC listing, and nothing else. So my other answer is less than optimal because everywhere the counter for \thechapter is used what shows up will be effected. In particular, when labels are written to the .aux file it will include also the part information making it show up also in calls to \ref.

In this case the easiest thing to do would be to get your hands dirty and actually create a new chapter command which adds the part info only to the .toc file and otherwise behaves exactly like the ordinary \chapter. To do so, you can first dive into the document class you are using (which is book.cls), and see that \chapter is defined by

\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{plain}%
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thechapter}#1}%
                       \else
                         \addcontentsline{toc}{chapter}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{chapter}{#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}

There are of course other commands involved in building the \chapter command, but the one that concerns us, where the ToC entry is written, is in \@chapter, where the \addcontentsline command is called. Specifically it reads

\addcontentsline{toc}{chapter}%
       {\protect\numberline{\thechapter}#1}%

So all we need to do is to create a new command that does exactly the samething as \chapter except swap out the \numberline{\thechapter} bit with \numberline{\thepart.\thechapter}. If you always want to use this particular format in the ToC, we can just patch \@chapter by redefining it. But we want to also have the possibility of \chapter{Chapter Five} that doesn't show the part info. So we will define a new command called \pchapter that does everything like \chapter except this one minor addition.

The following code makes this change. (Note for convenience I removed the formatting commands for titlesec and titletoc; they should not be effected by the code, since titlesec patches \@makechapterhead to achieve the formatting, which we still call in the new \@pchapter.)

\documentclass[a4paper, twoside, openright, 12pt]{book}

\usepackage[newparttoc]{titlesec}

\makeatletter
% The \pchapter command differs from \chapter only in changing to call 
% to \@chapter to \@pchapter
\newcommand\pchapter{\if@openright\cleardoublepage\else\clearpage\fi
            \thispagestyle{plain}%
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@pchapter\@schapter}
% As discussed above, the \@pchapter command only changes in how it calls 
% \addcontentsline
\def\@pchapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thepart.\thechapter}#1}%
                       \else
                         \addcontentsline{toc}{chapter}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{chapter}{#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

\pagestyle{empty}

\title{ToC Test}

\begin{document}
\frontmatter
\maketitle

\mainmatter

\tableofcontents

\part{Part One}
% Calls to \pchapter records I.1 and I.2 in the ToC
\pchapter{Chapter One}
\pchapter{Chapter Two}

\part{Part Two}
\pchapter{Chapter Three}
\pchapter{Chapter Four}

% Call to \chapter records 5 in the ToC
\chapter{Chapter Five}
\end{document}

You will probably still need to use titletoc or at least do something to make enough room for the labels in the ToC.

2
  • Completely as an aside: as to your original problem of trying to visually distinguish chapters within parts with chapters outside parts in the TOC, have you considered just inserting something (like a line or a fleuron) between parts? You can do this with \addtocontents{toc}{stuff to add}, though you should beware of addtocontents writing stuff out of order which can happen. Sep 5, 2019 at 18:04
  • That is absolutely brilliant. Thank you so much for all the help! It is exactly what I needed. Also, your explanations are very clear, I really appreciate that. I did not consider adding something between parts, but now that you have mentioned I also did that, and it looks really cool. I'm also keeping the part prefix though, I want the separation to be as clear as possible (it's for my thesis, so not taking any risks). Thank you again!
    – inutza
    Sep 6, 2019 at 15:26

You must log in to answer this question.

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