217

I would like to know if it's possible to have a Table of Contents looking like the following, where the number of the page is the big number on the left.

PrettytTOC

The sections could look like in the following "uggly-font-formatted" example. If someone has a better idea for the sections and subsections...

Section

2

4 Answers 4

271
+550

Here's a solution with a redefinition of the \chapter command (as implemented in book.cls) using \parboxes and the leftbar environment from the framed package:

\documentclass{book}
\usepackage{xcolor}
\usepackage{framed}

\definecolor{myred}{RGB}{127,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

\renewenvironment{leftbar}{%
  \def\FrameCommand{{\color{myyellow}\vrule width 2pt depth 6pt} \hspace{10pt}}%
  \MakeFramed {\advance\hsize-\width \FrameRestore}}%
 {\endMakeFramed}

\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addtocontents{toc}%
                                   {%
                                       {\protect\parbox{3.5em}{\hfill\Huge\color{myred}\bfseries\thepage}}%
                                         \protect\hspace*{.5em}
                                        \protect\parbox{\dimexpr\linewidth-4em\relax}{%
                                           \protect\begin{leftbar}
                                              {\scshape\small\chaptername~\thechapter}\\\sffamily#1%
                                           \protect\end{leftbar}}\par\noindent
                                    }%
                       \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

\begin{document}

\tableofcontents

\chapter{Beginning to learn design with \LaTeX}
\chapter{Beginning to learn design with HTML}
\setcounter{page}{13}% just for the example
\chapter{Beginning to learn design with HTML and some other text to span more than one line in the ToC}

\end{document}

enter image description here

The first \parbox will contain the page number; I just reserved space for two-digit numbers (3.5em); for three-digit numbers, the width of this box will have to be increased, and that of the third \parbox will have to be decreased in the same amount.

I've made some improvements and additions to my first solution. Now there will be no problems when loading hyperref (my previous solution was incompatible with hyperref); the modifications to the ToC emtries are now made with the help of the titletoc package; I've also added redefinitions for the section and subsection entries.

\documentclass{book}
\usepackage{xcolor}
\usepackage{framed}
\usepackage{titletoc}
\usepackage{etoolbox}
\usepackage{lmodern}

\definecolor{myred}{RGB}{127,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

\patchcmd{\tableofcontents}{\contentsname}{\sffamily\contentsname}{}{}

\renewenvironment{leftbar}
  {\def\FrameCommand{\hspace{6em}%
    {\color{myyellow}\vrule width 2pt depth 6pt}\hspace{1em}}%
    \MakeFramed{\parshape 1 0cm \dimexpr\textwidth-6em\relax\FrameRestore}\vskip2pt%
  }
 {\endMakeFramed}

\titlecontents{chapter}
  [0em]{\vspace*{2\baselineskip}}
  {\parbox{4.5em}{%
    \hfill\Huge\sffamily\bfseries\color{myred}\thecontentspage}%
   \vspace*{-2.3\baselineskip}\leftbar\textsc{\small\chaptername~\thecontentslabel}\\\sffamily}
  {}{\endleftbar}
\titlecontents{section}
  [8.4em]
  {\sffamily\contentslabel{3em}}{}{}
  {\hspace{0.5em}\nobreak\itshape\color{myred}\contentspage}
\titlecontents{subsection}
  [8.4em]
  {\sffamily\contentslabel{3em}}{}{}  
  {\hspace{0.5em}\nobreak\itshape\color{myred}\contentspage}

\begin{document}

\tableofcontents

\chapter{Beginning to learn design with \LaTeX}
\section{This is a test section}
\subsection{Long subsection title and some other text to span more than one line}

\newpage\setcounter{page}{123}% just for the example

\chapter{Beginning to learn design with HTML and some other text to span more than one line in the ToC}
\section{This is a test section}
\subsection{Long subsection title and some other text to span more than one line}

\end{document}

enter image description here

As Harish Kumar suggested in a comment, it will be nice to have some decorations for parts too. This new variation explores one possibility for these decorations:

\documentclass{book}
\usepackage{xcolor}
\usepackage{framed}
\usepackage{tikz}
\usepackage{titletoc}
\usepackage{etoolbox}
\usepackage{lmodern}

% definition of some personal colors
\definecolor{myred}{RGB}{127,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

% command for the circle for the number of part entries
\newcommand\Circle[1]{\tikz[overlay,remember picture] 
  \node[draw,circle, text width=18pt,line width=1pt] {#1};}

% patching of \tableofcontents to use sans serif font for the tile
\patchcmd{\tableofcontents}{\contentsname}{\sffamily\contentsname}{}{}
% patching of \@part to typeset the part number inside a framed box in its own line
% and to add color
\makeatletter
\patchcmd{\@part}
  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
  {\addtocontents{toc}{\protect\addvspace{20pt}}
    \addcontentsline{toc}{part}{\huge{\protect\color{myyellow}%
      \setlength\fboxrule{2pt}\protect\Circle{%
        \hfil\thepart\hfil%
      }%
    }\\[2ex]\color{myred}\sffamily#1}}{}{}

%\patchcmd{\@part}
%  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
%  {\addtocontents{toc}{\protect\addvspace{20pt}}
%    \addcontentsline{toc}{part}{\huge{\protect\color{myyellow}%
%      \setlength\fboxrule{2pt}\protect\fbox{\protect\parbox[c][1em][c]{1.5em}{%
%        \hfil\thepart\hfil%
%      }}%
%    }\\[2ex]\color{myred}\sffamily#1}}{}{}
\makeatother

% this is the environment used to typeset the chapter entries in the ToC
% it is a modification of the leftbar environment of the framed package
\renewenvironment{leftbar}
  {\def\FrameCommand{\hspace{6em}%
    {\color{myyellow}\vrule width 2pt depth 6pt}\hspace{1em}}%
    \MakeFramed{\parshape 1 0cm \dimexpr\textwidth-6em\relax\FrameRestore}\vskip2pt%
  }
 {\endMakeFramed}

% using titletoc we redefine the ToC entries for parts, chapters, sections, and subsections
\titlecontents{part}
  [0em]{\centering}
  {\contentslabel}
  {}{}
\titlecontents{chapter}
  [0em]{\vspace*{2\baselineskip}}
  {\parbox{4.5em}{%
    \hfill\Huge\sffamily\bfseries\color{myred}\thecontentspage}%
   \vspace*{-2.3\baselineskip}\leftbar\textsc{\small\chaptername~\thecontentslabel}\\\sffamily}
  {}{\endleftbar}
\titlecontents{section}
  [8.4em]
  {\sffamily\contentslabel{3em}}{}{}
  {\hspace{0.5em}\nobreak\itshape\color{myred}\contentspage}
\titlecontents{subsection}
  [8.4em]
  {\sffamily\contentslabel{3em}}{}{}  
  {\hspace{0.5em}\nobreak\itshape\color{myred}\contentspage}


\begin{document}

\tableofcontents

\part{Designing with \LaTeX}

\chapter{Beginning to learn design with \LaTeX}
\section{This is a test section}
\subsection{Long subsection title and some other text to span more than one line}

\newpage\setcounter{page}{123}% just for the example

\part{Designing with HTML}

\chapter{Beginning to learn design with HTML and some other text to span more than one line in the ToC}
\section{This is a test section}
\subsection{Long subsection title and some other text to span more than one line}

\end{document}

enter image description here

Unfortunately, if hyperref is loaded, the part entry formatting will be lost (I'll have to think about this).

Memoir document class

In some of the comments below there are suggestions to implement the style using the memoir document class. Below I present two variations.

First variation

This version works for numbered and unnumbered parts, chapters, sections and subsections, and cooperates with hyperref:

\documentclass{memoir}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{framed}
\usepackage{tikz}
\usepackage[colorlinks,linkcolor=black]{hyperref}

% colors to be used
\definecolor{myred}{RGB}{127,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

% a modification of the leftbar environment defined by the framed package
% will be used to place a vertical colored bar separating the page number and the
% title in chapter entries
\renewenvironment{leftbar}{%
  \def\FrameCommand{\textcolor{myyellow}{\vrule width 2pt depth 6pt}\hspace*{15pt}}%
  \MakeFramed{\advance\hsize-\width\FrameRestore}}%
 {\endMakeFramed}

% a command to circle the part numbers
\newcommand\Circle[1]{\tikz[overlay,remember picture] 
  \node[draw=myyellow,circle, text width=18pt,line width=1pt,align=center] {#1};}

% redefinition of the name of the ToC
\renewcommand\printtoctitle[1]{\HUGE\sffamily\bfseries#1}

\makeatletter
% redefinitions for part entries
\renewcommand\cftpartfont{\Huge\sffamily\bfseries\hfill}
\renewcommand\partnumberline[1]{%
    \hbox to \textwidth{\hss\Circle{\textcolor{myyellow}{#1}}\hss}%
  \vskip3.5ex\par\hfill\color{myred}}
\renewcommand*\cftpartformatpnum[1]{\hfill}
\renewcommand\cftpartafterpnum{\vskip1ex}

% redefinitions for chapter entries
\renewcommand\chapternumberline[1]{\mbox{\small\@chapapp~#1}\par\noindent\Large}
\renewcommand\cftchapterfont{\sffamily}
\cftsetindents{chapter}{0pt}{0em}
\renewcommand\cftchapterpagefont{\Huge\sffamily\bfseries\color{myred}}

\newcommand*{\l@mychap}[3]{%
  \def\@chapapp{#3}
  \vskip1ex%
  \par\noindent\begin{minipage}{\textwidth}%
  \parbox{4.5em}{%
    \hfill{\cftchapterpagefont#2}%
  }\hspace*{1.5em}%
  \parbox{\dimexpr\textwidth-4.5em-15pt\relax}{%
    \leftbar\cftchapterfont#1\hspace{1sp}\endleftbar%
  }%
  \end{minipage}\par%
}

\renewcommand*{\l@chapter}[2]{%
  \l@mychap{#1}{#2}{\chaptername}%
}

\renewcommand*{\l@appendix}[2]{%
  \l@mychap{#1}{#2}{\appendixname}%
}

% redefinitions for section entries
\renewcommand\cftsectionfont{\sffamily}
\renewcommand\cftsectionpagefont{\sffamily\itshape\color{myred}}
\renewcommand\cftsectionleader{\nobreak}
\renewcommand\cftsectiondotsep{\cftnodots}
\renewcommand\cftsectionafterpnum{\hspace*{\fill}}
\setlength\cftsectionnumwidth{12em}
\cftsetindents{section}{6em}{3em}
\renewcommand\cftsectionformatpnum[1]{%
  \hskip1em\hbox to \@pnumwidth{{\cftsectionpagefont #1\hfill}}}

% redefinitions for subsection entries
\renewcommand\cftsubsectionfont{\sffamily}
\renewcommand\cftsubsectionpagefont{\sffamily\itshape\color{myred}}
\renewcommand\cftsubsectionleader{\nobreak}
\renewcommand\cftsubsectiondotsep{\cftnodots}
\renewcommand\cftsubsectionafterpnum{\hspace*{\fill}}
\setlength\cftsubsectionnumwidth{12em}
\cftsetindents{subsection}{9em}{3em}
\renewcommand\cftsubsectionformatpnum[1]{%
  \hskip1em\hbox to \@pnumwidth{{\cftsubsectionpagefont #1\hfill}}}
\makeatother

\settocdepth{subsection}
\setsecnumdepth{subsection}

\begin{document}

\phantomsection
\addcontentsline{toc}{part}{\textcolor{myred}{Test Unnumbered Part}}
\part*{Test Unnumbered Part}

\tableofcontents*

\part{Designing with \LaTeX}
\chapter{Beginning to learn design with \LaTeX}
\section{A test section}
\subsection{A test subsection}
\phantomsection
\addcontentsline{toc}{section}{Test unnumbered section}
\section*{Test unnumbered section}

\newpage\setcounter{page}{123}% just for the example

\part{Designing with HTML}
\chapter{Beginning to learn design with HTML and some other text to span more than one line} 
\section{Another test section}

\chapter*{Test Unnumbered Chapter}
\phantomsection
\addcontentsline{toc}{chapter}{Test Unnumbered Chapter}

\end{document}

enter image description here

Second variation

projetmbc has suggested in an edit to the original question a different formatting when including sections and subsections; this is a first attempt to produce this new layout.

Some remarks:

  1. The \StartMark command must be placed before each \chapter command and \EndMark must be placed immediately after the last sectional unit of a chapter that will be included in the ToC:

  2. Page breaks can occur for the sectional units associated to a chapter and will be dealt automatically.

  3. The code needs at least three runs.

The code:

\documentclass{memoir}
\usepackage[T1]{fontenc}
\usepackage{refcount}
\usepackage[colorlinks,linkcolor=black]{hyperref}
\usepackage{tikz}
\usetikzlibrary{calc}

% colors to be used
\definecolor{myred}{RGB}{127,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

% a command to circle the part numbers
\newcommand\Circle[1]{\tikz[overlay,remember picture] 
  \node[draw=myyellow,circle, text width=18pt,line width=1pt,align=center] {#1};}

% redefinition of the name of the ToC
\renewcommand\printtoctitle[1]{\HUGE\sffamily\bfseries\color{myred}#1}

\makeatletter
% redefinitions for part entries
\renewcommand\cftpartfont{\Huge\sffamily\bfseries}
\renewcommand\partnumberline[1]{%
    \hbox to \textwidth{\hss\Circle{\textcolor{myyellow}{#1}}\hss}%
  \vskip3.5ex\color{myred}}

\renewcommand*{\l@part}[2]{%
  \ifnum \c@tocdepth >-2\relax
    \cftpartbreak
    \begingroup
      {\interlinepenalty\@M
       \leavevmode
       \settowidth{\@tempdima}{\cftpartfont\cftpartname}%
       \addtolength{\@tempdima}{\cftpartnumwidth}%
       \let\@cftbsnum \cftpartpresnum
       \let\@cftasnum \cftpartaftersnum
       \let\@cftasnumb \cftpartaftersnumb
       \advance\memRTLleftskip\@tempdima \null\nobreak\hskip -\memRTLleftskip
       \centering{\cftpartfont#1}\par%
       }
      \nobreak
        \global\@nobreaktrue
        \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}

% redefinitions for chapter entries
\renewcommand\chapternumberline[1]{\mbox{\Large\@chapapp~#1}\par\noindent}
\renewcommand\cftchapterfont{\Large\sffamily}
\cftsetindents{chapter}{0pt}{0pt}
\renewcommand\cftchapterpagefont{\HUGE\sffamily\bfseries\color{myred}}

\newcommand*{\l@mychap}[3]{%
  \def\@chapapp{#3}\vskip1ex%
  \par\noindent\begin{minipage}{\textwidth}%
  \parbox{4.7em}{%
    \hfill{\cftchapterpagefont#2}%
  }\hspace*{1.5em}%
  \parbox{\dimexpr\textwidth-4.7em-15pt\relax}{%
    \cftchapterfont#1%
  }%
  \end{minipage}\par\vspace{2ex}%
}

\renewcommand*{\l@chapter}[2]{%
  \l@mychap{#1}{#2}{\chaptername}%
}

\renewcommand*{\l@appendix}[2]{%
  \l@mychap{#1}{#2}{\appendixname}%
}

% redefinitions for section entries
\renewcommand\cftsectionfont{\sffamily}
\renewcommand\cftsectionpagefont{\sffamily\itshape\color{myred}}
\renewcommand{\cftsectionleader}{\nobreak}
\renewcommand{\cftsectionafterpnum}{\cftparfillskip}
\cftsetindents{section}{7.5em}{3em}
\renewcommand\cftsectionformatpnum[1]{%
  \hskip1em\hbox to 4em{{\cftsectionpagefont #1\hfill}}}

% redefinitions for subsection entries
\renewcommand\cftsubsectionfont{\sffamily}
\renewcommand\cftsubsectionpagefont{\sffamily\itshape\color{myred}}
\renewcommand\cftsubsectionleader{\nobreak}
\renewcommand{\cftsubsectionafterpnum}{\cftparfillskip}
\renewcommand\cftsubsectiondotsep{\cftnodots}
\cftsetindents{subsection}{10.5em}{3em}
\renewcommand\cftsubsectionformatpnum[1]{%
  \hskip1em\hbox to 4em{{\cftsubsectionpagefont #1\hfill}}}
\makeatother

\settocdepth{subsection}
\setsecnumdepth{subsection}

% length to be used when drawing a line from the top of the text area
\newlength\Myhead
\setlength\Myhead{\dimexpr\headheight+\headsep+1in+\voffset+5ex\relax}

% length to be used when drawing a line to the bottom of the text area
\newlength\Myfoot
\setlength\Myfoot{\dimexpr\paperheight-\Myhead-\textheight-\footskip+5ex\relax}

% auxiliary counter to place labels
\newcounter{chapmark}

% Adds a mark and a label at the beginning of each chapter entry in the ToC and draws a line 
% from the start of the chapter to the bottom of the text area if the mark 
% for the chapter ending lies in a different page than the one from the end of the chapter.
% (the value of tjose pages is calculated using the label)
% Must be used right before each \chapter command
\newcommand\StartMark{%
  \addtocontents{toc}{\protect\label{st\thechapmark}%
    \protect\begin{tikzpicture}[overlay,remember picture,baseline]   
    \protect\node [anchor=base] (s\thechapmark) {};%
    \ifnum\getpagerefnumber{st\thechapmark}=\getpagerefnumber{en\thechapmark} \else
    \protect\draw[myyellow,line width=3pt] let \protect\p3= (s\thechapmark),%
      \protect\p4 = (current page.south) in %
      ($ (4em,\protect\y3) + (0,-1ex) $) -- ($ (4em,\protect\y4) + (0,\protect\the\Myfoot)$);\fi
    \protect\end{tikzpicture}\par}%
}

% Adds a mark and a label at the end of each chapter entry in the ToC and draws a line from 
% the top of the text area to the ending of the chapter if the mark 
% for the chapter ending lies in a different page than the one from the start of the chapter
% if both marks are in the same page, simple draws a line connecting the marks
% (the value of tjose pages is calculated using the label)
% Must be used right after the last sectional unit (that will go to the ToC) belonging to 
% a chapter
\newcommand\EndMark{
\addtocontents{toc}{\protect\label{en\thechapmark}%
  \protect\begin{tikzpicture}[overlay,remember picture,baseline]   
  \protect\node [anchor=base] (e\thechapmark) {};
  \ifnum\getpagerefnumber{st\thechapmark}=\getpagerefnumber{en\thechapmark} 
  \protect\draw[myyellow,line width=3pt] let \protect\p1= (s\thechapmark), \protect\p2=(e\thechapmark) in ($ (4em,\protect\y1) + (0,-1ex) $) -- ($ (4em,\protect\y2) + (0,2ex) $);
  \else%
  \protect\draw[myyellow,line width=3pt] let \protect\p1= (e\thechapmark), \protect\p2=(current page.north) in ($(4em,\protect\y2) + (0,-\protect\the\Myhead)$) -- ($ (4em,\protect\y1) + (0,1.5ex) $);
\fi
\protect\end{tikzpicture}\par}\stepcounter{chapmark}%
}

\begin{document}

\tableofcontents*

\part{Designing with \TeX\ and \LaTeX}

\StartMark
\chapter{Beginning to learn design with \TeX}
\section{Another test section}
\section{Another test section }
\subsection{A test subsection}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection with a long title spanning more than one line in the table of contents}
\section{Another test section}
\section{Another test section}
\section{Another test section}
\EndMark

\StartMark
\chapter{Beginning to learn design with \LaTeX}
\section{A test section with a long title spanning more than one line in the table of contents}
\subsection{A test subsection}
\subsection{Another test subsection}
\section{A test section}
\phantomsection
\addcontentsline{toc}{section}{Test unnumbered section}
\section*{Test unnumbered section}
\subsection{A test subsection}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\EndMark

\newpage\setcounter{page}{123}% just for the example

\part{Designing with HTML}
\StartMark
\chapter{Beginning to learn design with HTML and some other text to span more than one line} 
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section with a long title spanning more than one line in the table of contents}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\EndMark

\StartMark
\chapter{Beginning to learn design with HTML and some other text to span more than one line} 
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection with a long title spanning more than one line in the table of contents}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\EndMark

\appendix
\StartMark
\chapter{Test Appendix}
\section{Another test section}
\section{Another test section with a long title spanning more than one line in the table of contents}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\EndMark

\end{document}

The obtained ToC:

enter image description here enter image description here enter image description here

Tufte

Here's a variation for the tufte-book document class:

\documentclass{tufte-book}
\usepackage{lmodern}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\newcounter{chapmark}
\setcounter{tocdepth}{1}

\colorlet{mycolor}{BrickRed}

\titlecontents{chapter}%
  [0em]
  {\vspace{1.5\baselineskip}\begin{fullwidth}\LARGE\rmfamily\itshape}
  {\hspace*{0em}\thecontentslabel}
  {\hspace*{0em}\llap{%
    \parbox[b]{40pt}{%
      \fontsize{25}{30}\sffamily\bfseries\color{mycolor}%
      \hfill\thecontentspage%
      }\hspace{20pt}%
    }%
  }
  {\rmfamily\upshape\qquad}
  [\end{fullwidth}]

\titlecontents{section}
  [0em]
  {\vspace{0\baselineskip}\begin{fullwidth}\Large\rmfamily\itshape}
  {\hspace*{0em}\contentslabel{2em}}
  {\llap{\parbox[b]{40pt}{\hfill\thecontentspage}\hspace{20pt}}\hspace*{0em}}
  {\rmfamily\upshape\qquad}
  [\end{fullwidth}]

\titlecontents{subsection}
  [0em]
  {\vspace{-.15\baselineskip}\begin{fullwidth}\large\rmfamily\itshape}
  {\hspace*{0em}\contentslabel{4em}}
  {\llap{\parbox[b]{40pt}{\hfill\small\thecontentspage}\hspace{20pt}}\hspace*{0em}}
  {\rmfamily\upshape}
  [\end{fullwidth}] 

\newcommand\StartMark{%
\addtocontents{toc}{\protect\label{st\thechapmark}%
\par\protect\begin{tikzpicture}[overlay,remember picture,baseline]   
  \protect\node [anchor=base] (s\thechapmark) {};%
  \ifnum\getpagerefnumber{st\thechapmark}=\getpagerefnumber{en\thechapmark}   
  \else
    \protect\draw[mycolor,line width=3pt] 
      let \protect\p3= (s\thechapmark),%
            \protect\p4 = (current page text area.south) in %
      ( $ (0em,\protect\y3) + (-20pt,-5ex) $ ) -- 
      ( $ (0em,\protect\y4) + (-20pt,-5pt) $ );
  \fi
\protect\end{tikzpicture}\par}%
}

\newcommand\EndMark{%
\addtocontents{toc}{\protect\label{en\thechapmark}%
\par\protect\begin{tikzpicture}[overlay,remember picture,baseline]   
  \protect\node [anchor=base] (e\thechapmark) {};
  \ifnum\getpagerefnumber{st\thechapmark}=\getpagerefnumber{en\thechapmark} 
    \protect\draw[mycolor,line width=3pt] 
    let \protect\p1= (s\thechapmark), 
          \protect\p2=(e\thechapmark) in %
    ( $ (0em,\protect\y1) + (-20pt,-5ex) $ ) -- 
    ( $ (0em,\protect\y2) + (-20pt,2ex) $ );
  \else%
  \protect\draw[mycolor,line width=3pt] 
  let \protect\p1= (e\thechapmark), 
        \protect\p2=(current page text area.north) in 
  ( $ (0em,\protect\y2) + (-20pt,5pt) $ ) -- 
  ( $ (0em,\protect\y1) + (-20pt,2ex) $ );
  \fi
\protect\end{tikzpicture}\par}%
\stepcounter{chapmark}%
}

\setcounter{page}{123}% just for the example

\begin{document}

\tableofcontents

\StartMark
\chapter{Beginning to learn design with \LaTeX}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\EndMark

\StartMark
\chapter{Beginning to learn design with HTML}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\EndMark

\StartMark
\chapter{Beginning to learn design with \LaTeX\ again}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\EndMark

\StartMark
\chapter{Beginning to learn design with \LaTeX\ again}
\section{This is a test section}
\section{This is a test section}
\section{This is a test section}
\EndMark

\end{document}

Images of the resulting ToC:

enter image description here

42
  • 2
    With your minimal code you make it look easy. Looks great!
    – Werner
    Commented Nov 23, 2011 at 0:15
  • 1
    The alignment seems to be a little off on the right hand side. Otherwise, what a beautiful implementation!
    – qubyte
    Commented Nov 23, 2011 at 11:16
  • 2
    @bobicool: when I have the time I will take a look about using memoir (although I don't use that document class). For the problem with the frenchb option for babel, simply use \shorthandoff{;} before \tableofcontents to temporarily switch off the shorthand action of the character ;. To switch back on the shorthand, you can use \shorthandon{;} after \tableofcontents. Commented Apr 2, 2012 at 14:19
  • 1
    @bobicool: I've updated my answer providing an implementation for the chapter entries with the memoir document class. Commented Apr 8, 2012 at 23:21
  • 2
    This is amazing! When trying to use \frontmatter and \mainmatter in memoir, I get the following errors for both \StartMark and \EndMark: Missing number, treated as zero \StartMark and Missing = inserted for \ifnum \StartMark. Is this fixable?
    – Holene
    Commented Apr 20, 2016 at 11:37
48

Here is a solution using ConTeXt:

\define[3]\myTOC{% #1=chapter number, #2=chapter text, #3=page
    {\ssd #3\hskip.4em}
    \framed
        [
                    frame=off,
                leftframe=on,
            rulethickness=2pt,
               framecolor=darkgreen,
                  loffset=1em,
                    align=right,
                 location=bottom,
          foregroundstyle=sans,
        ]
    {{\rm\sc Chapter #1}\\#2}
}

\setuplist [chapter]
    [
        alternative=none,
        command=\myTOC,
    ]

\starttext

\placelist [chapter]

\setnumber[userpage]{10}
\startchapter [title=Beginning to learn design with ConTeXt]
\stopchapter

\startchapter [title=Beginning to learn design with HTML]
\stopchapter

\stoptext

The result looks like this:

result

However, it is not optimal. It only defines the list style for the chapters. But since you didn't specify anything else, that's all I can do.

The command \setuplist defines the layout of the list in the first argument, here chapter. alternative=none tells the system that you don't want a predefined style, rather than to use the custom command, which defines the appearance. I set the page number to a two-digit value, otherwise it gets confusing, which number belongs to the page and which to the chapter count.

5
  • 1
    This is another great ConTeXt example! What's the use of {\sc Chapter #1} that seems to have lost the Small Caps?
    – Werner
    Commented Nov 22, 2011 at 23:28
  • Yes I saw it too and removed it from the code (and you posted the comment in the mean time ;) Does LM have sans small caps by the way?
    – Marco
    Commented Nov 22, 2011 at 23:35
  • Not that I can see; Read An exploration of the Latin Modern fonts (p 3).
    – Werner
    Commented Nov 22, 2011 at 23:45
  • 1
    @Werner According to the doc there are no sans small caps. I changed to roman ones (which in fact is ugly). The fine-tuning is is left for the OP.
    – Marco
    Commented Nov 23, 2011 at 0:03
  • Yes, @Gonzalo's answer reflects the same for LaTeX. Most likely the OP's images is from a PDF screen grab, with the original document constructed in some different publishing software.
    – Werner
    Commented Nov 23, 2011 at 0:15
26
+250

I have pached the solution proposed by Marco (1) to allow for starred version of the \chapter command and (2) to prevent wrong pagebreaks which sometimes occured (in Marco's solution) between the pagenumber and the chapter title. For the latter purpose I wrapped the \parbox that contains \thecontentspage and the leftbar environment into the minipage environment.

\usepackage{framed}
\usepackage{titletoc}
\usepackage{etoolbox}
\definecolor{myred}{RGB}{127,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

\patchcmd{\tableofcontents}{\contentsname}{\sffamily\contentsname}{}{}

\renewenvironment{leftbar}
  {\def\FrameCommand{\hspace{6em}%
    {\color{myyellow}\vrule width 2pt depth 6pt}\hspace{1em}}%
    \MakeFramed{\parshape 1 0cm \dimexpr\textwidth-6em\relax\FrameRestore}\vskip2pt%
  }
 {\endMakeFramed}

\titlecontents{chapter}
  [0em]{\vspace*{\baselineskip}}
  {%
    \begin{minipage}{\textwidth}
    \parbox{4.5em}{\hfill\Huge\sffamily\bfseries\color{myred}\thecontentspage}%
   \vspace{-2.3\baselineskip}\leftbar{\sffamily\chaptername~\thecontentslabel}\\
   \sffamily
  }
  {%
    \begin{minipage}{\textwidth}
    \parbox{4.5em}{%
    \hfill\Huge\sffamily\bfseries\color{myred}\thecontentspage}%
   \vspace{-2.3\baselineskip}\leftbar\textsf{}\\\sffamily}
  {\endleftbar\end{minipage}\nopagebreak}[\vskip2pt\nopagebreak]
\titlecontents{section}
  [8.4em]
  {\sffamily\contentslabel{3em}}{}{}
  {\hspace{0.5em}\nobreak\itshape\color{myred}\contentspage}
\titlecontents{subsection}
  [8.4em]
  {\sffamily\contentslabel{3em}}{}{}
  {\hspace{0.5em}\nobreak\itshape\color{myred}\contentspage}
2
  • This almost works with memoir ... Any chance you might be able to fix it to work with it?
    – André
    Commented Apr 7, 2012 at 13:41
  • 2
    The page number of subsubsection is not changed in TOC, please add this feature
    – KOF
    Commented Jan 12, 2013 at 22:01
14

Here is an etoc implementation. This is basically plagiarism of Gonzalo Medina's answer which I have translated with some adaptations into etoc lingua.

It is fully compatible with hyperref, and needs the usual three runs to get the TOC correct (inclusive of page numbers).

Caveat: the fanciful formatting is only for the table of contents. The body \part's, \chapter's, \section's are the standard ones. To customize them to look analogously to their TOC counterparts one may use titlesec.

Update: the code uses in a better way the \MakeFramed from the framed package; this seems to avoid strange page breaks which arose in the first version under some circumstances.

Also, the code compiles as is with the memoir class.(*) See however the code comments: particularly if one wants to allow lone chapters without sections (as would be the case in memoir with \tableofcontents which adds to the TOC a chapter-like entry for the TOC itself), a (commented-out) more sophisticated etoc chapter style must be made use of, for technical reasons which are explained therein.

(*) this needed \etocsetlevel {appendix}{0} which should arguably be made by default by etoc whenever it detects memoir.

The design has one user adjustable parameter \tocmargin which is a length, here set to have value 5.4em (computed in the default sans font in default size), inside the \etocsettocstyle command call.

The code makes some efforts with penalties to avoid badly located page breaks. This last edit has my best effort to control these pagebreaks. framed.sty makes it favorable to have a page break just at the start of any environment defined by its means, thus, it is difficult to avoid that a Chapter gets separated from the just prior Part heading. I have added some small vertical glue between sections and subsections to encourage breaks there. Adjusting the \tocmargin value can be in some situation the fix to an annoying page break (I tested it in stress situations with short pages).

The current code give a result a bit more spread out than is apparent on these images, the pages are better filled in than here, but the breaks are at the same locations.

I have also added to the source code two \localtableofcontents for the two Parts.

pretty toc 1 pretty toc 2 pretty toc 3

The code:

\documentclass{book}
%\documentclass{memoir}
%
% debugging page breaks, but too short pageheight gives bad page breaks
% anyhow, despite my best efforts
%\usepackage[paperheight=12cm]{geometry}
\usepackage{xcolor}
\usepackage{framed}
\usepackage{tikz}
\usepackage{lmodern}
\usepackage{etoc}
\usepackage[linktoc=all]{hyperref}

%% THE FOLLOWING IS TAKEN (STOLEN) VERBATIM 
%% FROM GONZALO MEDINA'S ANSWER

% definition of some personal colors
\definecolor{myred}{RGB}{127,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

% command for the circle for the number of part entries
\newcommand\Circle[1]{\tikz[overlay,remember picture] 
  \node[draw,circle, text width=18pt,line width=1pt] {#1};}


%% (jfbu) This defines an adjustable length, which gives the position of the
%% section and subsection from the left document margin.
\newlength{\tocmargin}
% Its value is set in the \etocsettocstyle command below, after \sffamily


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% USE OF framed package

% modified (jfbu): it took me a while to settle on that way of using the
% framed package. Initially I had as in Gonzalo Medina's code
% a \parshape, but it applies only to one paragraph (this was from his
% code where the left bar applied only to the chapter entries of the
% toc, not the sections and subsections). Besides, I had some problems
% with disturbing page breaks in certain circumstances, and these
% problems seem to be gone now, I needed to do adjustment to \hsize from
% inside the chapter style. This is all gone now.
\newenvironment{tocleftbar}
  {\def\FrameCommand {\hspace{\dimexpr\tocmargin-4pt\relax}%
           {\color{myyellow}\vrule width 2pt depth 6pt}\hspace{2pt}%
           \hspace{-\tocmargin}}% 
    \MakeFramed {\leftskip\tocmargin\rightskip\tocmargin\FrameRestore}}
 {\endMakeFramed}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% THE PLAGIARISM CONTINUES, WITH SOME MORE MODIFICATIONS, 
%% AND  EMBEDDED INSIDE ETOC COMMANDS

\etocsetstyle{part}
   {\nobreak\etocskipfirstprefix }
   {\pagebreak[3]}% makes a page break before a Part favorable
   {% debugging:
    % \noindent\rule{\tocmargin}{1pt}\par
    %          \rule{\dimexpr2\tocmargin\relax}{1pt}\par
    \addvspace{1.5\baselineskip}%
    {\huge\centering
     \color{myyellow}\Circle{\hfil\etocnumber\hfil}\\*[2ex]%
     \color{myred}\etocname\par}}
   {}

% totalement instable: passer de 2ex à 2.5ex suffit à tout bouleverser!
% toutes choses égales par ailleurs.


\newif\ifintocleftbar

\etocsetstyle{chapter}
% don't break immediately after Part
   {\nobreak\etocskipfirstprefix }
% but allow break after section or subsection
   {\pagebreak[2]}
   {%\addvspace{\baselineskip}%
    % it seems that framed's code too easily allows a page break 
    % if some vertical glue is here, so let's use a \kern
    \kern\baselineskip
    \begin{tocleftbar}
      \intocleftbartrue
      \makebox[0pt][r]{\smash{\parbox{6em}%
                       {\null\hfill\Huge\bfseries\color{myred}\etocpage
                        \hspace*{8pt}}}}%
% use of \parbox, else framed.sty has a tendency to allow page breaks if
% multi-line name
      \parbox[t]{\dimexpr\hsize-2\tocmargin\relax}
      {\Large\textsc{\small \etoclink{Chapter~\etocthenumber}}%
%\\* does not do what I want. \par\nobreak does.
% But as we are now in \parbox, does not matter
      \par\etocname\rule[-\baselineskip]{0pt}{0pt}}%
      \par
      \normalsize\leftskip\dimexpr\leftskip+3em\relax
    }
   {}

% THIS IS A VARIANT ABLE TO COPE WITH CHAPTERS WITHOUT SECTIONS
% it is **complicated** because \etocname etc.. loose their meaning if one
% closes a group in the third ("prefix")  or fourth ("contents")
% arguments to \etocsetstyle. Thus, we have to save them before
% issueing \end{tocleftbar} to close the previous left bar environment.
% Although the code mentions \etocthelink, etc, it works also if
% hyperref is not loaded.
% \etocsetstyle{chapter}
%    {\nobreak\etocskipfirstprefix}% don't break immediately after Part
%    {\pagebreak[2]}% but allow break after section or subsection
%    {\global\let\ETOCthepage\etocthepage
%                    \global\let\ETOCthename\etocthename
%                    \global\let\ETOCthenumber\etocthenumber
%                    \global\let\ETOCthelink\etocthelink
%     \ifintocleftbar\end{tocleftbar}\fi%
%     \kern\baselineskip
%     \begin{tocleftbar}
%       \intocleftbartrue
%       \makebox[0pt][r]{\smash{\parbox{6em}%
%                        {\null\hfill\Huge\bfseries\color{myred}%
%                         \ETOCthelink{\ETOCthepage}%
%                         \hspace*{8pt}}}}%
%       \parbox[t]{\dimexpr\hsize-2\tocmargin\relax}
%       {\Large\textsc{\small \ETOCthelink{Chapter~\ETOCthenumber}}%
% %\\* does not do what I want. \par\nobreak does.
% % But as we are now in \parbox, does not matter
%       \par\ETOCthelink{\ETOCthename}\rule[-\baselineskip]{0pt}{0pt}}%
%       \par
%       \normalsize\leftskip\dimexpr\leftskip+3em\relax
%     }
%    {\ifintocleftbar\end{tocleftbar}\fi}


\etocsetstyle {section}
% avoid break after Chapter
  {\nopagebreak[3]\vspace{1pt}\etocskipfirstprefix}
% allow break after previous subsection or section
  {\vspace {1pt plus .2pt minus .2pt}}
  {\makebox[0pt][r]{\makebox[3em][l]{\etocnumber}}%
   \etocname
   \hspace{0.5em}\nobreak{\itshape\color{myred}\etocpage}\par }
  {\end{tocleftbar}}


% IMPORTANT: this is ALSO executed if the last thing
                   % before the next chapter is a subsection

\etocsetstyle {subsection}
% avoid break immediately after section
  {\nopagebreak[2]\vspace{1pt}\etocskipfirstprefix}
  {\vspace {1pt plus .2pt minus .2pt}}%
  % {\etocskipfirstprefix}
  % {\pagebreak[2]\etocskipfirstprefix}% allow break after previous subsection
  {\makebox[0pt][r]{\makebox[3em][l]{\etocnumber}}\etocname
   \hspace{0.5em}\nobreak{\itshape\color{myred}\etocpage}\par }
  {}


\etocsettocstyle{\sffamily\setlength{\tocmargin}{5.4em}%
          {\Huge\bfseries\color{myred}\contentsname\par}%
          \leftskip 0pt\parindent 0pt }{}

  % for memoir class: (does no harm otherwise) [Nota Bene: this should
  % perhaps be done automatically by etoc when it detects the memoir
  % class, for better compatibility]
\etocsetlevel {appendix}{0}

\begin{document}

% Use \tableofcontents* for memoir class, as the TOC itself should not
% appear as a Chapter in the TOC. The * works also with book/report+etoc
% but does nothing. 

% With memoir and without *, the TOC defines a  not-very aesthetic
% chapter entry in itself and one must un-comment the more complicated
% etoc style for chapter above.

% debugging
% \hrule

\tableofcontents*

\part{Designing with \TeX\ and \LaTeX}

\localtableofcontents

\chapter{Beginning to learn design with \TeX}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection with a long title spanning more than one line in the table of contents}
\section{Another test section}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection with a long title spanning more than one line in the table of contents}

% If this is un-commented the more sophisticated chapter style must be used.
%\chapter{A lone chapter without any sections}

\chapter{Beginning to learn design with \LaTeX}
\section{A test section with a long title spanning more than one line in the table of contents}
\subsection{A test subsection}
\subsection{Another test subsection}
\section{A test section}
\ifdefined\phantomsection\phantomsection\fi
\addcontentsline{toc}{section}{Test unnumbered section}
\section*{Test unnumbered section}
\subsection{A test subsection}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}


\newpage\setcounter{page}{123}% just for the example

\part{Designing with HTML}

\localtableofcontents

\chapter{Beginning to learn design with HTML and some other text to span more than one line} 
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section with a long title spanning more than one line in the table of contents}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}



\chapter{Beginning to learn design with HTML and some other text to span more than one line} 
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection with a long title spanning more than one line in the table of contents}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}
\section{Another test section}
\subsection{A test subsection}


\appendix

\chapter{Test Appendix}
\section{Another test section}
\section{Another test section with a long title spanning more than one line in the table of contents}
\section{Another test section}
\subsection{A test subsection}
\section{Another test section}
\section{Another test section}


\end{document}
6
  • Hello. Thanks for this other solution. Is there an advantage of using etoc ?
    – projetmbc
    Commented Mar 5, 2014 at 9:40
  • I don't see any advantage using etoc for this specific design, which was very convingly treated by Gonzalo with titletoc or the memoir class, with or without the leftbar of framed. The etoc approach needs no extra mark-up (\StartMark, \EndMark) It uses the leftbar approach to have the long margin bars alongside sections and subsections. The updated code works also directly with the memoir class. Adding \localtableofcontents after a \part creates a local TOC with the same design for each part. Works with or without hyperref.
    – user4686
    Commented Mar 5, 2014 at 15:21
  • 1
    my usage of framed.sty is not satisfactory: if one takes the code above, uncomment the line with the lonely chapter, and the etoc chapter style necessary for it, with the book class, one gets an awful page break at the top of page 3. Reducing the \tocmargin from 5.4em to 5em fixes it. But this is hardly satisfactory. If some expert knowing framed.sty could explain why this incredible break, I would be curious to learn how to avoid it.
    – user4686
    Commented Mar 5, 2014 at 18:32
  • @jfbu As you seem to be the resident expert for etoc around this site, may I please get your attention for this question: tex.stackexchange.com/questions/306933/… ? Commented May 1, 2016 at 14:39
  • @jfbu the code doesn't re-compile when we have two or more appendices. Commented Aug 2, 2016 at 13:46

You must log in to answer this question.

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