第一個版本

第一個版本

我需要一個部分,它不是真正的一部分,也不是一章。因此,我嘗試基於 \part 命令定義一個新命令 \subpart,但具有自己的編號,看起來像這樣,位於新的空白頁中間:

第一子部分

子部分名稱

然而,當我嘗試這樣做時,總是會出現錯誤...

我的方法是基於網路上「我不記得了」的程式碼:

\makeatletter
\def\@subpart[#1]{%
    \ifnum \c@secnumdepth >\m@ne
      \refstepcounter{subpart}%
      \addcontentsline{toc}{subpart}{\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{subpart}{#1}%
    \fi
    {\parindent \z@ %\raggedright                    % Commented \raggedright
     \interlinepenalty \@M
     \normalfont
     \ifnum \c@secnumdepth >\m@ne
       \Large\bfseries\hfil\partname~\thepart\hfil   %  added \hfil … \hfil for centering
       \par\nobreak
     \fi
     \huge \bfseries %
     \markboth{}{}\par}%
    \nobreak
    \vskip 3ex
    \@afterheading}
\makeatother

我真的應該學習正確使用 LaTeX...

謝謝你的幫助,
賽巴斯蒂安

答案1

定義一個全新的部門單位並不是一件簡單的事。使用titlesec包可以減輕工作負擔;由於您沒有提供有關子部分的條目在目錄中應如何顯示的信息,因此我選擇了介於部分和章節之間的樣式;您可以適當地變更此重新定義\l@subpart

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{tocloft}
\usepackage{lipsum}
\usepackage{fmtcount}

\newcommand\subpartname{Subpart}
\titleclass{\subpart}{top}[\part]
\newcounter{subpart}
\renewcommand\thesubpart{\Numberstring{subpart}}

\makeatletter
\titleformat{\subpart}[display]
  {\normalsize\Huge\filcenter}{\scshape\subpartname~\thesubpart}{1em}{{\bfseries#1}\iftitlemeasuring{\def\ttl@endlongest{\clearpage}}{}}
\titlespacing*{\subpart}
  {0pt}{0em}{\pagetotal}
\makeatother

\newcommand\subpartautorefname{\subpartname}
\newcommand\subpartbreak{\cleardoublepage\mbox{}\vfil}

\assignpagestyle{\subpart}{plain}

\makeatletter
\def\toclevel@subpart{0}
\newcommand*\l@subpart[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{1em \@plus\p@}%
    \setlength\@tempdima{3.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      {\leavevmode
       \large\bfseries\subpartname~#1\hfil\hb@xt@\@pnumwidth{\hss #2}}\par
       \nobreak
         \global\@nobreaktrue
         \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}
\makeatother

\begin{document}

\tableofcontents
\part{Test Part One}
\subpart{Test Subpart One}
\chapter{Test Chapter One}
\section{Test Section}
\subpart{Test Subpart Two}
\chapter{Test Chapter Two}
\section{Test Section}

\end{document}

考慮到現在分段單元的級別從\chapter下往下減少了一級:即,\part有級別-1\subpart有級別0\chapter有級別1\section有級別2\subsection有級別3,等等。

答案2

貢薩洛·梅迪納的回答很好。不過,我想補充一下

  1. 展示如何使用該titletoc套件來處理目錄,以及

  2. 顯示如何變更編號方案,以便子部分和章節包含較高層級的計數器。

第一個版本

只需添加titletoc

範例子部分

目錄範例

\documentclass{book}

\usepackage[newparttoc]{titlesec}
\usepackage{titletoc}

\titleformat{\part}[display]{\normalfont\Huge\bfseries}%
{\partname~\thepart}{1pc}{\Huge\bfseries}

\titleclass{\subpart}{page}[\part]

\newcounter{subpart}

\renewcommand{\thesubpart}{\Alph{subpart}}
\newcommand{\subpartname}{Subpart}

\titleformat{\subpart}[display]{\normalfont\Large\bfseries}%
{\subpartname~\thesubpart}{1pc}{\Huge\bfseries}

\titlespacing{\subpart}{0pt}{0pt}{0pt}

\titlecontents{subpart}[0pt]{\addvspace{1pc}\normalfont\bfseries}%
{\thecontentslabel\enspace ---\enspace\large}%
{\normalfont\large\bfseries}{\hspace{2em plus 1fill}\large\contentspage}

\begin{document}
\tableofcontents

\part{Part One}

\subpart{First Subpart}

\chapter{A chapter}

\subpart{Second Subpart}

\chapter{Another chapter}

\section{Testing}

\section{TT}

\part{Part Two}

\subpart{First Subpart}

\chapter{A chapter}

\subpart{Second Subpart}

\chapter{Another chapter}

\end{document}

上面要注意的主要事情是,part為了使目錄功能正常工作,有必要重新定義 。那麼程式碼

  • 直接在下面(以及上面)設定subpart為細分,partchapter
  • 定義對應的計數器及其列印方式 ( \thesubpart)
  • 引入子部分名稱的字串
  • 將子部分標題的列印設定為顯示(居中)區塊
  • 定義目錄的格式

第二版

使用子編號:

新樣本子部分

新的樣本目錄

\documentclass{book}

\usepackage[newparttoc]{titlesec}
\usepackage{titletoc}
\usepackage{amsmath}

\titleformat{\part}[display]{\normalfont\Huge\bfseries}%
{\partname~\thepart}{1pc}{\Huge\bfseries}

\titleclass{\subpart}{page}[\part]

\newcounter{subpart}[part]
\numberwithin{chapter}{subpart}

\renewcommand{\thesubpart}{\thepart-\Alph{subpart}}
\newcommand{\subpartname}{Subpart}

\titleformat{\subpart}[display]{\normalfont\Large\bfseries}%
{\subpartname~\thesubpart}{1pc}{\Huge\bfseries}

\titlespacing{\subpart}{0pt}{0pt}{0pt}

\titlecontents{subpart}[0pt]{\addvspace{1pc}\normalfont\bfseries}%
{\thecontentslabel\enspace ---\enspace\large}%
{\normalfont\large\bfseries}{\hspace{2em plus 1fill}\large\contentspage}

\titlecontents{chapter}[0pt]{\addvspace{0.5pc}\normalfont\bfseries}%
{\thecontentslabel\enspace}{}{\hspace{2em plus 1fill}\contentspage}

\dottedcontents{section}[6em]{}{4em}{1pc}

\begin{document}
\tableofcontents

\part{Part One}

\subpart{First Subpart}

\chapter{A chapter}

\subpart{Second Subpart}

\chapter{Another chapter}

\section{Testing}

\section{TT}

\part{Part Two}

\subpart{First Subpart}

\chapter{A chapter}

\subpart{Second Subpart}

\chapter{Another chapter}

\end{document}

在這種情況下,程式碼是相同的,除了

  • 計數器subpart現在被定義為在每次重置part
  • 計數器chapter在每個子部分重置(\numberwithinAMS 套件已用於此目的)
  • \thesubpart現在也列印零件號
  • 也重新定義了較低層級的目錄格式,以便為標籤留出空間

相關內容