漂亮的目录

漂亮的目录

我想知道是否可以有如下所示的目录,其中页码是左边的大数字。

PrettytTOC

这些部分可能看起来像以下“丑陋字体格式”的示例。如果有人对章节和子章节有更好的想法......

部分

答案1

下面是使用es 和包中的环境重新定义\chapter命令(如中实现)的解决方案:book.cls\parboxleftbarframed

\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}

在此处输入图片描述

第一个框\parbox包含页码;我只为两位数保留了空间(3.5em);对于三位数,此框的宽度必须增加,而第三个框的宽度\parbox必须减少相同的量。

我对我的第一个解决方案做了一些改进和补充。现在加载时不会出现任何问题hyperref(我以前的解决方案与hyperref);现在在 ToC 元素的修改是在标题目录包;我还为节和小节条目添加了重新定义。

\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}

在此处输入图片描述

作为哈里什·库马尔评论中建议,如果零件也有一些装饰就好了。这个新变体探索了这些装饰的一种可能性:

\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}

在此处输入图片描述

不幸的是,如果hyperref加载,部分条目格式将会丢失(我必须考虑这一点)。

回忆录文献类

下面的一些评论建议使用memoir文档类来实现样式。下面我介绍了两种变体。

第一种变体

此版本适用于编号和未编号的部分、章节、节和小节,并与以下部分配合使用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}

在此处输入图片描述

第二种变体

项目mbc在对原始问题的编辑中建议在包含章节和小节时采用不同的格式;这是首次尝试生成这种新布局。

一些评论:

  1. \StartMark命令必须放在每个 \chapter 命令之前,并且\EndMark必须放在将包含在目录中的章节的最后一个部分单元之后:

  2. 与章节相关的部分单元可能会出现分页符,并且会自动处理。

  3. 该代码至少需要运行三次。

代码:

\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}

获得的 ToC:

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

塔夫特

这是文档类的变体tufte-book

\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}

生成的 ToC 的图像:

在此处输入图片描述

答案2

以下是使用 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

结果如下:

结果

但是,这不是最佳选择。它仅定义章节的列表样式。但由于您没有指定任何其他内容,所以我只能这样做。

该命令\setuplist在第一个参数中定义列表的布局,这里chapter.alternative=none告诉系统您不需要预定义样式,而是使用command定义外观的自定义 。我将页码设置为两位数,否则会造成混淆,哪个数字属于页面,哪个数字属于章节计数。

答案3

我已经对 Marco 提出的解决方案进行了打包 (1),以允许使用带星号的命令版本\chapter,以及 (2) 防止有时(在 Marco 的解决方案中)在页码和章节标题之间出现错误的分页符。为了实现后一个目的,我将包含 \parbox\thecontentspage环境的 包装leftbarminipage环境中。

\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}

答案4

这是一个埃托克实施。这基本上是抄袭Gonzalo Medina 的回答我对其进行了一些改编,并将其翻译成了etoc语言版本。

它与 hyperref 完全兼容,并且需要通常的三次运行才能获得正确的目录(包括页码)。

警告:奇特的格式是仅有的用于目录。正文\part的 、\chapter\section是标准格式。要自定义它们以使其看起来类似于 TOC 对应部分,可以使用titlesec

更新:代码以更好的方式使用\MakeFramed框架包;这似乎可以避免在某些情况下在第一个版本中出现的奇怪的分页符。

此外,代码可以与memoir类一起编译。(*)但是请参见代码注释:特别是如果想要允许没有章节的单独章节(就像在目录中添加一个类似章节的条目用于目录本身的情况memoir一样\tableofcontents ),必须使用(注释掉的)更复杂的 etoc 章节样式,其技术原因将在其中进行解释。

(*) 这是必需的,可以说,只要检测到,\etocsetlevel {appendix}{0}就应该默认执行此操作。etocmemoir

该设计有一个用户可调整的参数\tocmargin,即长度,这里设置为5.4em 在命令调用中具有值(以默认大小的默认 sans 字体计算)\etocsettocstyle

代码通过惩罚措施来避免出现位置错误的分页符。最后一次编辑是我尽最大努力来控制这些分页符的。这framed.sty使得在由其定义的任何环境的开头都有分页符成为可能,因此,很难避免章节与紧接在前的部分标题分离。我在部分和小节之间添加了一些小的垂直粘连,以鼓励在那里进行分页。\tocmargin 在某些情况下,调整值可以解决恼人的分页符问题(我在页面较短的压力情况下对其进行了测试)。

当前代码给出的结果比这些图像上显示的更加分散,页面比这里填充得更好,但断点在相同的位置。

\localtableofcontents我还在这两个部分的源代码中添加了两个。

漂亮的 toc 1 漂亮的 toc 2 漂亮的 toc 3

代码:

\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}

相关内容