2
\documentclass[openany]{book}
\raggedbottom       %reduce underfull \vbox

\usepackage{titlesec, titletoc}
\titlelabel{\thetitle\quad}
%change the part format in book 
\titleformat{\part}[frame]{\normalfont\huge\bfseries}{\partname\ \thepart }{20pt}{\Huge\centering}
%change the part format in table of contents
\titlecontents{part}%
[0pt]{\sffamily\bfseries\huge\protect\addvspace{25pt}\titlerule\addvspace{1.5ex}}%remove rule if you like
{}{\partname~}
{\hfill\contentspage}%replaced with {} if don't want page number for parts
[\addvspace{0.7ex}\titlerule\addvspace{10pt}]%remove rule if you like

%change the chapter format in table of contents
\titlecontents{chapter}%
[0pt]{\sffamily\bfseries\large\protect\addvspace{10pt}}%
{\chaptertitlename\ \thecontentslabel~}{}
{\hfill\contentspage}%
[\addvspace{1.5ex}]%

%change the appendix name from chapter to appendix
\makeatletter
\g@addto@macro{\appendix}{%
  \renewcommand\chaptername{Appendix}%
  \addtocontents{toc}{\protect\renewcommand*{\protect\chaptername}{\protect\appendixname}}%
}
\makeatother
%force paragraph newline
\titleformat{\paragraph}[hang]{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}


%insert figures
\usepackage{graphicx}

%improve the interface for defining floating objects such as figures and tables
\usepackage{float}

%customize page layout
\usepackage[a4paper, left=3cm, right=3cm]{geometry}

%color
\usepackage[dvipsnames]{xcolor}

%page headers and footers   left,right,odd,even
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\normalfont\small\rmfamily\nouppercase{\leftmark}}
\fancyhead[LO]{\normalfont\small\rmfamily\nouppercase{\rightmark}}
\fancyhead[LE,RO]{\thepage}

