使用 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 程式碼中,但您需要將第一行變更為 LaTeX 命令以載入 Helvetica 或 Heros Narrow 字體。

\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

結果: 目錄

相關內容