如何隱藏目錄中的章節編號?

如何隱藏目錄中的章節編號?

我知道\setcounter{secnumdepth}{1}可以讓您設定編號的深度,但我只能對節和小節進行編號,而不是章節。有沒有單行程式碼可以做到這一點?我已經看到了類似問題的一些答案,但對於這樣一個簡單的任務來說,它們似乎太複雜。

答案1

etoc我們的用戶 jfbu 提供的非常精美且複雜的軟體包為此提供了方法。

透過使用\etocsetlevel{level name}{level value}可以改變結構層級(例如章節)到某個較低的水平(例如,超出分段)然後將tocdepth計數器限制為上面的某個值。

\etocsetlevel{chapter}{6}並將\setcounter{tocdepth}{4}完成這項工作。

這僅影響目錄中的表示,而不影響文件的主要部分。

可能需要調整目錄中的間距,這可以透過套件\cft....中的各種命令來實現tocloft(此處未使用)

secnumdepth請注意和計數器之間的區別tocdepth

  • tocdepth決定哪些等級顯示在目錄中(-1 到 6)從partsubparagraph(對於標準 LaTeX 類別)
  • secnumdepth決定哪些等級在主文檔中獲得節號。

\documentclass{book}
\usepackage{etoc}       
\setcounter{secnumdepth}{4}% Show down to subsubsection
\begin{document}    

\setcounter{tocdepth}{4} %for main TOC, only show chapter/section
\etocsetlevel{part}{6} % push away the chapters
\etocsetlevel{chapter}{6}  % push away the chapters, beyond toc depth (4 )
\tableofcontents
\chapter{this is chapter heading}    
  \section{this is section heading}
  \subsection{this is subsection heading}
  \subsubsection{this is subsubsection heading}
  \subsubsection{this is another subsubsection heading}
  \chapter{another chapter}
  \section{this is yet another section} 
\end{document}

在此輸入影像描述

編輯

如果只需要刪除章節編號(但是,不刪除第 1.1 節等),一個技巧是修補指令\@chapter

\documentclass{book}
\usepackage{tocloft}
\setcounter{secnumdepth}{4}% Show down to subsubsection

\setlength{\cftchapindent}{-20pt}% Just some value...

\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@chapter}{\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}{%
                      \addcontentsline{toc}{chapter}{\protect\numberline{}#1}}{\typeout{Success}}{\typeout{Failed!}}
\makeatother

\begin{document}    

\tableofcontents

%\renewcommand{\thechapter}{\arabic{chapter}}
\chapter{First chapter}
  \section{First section}
  \subsection{First subsection}
  \subsubsection{Even more in the basement}
  \chapter{Another chapter}
  \section{this is yet another section} 
\end{document} 

在此輸入影像描述

答案2

也許你可以使用 KOMA-Script 類別:

\documentclass[emulatestandardclasses]{scrbook}
\renewcommand\addchaptertocentry[2]{\addtocentrydefault{chapter}{}{#2}}

\usepackage{blindtext}% dummy text
\begin{document}
\tableofcontents
\Blinddocument
\Blinddocument
\end{document}

在此輸入影像描述

答案3

解決方案titletoc

    \documentclass[11pt, a4paper]{book}

    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage{fourier}
    \usepackage{microtype}

    \usepackage{titletoc}%

      \titlecontents{chapter}[0em]{\lsstyle\smallskip\bfseries}%\vspace{1cm}%
      {}%
      {\itshape\bfseries}%numberless%
      {\hfill\contentspage}[\medskip]%
    %
     \titlecontents{section}[4.25em]{\smallskip}%
      {\contentslabel[\thecontentslabel]{2em}}%numbered
      {\hspace*{-1em}}%numberless
      {\hfill\contentspage}[\smallskip]%
    %
     \titlecontents{subsection}[7em]{}%
      {\contentslabel[\thecontentslabel]{2.75em}}%numbered
      {\hspace*{-1em}}%numberless
      {\hfill\contentspage}[\smallskip]

    \begin{document}
    \tableofcontents

    \chapter*{INTRODUCTION}
    \addcontentsline{toc}{chapter}{INTRODUCTION}

    \chapter{A NICE FIRST CHAPTER}

    \section{An Introductory Section}
    \newpage
    \section{Another Section}
    \subsection{A Boring Subsection }

    \end{document}

在此輸入影像描述

相關內容