重置 newtcbtheorem 环境计数器

重置 newtcbtheorem 环境计数器

这是我当前的代码:

\documentclass{article}

\usepackage{varwidth}
\usepackage{thmtools}
\usepackage[most,many,breakable]{tcolorbox}

\newtcbtheorem[number within=section]{Definition}{Definition}{
  enhanced,
  before skip=2mm,
  after skip=2mm,
  colback=red!5,
  colframe=red!80!black,
  colbacktitle=red!75!black,
  boxrule=0.5mm,
  attach boxed title to top left={
    xshift=1cm,
    yshift*=1mm-\tcboxedtitleheight
  },
  varwidth boxed title*=-3cm,
  boxed title style={
    interior engine=empty,
    frame code={
      \path[fill=tcbcolback]
      ([yshift=-1mm,xshift=-1mm]frame.north west)
      arc[start angle=0,end angle=180,radius=1mm]
      ([yshift=-1mm,xshift=1mm]frame.north east)
      arc[start angle=180,end angle=0,radius=1mm];
      \path[left color=tcbcolback!60!black,right color=tcbcolback!60!black,
      middle color=tcbcolback!80!black]
      ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
      [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
      -- (frame.south east) -- (frame.south west)
      -- ([xshift=-1mm,yshift=-1mm]frame.north west)
      [sharp corners]-- cycle;
    },
  },
  fonttitle=\bfseries,
  title={#2},
  #1
}{def}

\NewDocumentEnvironment{definition}{O{}O{}}
  {\begin{Definition}{#1}{#2}}{\end{Definition}}

\newcounter{lecturecounter}

\makeatletter

\newcommand\resetcounters{
  % Reset the counters for subsection, subsubsection and the definition
  % all the custom environments.
  \setcounter{subsection}{0}
  \setcounter{subsubsection}{0}
  \setcounter{paragraph}{0}
  \setcounter{subparagraph}{0}
}

\usepackage{xifthen}

\def\@lecture{}
\newcommand\lecture[3][\arabic{lecturecounter}]{
  \addtocounter{lecturecounter}{1}
  \setcounter{section}{#1}
  \renewcommand\thesubsection{#1.\arabic{subsection}}
  \resetcounters
  \ifthenelse{\isempty{#3}}{
    \def\@lecture{Lecture \arabic{lecturecounter}}
  }{
    \def\@lecture{Lecture \arabic{lecturecounter}: #3}
  }
  \hfill\footnotesize{#2}
  \hrule
  \vspace*{-0.3cm}
  \section*{\@lecture}
  \addcontentsline{toc}{section}{\@lecture}
}
\makeatother

\begin{document}
  \lecture{Oct 22 2022 Sat (15:56:41)}{Lecture Title}

  \begin{definition}[Definition]
    \label{def:definition}

    hello.
  \end{definition}

  \lecture{Oct 22 2022 Sat (15:56:41)}{Lecture Title 2 now}

  \begin{definition}[Definition]
    \label{def:definition}

    hello.
  \end{definition}
\end{document}

输出如下:

在此处输入图片描述

我如何重置定义环境计数器?

答案1

足以

  • 将声明\newcounter{lecturecounter}移至指令\newtcbtheorem

  • 改变

    \newtcbtheorem[number within=section]{Definition}{Definition}{
    

    \newtcbtheorem[number within=lecturecounter]{Definition}{Definition}{
    
  • 在定义中\lecture,改变

      \addtocounter{lecturecounter}{1}
    

      \stepcounter{lecturecounter}
    

在此处输入图片描述

\documentclass{article}

\usepackage{varwidth}
\usepackage{thmtools}
\usepackage[most,many,breakable]{tcolorbox}
\newcounter{lecturecounter}


\newtcbtheorem[number within=lecturecounter]{Definition}{Definition}{
  enhanced,
  before skip=2mm,
  after skip=2mm,
  colback=red!5,
  colframe=red!80!black,
  colbacktitle=red!75!black,
  boxrule=0.5mm,
  attach boxed title to top left={
    xshift=1cm,
    yshift*=1mm-\tcboxedtitleheight
  },
  varwidth boxed title*=-3cm,
  boxed title style={
    interior engine=empty,
    frame code={
      \path[fill=tcbcolback]
      ([yshift=-1mm,xshift=-1mm]frame.north west)
      arc[start angle=0,end angle=180,radius=1mm]
      ([yshift=-1mm,xshift=1mm]frame.north east)
      arc[start angle=180,end angle=0,radius=1mm];
      \path[left color=tcbcolback!60!black,right color=tcbcolback!60!black,
      middle color=tcbcolback!80!black]
      ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
      [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
      -- (frame.south east) -- (frame.south west)
      -- ([xshift=-1mm,yshift=-1mm]frame.north west)
      [sharp corners]-- cycle;
    },
  },
  fonttitle=\bfseries,
  title={#2},
  #1
}{def}

\NewDocumentEnvironment{definition}{O{}O{}}
  {\begin{Definition}{#1}{#2}}{\end{Definition}}


\makeatletter

\newcommand\resetcounters{
  % Reset the counters for subsection, subsubsection and the definition
  % all the custom environments.
  \setcounter{subsection}{0}
  \setcounter{subsubsection}{0}
  \setcounter{paragraph}{0}
  \setcounter{subparagraph}{0}
}

\usepackage{xifthen}

\def\@lecture{}
\newcommand\lecture[3][\arabic{lecturecounter}]{
  \stepcounter{lecturecounter}
  \setcounter{section}{#1}
  \renewcommand\thesubsection{#1.\arabic{subsection}}
  \resetcounters
  \ifthenelse{\isempty{#3}}{
    \def\@lecture{Lecture \arabic{lecturecounter}}
  }{
    \def\@lecture{Lecture \arabic{lecturecounter}: #3}
  }
  \hfill\footnotesize{#2}
  \hrule
  \vspace*{-0.3cm}
  \section*{\@lecture}
  \addcontentsline{toc}{section}{\@lecture}
}
\makeatother

\begin{document}
  \lecture{Oct 22 2022 Sat (15:56:41)}{Lecture Title}

  \begin{definition}[Definition] \label{def:definition}

    hello.
  \end{definition}

  \lecture{Oct 22 2022 Sat (15:56:41)}{Lecture Title 2 now}

  \begin{definition}[Definition] \label{def:definition2}
    hello again.
  \end{definition}
  
   \begin{definition}[Definition] \label{def:definition3}
    next.
  \end{definition}
\end{document}

相关内容