%booktitle
\title{\Huge C++ \\\qquad\textcolor{RedOrange}{Primer}}
\author{\textbf{Stanley B.Lippman} \and \textbf{Jos\'ee Lajoie} \and \textbf{Barbara E.Moo}}
\date{}


%revise\@endpart macro
\makeatletter
\def\@endpart{\bigbreak} 
\makeatother

% add mini-ta­bles-of-con­tents (mini­tocs) at the be­gin­ning of ev­ery chap­ter, part or sec­tion.
\usepackage{minitoc}
\setcounter{minitocdepth}{1} 
\renewcommand\tightmtcfalse

\begin{document}
%initialize minitoc
\dominitoc 

\frontmatter
\maketitle
\tableofcontents



\mainmatter
\chapter{Getting Started}
\minitoc 
%\startcontents[mainsections]
%\printcontents[mainsections]{l}{1}{\section*{Main Sections}\setcounter{todepth}{1}}

This chapter introduces most of the basic elements of C++: types, variables,
expressions, statements, and functions. Along the way, we’ll briefly explain how to
compile and execute a program.

After having read this chapter and worked through the exercises, you should be able
to write, compile, and execute simple programs. Later chapters will assume that you
can use the features introduced in this chapter, and will explain these features in more
detail.
\newpage
    \section{Writing a Simple C++ Program}

    \section{A First Look at Input/Output}\label{a first look at input/output}

    \section{A Word about Comments}
    %\stopcontents[mainsections]

\backmatter
\chapter{Index}

\end{document}

    \section{A First Look at Input/Output}\label{a first look at input/output}

    \section{A Word about Comments}
    %\stopcontents[mainsections]

\backmatter
\chapter{Index}

\end{document}

enter image description here

Since the 'titletoc' and 'minitoc' are incompatible, I want to remove the package 'minitoc' and make the part toc with 'titletoc'.

  • Provoking suggestion: Drop titlesec etc. rather ;-) – user31729 Sep 23 '17 at 6:06
  • I need 'titletoc' to format my table of content, as you can see. – K.Robert Sep 23 '17 at 6:10
4

Maybe you want something like this.

krobertTitletoc

\documentclass[openany]{book}
\raggedbottom       %reduce underfull \vbox

\usepackage{titlesec, titletoc}
\usepackage{indentfirst}% indents first paragraph after a section/chapter heading
\titlelabel{\thetitle\quad}
%change the part format in book 
\titleformat{\part}[frame]{\normalfont\huge\bfseries}{\partname\ \thepart }{20pt}{\Huge\centering}
%change the part format in table of contents
\titlecontents{part}%
[0pt]{\sffamily\bfseries\huge\protect\addvspace{25pt}\titlerule\addvspace{1.5ex}}%remove rule if you like
{}{\partname~}
{\hfill\contentspage}%replaced with {} if don't want page number for parts
[\addvspace{0.7ex}\titlerule\addvspace{10pt}]%remove rule if you like

%change the chapter format in table of contents
\titlecontents{chapter}%
[0pt]{\sffamily\bfseries\large\protect\addvspace{10pt}}%
{\chaptertitlename\ \thecontentslabel~}{}
{\hfill\contentspage}%
[\addvspace{1.5ex}]%

%change the appendix name from chapter to appendix
\makeatletter
\g@addto@macro{\appendix}{%
  \renewcommand\chaptername{Appendix}%
  \addtocontents{toc}{\protect\renewcommand*{\protect\chaptername}{\protect\appendixname}}%
}
\makeatother
%force paragraph newline
\titleformat{\paragraph}[hang]{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}

\titleformat{\chapter}[display]{\normalfont\bfseries\huge}{\chaptertitlename\ \thechapter}{20pt}{\Huge}[%
\vspace*{4pc}
\startcontents
{\normalsize\bfseries\contentsname\endgraf\vspace{5pt}}
\contentsmargin{1em}
\titlerule\vspace{5pt}
\printcontents{l}{1}{}
\vspace{5pt}\titlerule
]
\titlecontents*{lsection}[10pt]{\small}{}{}{\titlerule*[1pc]{.} \contentspage}[\endgraf]

%insert figures
\usepackage{graphicx}

%improve the interface for defining floating objects such as figures and tables
\usepackage{float}

%customize page layout
\usepackage[a4paper, left=3cm, right=3cm]{geometry}

%color
\usepackage[dvipsnames]{xcolor}

\usepackage{blindtext}
%page headers and footers   left,right,odd,even
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\normalfont\small\rmfamily\nouppercase{\leftmark}}
\fancyhead[LO]{\normalfont\small\rmfamily\nouppercase{\rightmark}}
\fancyhead[LE,RO]{\thepage}

%booktitle
\title{\Huge C++ \\\qquad\textcolor{RedOrange}{Primer}}
\author{\textbf{Stanley B.Lippman} \and \textbf{Jos\'ee Lajoie} \and \textbf{Barbara E.Moo}}
\date{}


%revise\@endpart macro
\makeatletter
\def\@endpart{\bigbreak} 
\makeatother


\begin{document}

\startcontents[chapters]
\tableofcontents


\chapter{Getting Started}

This chapter introduces most of the basic elements of C++: types, variables,
expressions, statements, and functions. Along the way, we’ll briefly explain how to
compile and execute a program.

After having read this chapter and worked through the exercises, you should be able
to write, compile, and execute simple programs. Later chapters will assume that you
can use the features introduced in this chapter, and will explain these features in more
detail.
\blindtext
\newpage
    \section{Writing a Simple C++ Program}

    \section{A First Look at Input/Output}\label{a first look at input/output}

    \section{A Word about Comments}


\end{document}
  • how to add the text "Contents" above the title rule and add a leader between the section name and the page number? – K.Robert Sep 23 '17 at 7:28
  • I have known how to add a leader, I would appreciate it that if you can tell me how to add the text "Contents" above the title rule and make the first paragraph below indent automatically. – K.Robert Sep 23 '17 at 10:44
  • @K.Robert See the updated answer. – Johannes_B Sep 23 '17 at 11:51
  • your code add "contents" and two hline to table of content, too. – K.Robert Sep 23 '17 at 12:36
  • I solve it by defining a new command named startmycontents and add the new command at each chapter instead of putting startcontents in the optional pararmeter '\titleformat'. Thx! – K.Robert Sep 23 '17 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.