在环境中定义的特殊章节的独立章节计数器

在环境中定义的特殊章节的独立章节计数器

梅威瑟:

考虑以下 MWE,它定义了一个新环境specialChapterEnvironment,我想使用它来定义“特殊”章节标题的属性。例如,这只是将数字从阿拉伯字母更改为罗马字母。

\documentclass{book}


% Defines a new environment for special chapters
\newenvironment{specialChapterEnvironment}{
  \renewcommand{\thechapter}{\Roman{chapter}}
  }{}
  
\begin{document}
\tableofcontents

\setcounter{chapter}{0}
\begin{specialChapterEnvironment}
 \chapter{A special chapter}
\end{specialChapterEnvironment}

\chapter{A normal chapter}
\chapter{Another normal chapter}

\begin{specialChapterEnvironment}
 \chapter{Another special chapter}
\end{specialChapterEnvironment}


\end{document}

MWE 的输出

代码输出如下:

在此处输入图片描述

期望的输出/解决方案

正如人们所料,无论章节类型如何(即普通或特殊),章节计数器都会增加。但是,我希望有两个独立的章节计数器用于“普通”和“特殊”章节,即特殊章节应枚举为 I、II、III... 而普通章节应枚举为 1、2、3...

做到这一点最简单的方法是什么?

答案1

您可以在本地将\c@chapter(计数器的内部名称chapter)重新定义为\c@specialchapter,其中specialchapter是特殊章节的计数器。

在下面的代码中oneside,和geometry仅用于制作较小的图片。我添加了它hyperref以查看设置是否与之兼容。如果您不打算使用它,请删除有关 的行\theHchapter

\documentclass[oneside]{book}

\usepackage[a6paper]{geometry}

\usepackage{hyperref}

\makeatletter
\newcounter{specialchapter}
% Defines a new environment for special chapters
\newenvironment{specialChapterEnvironment}
 {%
  \let\c@chapter\c@specialchapter
  \renewcommand{\thechapter}{\Roman{chapter}}%
  \renewcommand{\theHchapter}{\Roman{chapter}}%
  % add here other settings
 }
 {}
\makeatother

\begin{document}

\tableofcontents

\begin{specialChapterEnvironment}
 \chapter{A special chapter}\label{one}

\ref{one} \ref{two} \ref{three} \ref{four}
\end{specialChapterEnvironment}

\chapter{A normal chapter}\label{two}
\chapter{Another normal chapter}\label{three}

\begin{specialChapterEnvironment}
 \chapter{Another special chapter}\label{four}
\end{specialChapterEnvironment}

\end{document}

在此处输入图片描述

答案2

\documentclass{book}

\makeatletter
\newcounter{specialchapter}
\renewcommand\thespecialchapter{\Roman{specialchapter}}
\newcommand\specialchapter{\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{plain}%
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@specialchapter\@schapter}
\def\@specialchapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{specialchapter}%
                         \typeout{\@chapapp\space\thespecialchapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thespecialchapter}#1}%
                       \else
                         \addcontentsline{toc}{chapter}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{chapter}{#1}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makespecialchapterhead{#2}]%
                    \else
                      \@makespecialchapterhead{#2}%
                      \@afterheading
                    \fi}
\def\@makespecialchapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \huge\bfseries \@chapapp\space \thespecialchapter
        \par\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}                    
\makeatother

%for section etc thechapter must be changed
\AddToHook{cmd/specialchapter/before}{\let\thechapter\thespecialchapter}
\AddToHook{cmd/chapter/before}{\renewcommand\thechapter{\arabic{chapter}}}

\begin{document}
\tableofcontents

\specialchapter{A special chapter}
\section{special}
\chapter{A normal chapter}
\section{normal}
\chapter{Another normal chapter}
\section{normal}
\specialchapter{Another special chapter}

\end{document}

在此处输入图片描述

相关内容