Я знаю, \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) отpart
доsubparagraph
(для стандартных классов 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}