使用 tikz 和 tocloft 自定义目录

使用 tikz 和 tocloft 自定义目录

我已经使用toclofttikz创建自定义table of contents。请参阅以下我的 MWE:

\documentclass{book}
\usepackage{tocloft,refcount}
\usepackage[colorlinks,linkcolor=black]{hyperref}
\usepackage{tikz,lipsum}
\usetikzlibrary{calc}

\definecolor{rulecolor}{cmyk}{0,1,0.29,0}
\newcommand{\tocchpnumfont}{\fontsize{15}{18}\selectfont\color{chapternumcolor}}
\newcommand{\tocchptextfont}{\fontsize{11}{14}\selectfont\color{white}}
\definecolor{chapternumcolor}{cmyk}{0.40,1,0,0}

\makeatletter
% redefinitions for part entries
\renewcommand\cftpartfont{\Huge\sffamily\bfseries}

\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
       }
      \nobreak
        \global\@nobreaktrue
        \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}

\newcommand\chapternumberline[1]    {\mbox{\Large\@chapapp~#1}\par\noindent}
\cftsetindents{chapter}{0pt}{0pt}
\newcommand\cftchapterpagefont{\tocchpnumfont}

\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}{%
    \tocchptextfont #1%
  }%
  \end{minipage}\par\vspace{2ex}%
}

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

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

\newcommand\cftsectionfont{\sffamily}
\newcommand\cftsectionpagefont{\hfill\color{myred}}
\newcommand{\cftsectionleader}{\nobreak}
\newcommand{\cftsectionafterpnum}{\cftparfillskip}
\cftsetindents{section}{9.5em}{2.5em}
\newcommand\cftsectionformatpnum[1]{%
  \hskip1em\hbox to 4em{{\cftsectionpagefont #1\hfill}}}

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

\newlength\Myhead
\setlength\Myhead{\dimexpr\headheight+\headsep+1in+\voffset+5ex\relax}

\newlength\Myfoot
\setlength\Myfoot{\dimexpr\paperheight-\Myhead-\textheight-\footskip+5ex\relax}

\newcounter{chapmark}

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

\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=1pt] 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=1pt] 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}}\stepcounter{chapmark}%
}
\renewcommand{\cftdot}{}

\begin{document}

\contentsline {chapter}{\chapternumberline {}Negative numbers}{8}    {chapter.1}
\StartMark
\label {st0}\begin {tikzpicture}[overlay,remember picture,baseline] \node     [anchor=base] (s0) {};\end {tikzpicture}\par 
\contentsline {section}{\numberline {8A}Integers}{00}{section.1.1}
\contentsline {section}{\numberline {8B}Adding and subtracting positive     integers }{00}{section.1.2}
\contentsline {section}{\numberline {8C}Adding and subtracting negative     integers}{00}{section1.3}
\contentsline {section}{\numberline {8D}Multiplication and division of     integers}{00}{section.1.3}
\contentsline {section}{\numberline {8E}Order of operation}{00}    {section*.2}
\contentsline {section}{\numberline {8F}Substituting integers}{00}    {section*.3}
\contentsline {section}{\numberline {8G}The number plane}{00}    {section.1.4}
\contentsline {section}{Investigation}{00}{section.1.5}
\contentsline {section}{Review: Chapter summary}{00}{section.1.6}
\contentsline {subsection}{Multiple-choice questions}{00}{subsection.1.6}
\contentsline {subsection}{Short-answer questions}{00}{subsection.1.6}
\contentsline {subsection}{Extended response questions}{00}    {subsection.1.6}
\label {en0}\begin {tikzpicture}[overlay,remember picture,baseline] \node     [anchor=base] (e0) {}; \draw [rulecolor,line width=1pt] let \p 1= (s0), \p     2=(e0) in ($ (7em,\y 1) + (0,1ex) $) -- ($ (7em,\y 2) + (0,2.24ex) $); \end     {tikzpicture} 
\EndMark

\contentsline {section}{1 Counting}{}{}
\contentsline {section}{2 Numeration}{}{}
\contentsline {section}{3 fractions}{}{}
\contentsline {section}{7 Calculation}{}{}
\contentsline {section}{8 Number patterns}{}{}
\contentsline {section}{3 Place value}{}{}
\contentsline {section}{4 Addition and subtraction}{}{}


\end{document}

我希望输出是:

我用过两列不同布局的目录尝试使用paracol包,但无法获得正确的输出。因此我已转到tikz,但仍然没有获得预期的输出。

答案1

