如何使用自定义章节编号,以使目录(包括间距)正确、标签有效并且子章节采用自定义格式?

如何使用自定义章节编号,以使目录(包括间距)正确、标签有效并且子章节采用自定义格式?

简单来说,我试图复制这个答案,除了命令\sectionnot\subsection

关于自定义部分和目录还有其他答案,例如这个问题但是这种方法存在缺点,例如目录中不能自动留空,标签引用不能正常进行,而且也不能自动使小节编号遵循您的惯例。

以下代码演示了这些缺点:

\documentclass{memoir}
%for this example, need to be able to show subsubsections
\setsecnumdepth{subsubsection}
\maxtocdepth{subsubsection}

\usepackage{hyperref}

%% This is the custom 'section' command from the question I reference
\makeatletter
\newcommand\@csection{\@startsection {section}{1}{\z@}%
  {-3.5ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\normalfont\Large\bfseries}}
\newcommand{\csection}[1]{%
  \@csection*{#1}%
  \addcontentsline{toc}{section}{#1}%
}
\makeatother

%% This is the custom 'SUBsection' command from the answer I reference
\NewCommandCopy{\originalsubsection}{\subsection} %\LetLtxMacro{\originalsubsection}{\subsection}
\RenewDocumentCommand{\subsection}{s d() o m}{%
  \IfBooleanTF{#1}
    {\originalsubsection*{#4}}%
    {%
     \IfValueTF{#2}
       {% letter
        \addtocounter{subsection}{-1}%
        \renewcommand{\thesubsection}{\thesection.#2}%
       }
       {% no letter
        \renewcommand{\thesubsection}{\thesection.\arabic{subsection}}%
       }%
     \originalsubsection[\IfValueTF{#3}{#3}{#4}]{#4}%
    }%
}

\renewcommand{\theHsubsection}{\thesubsection}

\begin{document}

\tableofcontents

\chapter{First Chapter}

\section{First section}

\subsection{normal subsection}

\subsection(P){custom subsection}\label{v}

\subsubsection{First subsubsection}\label{1-v}

\csection{1.A Custom section}
\csection{1.B \hspace{1ex} Custom section}\label{vA}

\subsection{First subsection}\label{1-vA}

\ref{v} and \ref{1-v} vs. \ref{vA} and \ref{1-vA}

\end{document}

目录

相关内容