向目录添加初步元素

向目录添加初步元素

假设到目前为止我已经完成了书的两个部分,但我想将包含所有部分和章节(但没有页码)的初步目录显示为预览,如下所示:

I  First Part              1

1  First Chapter           3
   1.1  First Section .... 3
   1.2  Second Section ... 5
   1.3  Third Section ....
2  Second Chapter
   2.1  First Section ....
   2.2  Second Section ...
   2.3  Third Section ....

II Second Part

3  Third Chapter
   1.1  First Section ....
   1.2  Second Section ...
   1.3  Third Section ....
4  Fourth Chapter
   2.1  First Section ....
   2.2  Second Section ...
   2.3  Third Section ....

我怎样才能包含这些伪部分/章节/节等,而又不使文档中出现空的部分/章节/节。

答案1

笔记请参阅GhostChapters底部的版本

\addtocontents可以通过伪造和省略原始命令的第二个参数来添加初步条目\contentsline{},从而删除页码。

由于缺少 MWE,这仅 适用于bookscrbook和类。memoir

\documentclass{book}

\usepackage{etoolbox}%

\newcommand{\addfakecontent}[2]{%
\refstepcounter{#1}%
\addtocontents{toc}{\protect\contentsline{#1}{\csuse{the#1}\hspace{1em}#2}{}}
}%


\AtEndDocument{%

\addfakecontent{part}{The next to final part}
\addfakecontent{chapter}{Some chapter of another part}%
\addfakecontent{chapter}{Even another chapter of another part}%


\addfakecontent{part}{The final part}
\addfakecontent{chapter}{Some chapter of the final part}
\addfakecontent{section}{A section of a final part}



}

\usepackage{blindtext}

\begin{document}

\tableofcontents

\part{First}

\chapter{Some chapter content}

\blindtext



\end{document}

在此处输入图片描述

Ghostchapter - 版本

此版本使用章节的概念ghost(即 \parts、\chapters 等)。通过\EnableGhostChapters在文档中的“任何”任意位置使用,以下结构命令将被禁用,但会向 中添加一个条目ToC。它适用于scrbookmemoir下至subparagraph和 上subsubsectionbook类——我不明白为什么它对paragraphsubparagraph书籍 类失败了……(奇怪的定义?)

darkgray幽灵章节在目录中以彩色显示。

用于\DisableGhostChapters切换回正常行为。

\EnablePagesInToc\DisablePagesInToc启用/禁用目录中页码的显示

注意:虽然我用过\patchcmd...但它不是一个补丁,而是一个黑客;-)

它不能正常工作,\include{}\input{}

\documentclass{book}


\usepackage{xcolor}%
\usepackage{etoolbox}%


\newcommand{\preliminarycontentscolor}{darkgray}

\newbool{UsePagesInToc}  


\newbool{GhostChapters}



\newcommand{\EnablePagesInToc}{%
\clearpage
\setbool{UsePagesInToc}{true}%
}%

\newcommand{\DisablePagesInToc}{%
\setbool{UsePagesInToc}{false}%
}%


\newcommand{\EnableGhostChapters}{%
\DisablePagesInToc%
\setbool{GhostChapters}{true}%
}%


\newcommand{\DisableGhostChapters}{%
\EnablePagesInToc%
\setbool{GhostChapters}{false}%
}%




\setcounter{tocdepth}{5}

\setcounter{secnumdepth}{5}


\makeatletter





\def\addcontentsline#1#2#3{%
  \ifbool{UsePagesInToc}{%
    \addtocontents{#1}{\protect\contentsline{#2}{#3}{\thepage}}%
  }{%
    \addtocontents{#1}{\begingroup\color{\preliminarycontentscolor}{\protect\contentsline{#2}{#3}{}}\endgroup}
  }%
}%


\makeatletter

\patchcmd{\@part}{%
  \markboth{}{}%
  {\centering
    \interlinepenalty \@M
    \normalfont
    \ifnum \c@secnumdepth >-2\relax
       \huge\bfseries \partname\nobreakspace\thepart%
       \par
       \vskip 20\p@
     \fi
     \Huge \bfseries #2\par}%
   \@endpart%
 }{%
  \ifbool{GhostChapters}{\thispagestyle{empty}}{%
  \markboth{}{}%
  {\centering
    \interlinepenalty \@M
    \normalfont
    \ifnum \c@secnumdepth >-2\relax
       \huge\bfseries \partname\nobreakspace\thepart%
       \par
       \vskip 20\p@
     \fi
     \Huge \bfseries #2\par}%
   \@endpart%
  }%
}{\typeout{part structure successfully patched}}{\typeout{part structure patching failed}}


\patchcmd{\@chapter}{\chaptermark{#1}%
  \addtocontents{lof}{\protect\addvspace{10\p@}}%
  \addtocontents{lot}{\protect\addvspace{10\p@}}%
  \if@twocolumn
  \@topnewpage[\@makechapterhead{#2}]%
  \else
  \@makechapterhead{#2}%
  \@afterheading
  \fi}{% Patch
  \ifbool{GhostChapters}{\thispagestyle{empty}}{%
    \chaptermark{#1}%
    \addtocontents{lof}{\protect\addvspace{10\p@}}%
    \addtocontents{lot}{\protect\addvspace{10\p@}}%
    \if@twocolumn
    \@topnewpage[\@makechapterhead{#2}]%
    \else
    \@makechapterhead{#2}%
    \@afterheading
    \fi}
}{\typeout{chapter structure successfully patched}}{\typeout{chapter structure patching failed}}


\patchcmd{\@sect}{%  
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}{%
  \ifbool{GhostChapters}{%
    \ifdim \@tempskipa>\z@
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
      \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}
  \else
    \def\@svsechd{%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
          \fi
          #7}}%
    \fi%
    \@xsect{#5}%
  }{%
    \ifdim \@tempskipa>\z@
    \begingroup
    #6{%
      \@hangfrom{\hskip #3\relax\@svsec}%
      \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
      \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
    \else
    \def\@svsechd{%
      #6{\hskip #3\relax
        \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
    \fi
  \@xsect{#5}
}%
}{\typeout{section structures successfully patched}}{\typeout{section structures patching failed}}


\AtBeginDocument{%
  \DisableGhostChapters%
  \EnablePagesInToc%
}%

\AtEndDocument{%
\phantom{some output}
}%

\makeatother
\usepackage{blindtext}

\begin{document}
\DisableGhostChapters
\tableofcontents



\part{First}

\chapter{First of first part}

\section{First section of first chapter}

\subsection{First subsection}

\subsubsection{First subsubsection}
\paragraph{First paragraph}

\subparagraph{First subparagraph}

\blindtext

\chapter{Second chapter}



\EnableGhostChapters

\part{OtherOne}



\chapter{New Chapter}

\section{Section of another chapter}%


\chapter{Another chapter}

\chapter{Yet another chapter of another part}

\section{First section of another chapter}

\subsection{First subsection}

\subsubsection{First subsubsection}
\paragraph{First paragraph}

\subparagraph{First subparagraph}


\EnablePagesInToc
\DisableGhostChapters

\chapter{You will see me!}

\part{And some visible part}

\EnableGhostChapters%
\DisablePagesInToc%
\chapter{With an invisible chapter}


\end{document}

在此处输入图片描述

相关内容