Here's a solution with a redefinition of the \chapter
command (as implemented in book.cls
) using \parbox
es 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](https://i.stack.imgur.com/L7cvx.png)
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](https://i.stack.imgur.com/Pda1B.png)
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](https://i.stack.imgur.com/r3GvE.png)
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](https://i.stack.imgur.com/hlg08.png)
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:
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:
Page breaks can occur for the sectional units associated to a chapter and will be dealt automatically.
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](https://i.stack.imgur.com/v1FKx.png)
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](https://i.stack.imgur.com/h8yyT.png)
Best Answer
To flesh-out an
siunitx
answer, you might do something likeThis needs
siunitx
v2.2, as theoutput-exponent-marker
option is new. (I notice that this points up an issue with negative exponents when not using\times 10^{<exponent>}
: I'll take a look at that over the next few days and see what I can do.)