有没有一种简单的方法可以让 Latex 具有:
- 第1章
- 第2章
- 第 3 章
- 第 3b 章
- 第四章
之前提出类似问题的所有问题都很老了,而且似乎有一些复杂/不太可靠的方法来实现这一点。有没有一些简单而优雅的方法?
答案1
这似乎足够强大。如果您需要连续的子章节,请\resetchapters
在您想要步进计数器时添加。
\documentclass[oneside]{book} % oneside is just to have smaller pictures
\usepackage[a6paper,margin=1cm,bottom=1.5cm,heightrounded]{geometry} % just to have smaller pictures
\usepackage{etoolbox} % needed for \preto
\newcounter{keepchapter}
\newif\ifsubchapter
\newcommand{\subchapter}{%
\ifsubchapter
% all is already set up
\else
\subchaptertrue
\setcounter{keepchapter}{\value{chapter}}%
\stepcounter{keepchapter}%
\renewcommand{\thechapter}{\thekeepchapter\alph{chapter}}%
\setcounter{chapter}{0}%
\fi
\standardchapter
}
\let\standardchapter\chapter
\preto\chapter{\resetchapter}
\newcommand{\resetchapter}{%
\ifsubchapter
\setcounter{chapter}{\value{keepchapter}}
\subchapterfalse
\renewcommand{\thechapter}{\arabic{chapter}}%
\fi
}
\begin{document}
\frontmatter
\begingroup\scriptsize % just to have smaller pictures
\tableofcontents
\endgroup % just to have smaller pictures
\chapter{Introduction}
References
\ref{first} \ref{a}
\ref{second-a} \ref{b}
\ref{second-b} \ref{c}
\ref{third-a} \ref{d}
\ref{third-b} \ref{e}
\mainmatter
\chapter{First}\label{first}
\section{Test}\label{a}
\subchapter{Second A}\label{second-a}
\section{Test}\label{b}
\subchapter{Second B}\label{second-b}
\section{Test}\label{c}
\resetchapter
\subchapter{Third A}\label{third-a}
\section{Test}\label{d}
\subchapter{Third B}\label{third-b}
\section{Test}\label{e}
\chapter{Fourth}\label{fourth-b}
\section{Test}\label{f}
\subchapter{Fifth A}\label{fifth-a}
\section{Test}\label{g}
\subchapter{Fifth B}\label{fifth-b}
\section{Test}\label{h}
\end{document}
如果hyperref
涉及,你需要添加几行
\newcounter{keepchapter}
\newif\ifsubchapter
\newcommand{\subchapter}{%
\ifsubchapter
% all is already set up
\else
\subchaptertrue
\setcounter{keepchapter}{\value{chapter}}%
\stepcounter{keepchapter}%
\renewcommand{\thechapter}{\thekeepchapter\alph{chapter}}%
\renewcommand{\theHchapter}{\thekeepchapter\alph{chapter}}%
\setcounter{chapter}{0}%
\fi
\standardchapter
}
\let\standardchapter\chapter
\preto\chapter{\resetchapter}
\newcommand{\resetchapter}{%
\ifsubchapter
\setcounter{chapter}{\value{keepchapter}}
\subchapterfalse
\renewcommand{\thechapter}{\arabic{chapter}}%
\renewcommand{\theHchapter}{\arabic{chapter}}%
\fi
}