如何重置节计数器

如何重置节计数器

这是我的文档的基本内容:

\documentclass[12pt]{article}

\begin{document}

\begin{center}
    \textbf{\LARGE{Topic I}}
 \end{center}

\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\begin{center}
    \textbf{\LARGE{Topic II}}
 \end{center}

\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\end{document}

显示类似

这

我希望章节编号从主题 II 重新开始。

答案1

您无需手动提供标题(这样容易出错并且需要大量的手动干预),而是可以重新定义命令\part以提供所需的布局;然后您可以让section计数器在每次part增加时重置:

\documentclass[12pt]{article}

\makeatletter
\@addtoreset{section}{part}
\def\@part[#1]#2{%
    \ifnum \c@secnumdepth >\m@ne
      \refstepcounter{part}%
      \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{part}{#1}%
    \fi
    {\parindent \z@ \raggedright
     \interlinepenalty \@M
     \normalfont\centering
     \ifnum \c@secnumdepth >\m@ne
       \LARGE\bfseries \partname\nobreakspace\thepart
       \par\nobreak
     \fi
     \huge \bfseries #2%
     \markboth{}{}\par}%
    \nobreak
    \vskip 3ex
    \@afterheading}
\renewcommand\partname{Topic}
\makeatother

\begin{document}

\part{}
\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\part{}
\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\end{document}

在此处输入图片描述

附录:

使用该xpatch包您可以简化代码:

\documentclass[12pt]{article}
\usepackage{xpatch}

\makeatletter
\@addtoreset{section}{part}
\xpatchcmd{\@part}{\normalfont}{\normalfont\centering}{}{}
\xpatchcmd{\@part}{\Large}{\LARGE}{}{}
\renewcommand\partname{Topic}
\makeatother

\begin{document}

\part{}
\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\part{}
\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\end{document}

附录2:

另一种变体,使用这次titlesec包轻松定制\part命令,抑制“部件号”标签并使标题居中:

\documentclass[12pt]{article}
\usepackage{titlesec}

\makeatletter
\@addtoreset{section}{part}
\makeatother
\titleformat{\part}[display]
{\normalfont\LARGE\bfseries\centering}{}{0pt}{}

\begin{document}

\part{Polynomials}
\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\part{Functions}
\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\end{document}

在此处输入图片描述

答案2

\documentclass[12pt]{article}

\makeatletter
\@addtoreset{section}{part}
\makeatother

\begin{document}

\part{Topic I}

\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content


\part{Topic II}

\section{Section I}
content

\section{Section II}
content

\subsection{Subsection I} 
content

\subsection{Subsection II}
content

\section{Section III}
content

\end{document}

注意\LARGE不接受参数,并且应始终包含以下段落的结尾,因此语法将是,\begin{center}\bfseries\LARGE Topic I\end{center}但使用明确的字体更改而不是节头不是 LaTeX 的方式:-)\part如果需要,可以单独定制头部的格式以使用居中标题。

相关内容