`\counterwithin` 不适用于自定义计数器

`\counterwithin` 不适用于自定义计数器

我想section对每个新讲座进行复述。我知道\counterwithin可以完成这项工作。它编译成功,但现在不起作用。

以下是相关代码。在此处输入图片描述

\newcounter{lecnum}
\renewcommand{\thepage}{\thelecnum-\arabic{page}}
\renewcommand{\thesection}{\thelecnum.\arabic{section}}
\renewcommand{\theequation}{\thelecnum.\arabic{equation}}
\renewcommand{\thefigure}{\thelecnum.\arabic{figure}}
\renewcommand{\thetable}{\thelecnum.\arabic{table}}

\newcommand{\lecture}[4]{
    \pagestyle{myheadings}
    \thispagestyle{plain}
    \newpage
    \setcounter{lecnum}{#1}
    \setcounter{page}{1}
    \noindent
    \begin{center}
        \framebox{
            \vbox{\vspace{2mm}
                \hbox to 6.28in { {\bf ECCV2020
                        \hfill August 2020} }
                \vspace{4mm}
                \hbox to 6.28in { {\Large \hfill Lecture #1: #2  \hfill} }
                \vspace{2mm}
                \hbox to 6.28in { {\it Lecturer: #3 \hfill Scribes: #4} }
                \vspace{2mm}}
        }
    \end{center}
    \markboth{Lecture #1: #2}{Lecture #1: #2}
}

\counterwithin*{section}{lecnum}

谢谢

答案1

您需要\stepcounter重置相关计数器。

\documentclass{article}
\usepackage[a4paper,textwidth=7in]{geometry}

\geometry{textheight=6cm}% just to make a smaller picture

\newcounter{lecnum}
\renewcommand{\thepage}{\thelecnum-\arabic{page}}
\counterwithin{section}{lecnum}
\counterwithin{equation}{lecnum}
\counterwithin{figure}{lecnum}
\counterwithin{table}{lecnum}

\pagestyle{myheadings}

\newcommand{\lecture}[4]{%
  \clearpage
  \thispagestyle{plain}
  \stepcounter{lecnum}% resets every associated counter
  \setcounter{lecnum}{#1}%
  \setcounter{page}{1}
  \begin{center}
    \framebox{%
      \parbox[t]{6.28in}{%
        \vspace{2mm}
        {\bfseries ECCV2020\hfill August 2020\par}
        \vspace{2mm}
        {\Large\hspace*{\fill}Lecture #1: #2\hspace*{\fill}\par}
        \vspace{4mm}
        {\itshape Lecturer: #3\hfill Scribes: #4\par}
        \vspace{2mm}
      }%
    }
  \end{center}
  \markboth{Lecture #1: #2}{Lecture #1: #2}
}

\begin{document}

\lecture{1}{Boxes}{egreg}{Rabbittac}

This is the first lecture.

\section{First}

First section with an equation
\begin{equation}
a=b
\end{equation}

\lecture{3}{Counters}{egreg}{Rabbittac}

This is the third lecture.

\section{First}

First section with an equation
\begin{equation}
a=b
\end{equation}

\end{document}

我重构了该框的代码,使其使用 LaTeX 命令,而不是普通命令。命令\bf\it类似命令已被弃用约 25 年。

在此处输入图片描述

相关内容