定义与计数器相关的自己的类选项

定义与计数器相关的自己的类选项

class.cls我创建了一个名为如下的类文件:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{class}
\LoadClass{report}


\RequirePackage{indentfirst}

\DeclareOption{withinchap}{%
    \newcounter{exercounter}[chapter]%
    \setcounter{exercounter}{0}}

\DeclareOption{withinsec}{%
    \newcounter{exercounter}[section]%
    \setcounter{exercounter}{0}}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{report}}
\ExecuteOptions{withinchap}
\ProcessOptions\relax


\newenvironment{exercise}%
    {\stepcounter{exercounter}\par\textbf{\theexercounter.}}%
    {\ignorespacesafterend}

我想要实现的是设置一对类选项withinchap(default) 和withinsec。如果使用withinchap(default) 选项,则exercounter在开始新章节时将练习计数器重置为零。如果使用withinsec选项,则在开始新部分时将练习计数器exercounter重置为零。但是当我withinsec在文件中使用选项时test.tex,如下所示:

\documentclass[withinsec]{class}
\usepackage{showframe}
\begin{document}

\chapter{AAAAA}
\section{aaaaa}

\begin{exercise}
This is an exercise.
\end{exercise}

\begin{exercise}
    This is another exercise.
\end{exercise}

\begin{exercise}
    This is the third exercise.
\end{exercise}


\section{bbbbb}

\begin{exercise}
    This is the fourth exercise in chapter one.
\end{exercise}

\begin{exercise}
    This is the fifth exerecise in chapter one.
\end{exercise}


\end{document}

在 Windows 终端中运行后pdflatex test,我收到一条错误消息

! LaTeX Error: Command \c@exercounter already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.18 \ProcessOptions\relax

?
! Emergency stop.
 ...

l.18 \ProcessOptions\relax

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on test.log.

Type  H <return>  for immediate help.
 ...

l.18 \ProcessOptions\relax

?

我想知道错误的原因。

答案1

重复\newcounter{exercounter}多次。您可以改用\counterwithin

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{class}
\LoadClass{report}

\RequirePackage{indentfirst}

\newcounter{exercounter}

\DeclareOption{withinchap}{\counterwithin{exercounter}{chapter}}
\DeclareOption{withinsec}{\counterwithin{exercounter}{section}}
%\DeclareOption*{\PassOptionsToClass{\CurrentOption}{report}}

\ExecuteOptions{withinchap}

\ProcessOptions\relax

\newenvironment{exercise}%
    {\stepcounter{exercounter}\par\textbf{\theexercounter.}}%
    {\ignorespacesafterend}

withinchap使用选项输出

在此处输入图片描述

withinsec使用选项输出

在此处输入图片描述

请注意,类代码中的注释行没有任何作用,因为该类report已经被加载。

为了向其传递选项,您需要延迟代码。

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{class}

\newcounter{exercounter}

\DeclareOption{withinchap}{\def\exercounter@within{chapter}}
\DeclareOption{withinsec}{\def\exercounter@within{section}}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{report}}

\ExecuteOptions{withinchap}
\ProcessOptions\relax

\LoadClass{report} % should go after processing options

\RequirePackage{indentfirst}

\newcounter{exercounter}
\counterwithin{exercounter}{\exercounter@within}

\newenvironment{exercise}%
    {\stepcounter{exercounter}\par\textbf{\theexercounter.}}%
    {\ignorespacesafterend}

如果您只想要锻炼号码,请使用\counterwithin*

相关内容