TransWikia.com

`counterwithin` not work with custom counters

TeX - LaTeX Asked by Rabbittac on November 9, 2020

I want to recount section for each new lecture. I learn that counterwithin can do this job. It compiles successfully but not works right now.

Here is the relevant code.enter image description here

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}

Thanks

One Answer

You need stepcounter in order to reset the associated counters.

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 ECCV2020hfill August 2020par}
        vspace{2mm}
        {Largehspace*{fill}Lecture #1: #2hspace*{fill}par}
        vspace{4mm}
        {itshape Lecturer: #3hfill Scribes: #4par}
        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}

I refactored the code for the box to use LaTeX commands, rather than plain ones. The commands bf, it and similar have been deprecated for about 25 years.

enter image description here

Answered by egreg on November 9, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP