我有一篇文章,分为章节和子章节。我需要在“5.2”和“5.3”之间插入子章节“5.2a”。最简单的方法是什么?
答案1
如果只是一次,最简单的方法是重新定义子部分计数器的表示,然后恢复到原始定义:
\documentclass{article}
\begin{document}
\section{Test Section}
\subsection{Test Subsection One}
\addtocounter{subsection}{-1}
\renewcommand\thesubsection{\thesection.\arabic{subsection}a}
\subsection{Test Subsection One A}\label{sec:test}
\renewcommand\thesubsection{\thesection.\arabic{subsection}}
\subsection{Test Subsection Two}
\end{document}
答案2
更通用的解决方案是进行重新定义,而不对\thesubsection
扩展内容做任何假设。
\documentclass{article}
%\usepackage{hyperref} % uncomment for testing with it
\makeatletter
\newcommand{\specialsubsection}[1]{%
\def\@specialsubsectionsuffix{#1}%
\@dblarg\@specialsubsection
}
\def\@specialsubsection[#1]#2{%
\begingroup
\expandafter\def\expandafter\thesubsection\expandafter{%
\thesubsection\@specialsubsectionsuffix}%
\@ifundefined{theHsubsection}%
{}%
{\expandafter\def\expandafter\theHsubsection\expandafter{%
\theHsubsection\@specialsubsectionsuffix}}%
\addtocounter{subsection}{-1}%
\subsection[#1]{#2}%
\endgroup
}
\makeatother
\begin{document}
\section{Test Section}
\subsection{Test Subsection One}
\specialsubsection{a}{Test Subsection One A}\label{sec:test}
\specialsubsection{b}[For toc]{Test Subsection One B}
\subsection{Test Subsection Two}
\end{document}
这也允许使用不同的 TOC 条目,并带有通常的可选参数(在后缀后)。
该\theHsubsection
位是为了使代码能够与之一起工作hyperref
。