任务的形式如下:给定输入(\contentslines 的集合),我们需要创建宏来读取此输入并创建所需的输出。我展示了典型的纯 TeX 解决方案。您可以看到代码紧凑而简单。您只需要了解TeX 原始级别的\hbox/\vbox算法。不需要外部包(仅使用一个用于加载 TeXgyre Heros Narrow 字体的外部文件)。

您可以尝试将此宏插入到您的 LaTeX 代码中,但您需要将第一行更改为用于加载 Helvetica 或 Heros Narrow 字体的 LaTeX 命令。

\input cs-heros \narrow % this is plain TEX specific, replace this if you needed

\def\dimA{80mm}
\def\dimB{7mm}
\def\dimC{12mm}
\def\dimD{10mm}
\def\dimE{.8pt}
\def\dimF{2mm}
\def\dimG{65mm}
\def\dimH{45mm}

\def\Magenta {\pdfliteral{0 .9 .1 .3 K 0 1 .2 .3 k}}
\def\Black   {\pdfliteral{0 g 0 G}}
\def\White   {\pdfliteral{1 g 1 G}}
\def\Blue    {\pdfliteral{1 .3 0 .3 k}}
\def\LightBlue {\pdfliteral{.2 .16 0 .12 k}}

\def\contentsline #1{\csname TOC#1\endcsname}
\def\TOCchapter #1#2#3{%
   \hbox to\dimA{\bf\llap{\Magenta#2\Black\kern\dimF}\rlap{\chaprule\hss}%
                 \kern\dimF\White #1\hss 00\Black\kern\dimB}
   \nointerlineskip\baselineskip=0pt
   \hbox{\kern\dimD \Magenta\vrule height1ex width\dimE}\nointerlineskip
}
\def\TOCsection #1#2#3{\hbox to\dimA{\kern\dimD\secrule #1\hss #2\kern\dimB}}
\def\TOCsubsection #1#2#3{\hbox to\dimA{\kern\dimD\secrule \kern\dimC #1\hss #2\kern\dimB}}

\def\TOCchapterA #1#2#3{\hbox to\dimA{\bf\rlap{\chaprule\hss}
                                       \kern\dimF\Magenta #1\hss}\medskip}
\def\TOCsectionA #1#2#3{\hbox{\kern\dimF\tocstrut#1}\hrule width\dimH}

\def\secrule {\Magenta\vrule height2.1ex depth.6ex width\dimE\Black\kern2mm}
\def\chaprule{\Blue\vrule height2.3ex depth.7ex width\dimA\Black} 
\def\tocstrut{\vrule height2.3ex depth.7ex width0pt}
\def\chapternumberline{}
\def\numberline#1{\hbox to0pt{\hss#1\kern4mm}}
\def\StartContents{\hbox\bgroup \offinterlineskip \vtop\bgroup}
\def\StartMark{}
\def\EndMark{\egroup\kern2mm\vtop\bgroup
   \let\TOCchapter=\TOCchapterA \let\dimA=\dimG  \let\TOCsection=\TOCsectionA 
   \let\Blue=\LightBlue}
\def\EndContents{\Black\egroup\egroup}


\StartContents
\contentsline {chapter}{\chapternumberline {}Negative numbers}{8} {chapter.1}
\StartMark
\contentsline {section}{\numberline {8A}Integers}{00}{section.1.1}
\contentsline {section}{\numberline {8B}Adding and subtracting positive integers }{00}{section.1
\contentsline {section}{\numberline {8C}Adding and subtracting negative integers}{00}{section1.3
\contentsline {section}{\numberline {8D}Multiplication and division of integers}{00}{section.1.3
\contentsline {section}{\numberline {8E}Order of operation}{00} {section*.2}
\contentsline {section}{\numberline {8F}Substituting integers}{00} {section*.3}
\contentsline {section}{\numberline {8G}The number plane}{00} {section.1.4}
\contentsline {section}{Investigation}{00}{section.1.5}
\contentsline {section}{Review: Chapter summary}{00}{section.1.6}
\contentsline {subsection}{Multiple-choice questions}{00}{subsection.1.6}
\contentsline {subsection}{Short-answer questions}{00}{subsection.1.6}
\contentsline {subsection}{Extended response questions}{00} {subsection.1.6}
\EndMark
\contentsline {chapter}{Statistic and Probability}{} {}
\contentsline {section}{1 Counting}{}{}
\contentsline {section}{2 Numeration}{}{}
\contentsline {section}{3 Fractions}{}{}
\contentsline {section}{7 Calculation}{}{}
\contentsline {section}{8 Number patterns}{}{}
\contentsline {section}{3 Place value}{}{}
\contentsline {section}{4 Addition and subtraction}{}{}
\EndContents

\end

结果: 目录

相关内容