怎么获得特定计数器在每个章节的总量?

发布于 2022-09-12 10:14:51

个人想做一份笔记, 里面会分多个章节, 每个章节出现一定量习题. 我现在想在每页的页脚处, 记录总共xx题,本章xx题. 记录总共多少题比较简单, 但获得每章的题量我没有想出好的解决办法. 一种可行的办法是每章使用不同的计数器, 章节少, 还行, 多了就不好弄了.
下面是我的代码:

\documentclass{book}
\usepackage{totcount}
\usepackage{fancyhdr}
\usepackage{amsthm}

\fancypagestyle{myfancy}{%
\fancyhf{}% clear all header and footer fields
\fancyfoot[C]{\thepage}
\fancyfoot[RE, LO]{total: \total{totproblem}}
\fancyfoot[LE, RO]{this chapter: \total{pro}}
}
\pagestyle{myfancy}


\makeatletter
\renewcommand\chapter{
    \addtocounter{totproblem}{\arabic{pro}}\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{myfancy}
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}
\makeatother

\newtheorem{pro}{problem}[chapter]

\newtotcounter{totproblem}
\regtotcounter{pro}


\begin{document}
    \chapter{first}
    \begin{pro}
        test
    \end{pro}
    \newpage
    \begin{pro}
        test
    \end{pro}

    \chapter{second}
    \begin{pro}
        test
    \end{pro}
    \newpage
    \begin{pro}
        test
    \end{pro}
    \addtocounter{totproblem}{\arabic{pro}}
\end{document}

查看更多

关注者
0
被浏览
1k
鱼香肉丝没有鱼先生
脾气不好,别来惹我!!!

使用 LaTeX3 做一个简单实现


format.tex

\ExplSyntaxOn
\iow_new:N \l_count_file
\int_zero_new:N \l_topic_section_total_int 
\msg_new:nnn{topic}{undefined}{the~topic~counter~undefined,~please~compile~again!}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[C]{\bfseries\thepage}
\file_if_exist:nTF{\c_sys_jobname_str.cnt}
{
  \file_input:n {\c_sys_jobname_str.cnt}
  \fancyfoot[L]{Total:~\textcolor{red}{\totaltopic}}
  \fancyfoot[R]{Current~section:~\textcolor{red}{\tl_use:c {current\roman{section}}}}
}
{
  \fancyfoot[L]{Total:~\textcolor{red}{??}}
  \fancyfoot[R]{Current~section:~\textcolor{red}{??}}
  \msg_warning:nn{topic}{undefined}
}

\cs_set_eq:NN \oldsection:n \section 

\RenewDocumentCommand\section{som}
{
  \int_compare:nNnF{\arabic{section}} = {0}
  {
    \iow_now:Nx \l_count_file {
      \expandafter\def\exp_not:c{current\roman{section}}{\int_use:c {l_topic_section_\roman{section}_int}}
    }  
  }
  \IfBooleanTF{#1}
  {
    \oldsection:n*{#3}
  }
  {
    \IfNoValueTF{#2}
    {
      \oldsection:n{#3}
    }
    {
      \oldsection:n[#2]{#3}
    }
  }
    \int_zero_new:c {l_topic_section_\roman{section}_int}
}

\cs_new:Npn \topic {
  \group_begin:
    \int_gincr:c {l_topic_section_\roman{section}_int}
    {\bfseries\noindent Topic.\thesection.\int_use:c {l_topic_section_\roman{section}_int}}~
}

\cs_new:Npn \endtopic {
  \par 
  \group_end:
}

\AtBeginDocument{
  \iow_open:Nn \l_count_file {\c_sys_jobname_str.cnt}
}
\AtEndDocument{
  \iow_now:Nx \l_count_file {
    \expandafter\def\exp_not:c{current\roman{section}}{\int_use:c {l_topic_section_\roman{section}_int}}
  }    
  \int_step_inline:nn{\arabic{section}}
  {
   \int_add:Nn \l_topic_section_total_int {\int_use:c {l_topic_section_\int_to_roman:n{#1}_int}}
  }
  \iow_now:Nx \l_count_file {
    \def\noexpand\totaltopic{\int_use:N \l_topic_section_total_int}
  }
  
  \iow_close:N \l_count_file
}
\ExplSyntaxOff

main.tex

\documentclass{article}
\usepackage{fancyhdr,xcolor}
\usepackage[margin = 3cm]{geometry}

\input format
\title{title}
\author{author}
\begin{document}
\maketitle
\tableofcontents
\section{aaa}
\begin{topic}
aaa
\end{topic}
\begin{topic}
  aaa
\end{topic}
\begin{topic}
aaa
\end{topic}
\begin{topic}
aaa
\end{topic}

\newpage

\section{bbb}
\begin{topic}
aaa
\end{topic}
\begin{topic}
aaa
\end{topic}

\newpage

\section{ccc}
\begin{topic}
aaa
\end{topic}

\end{document}

结果

1.png
2.png
3.png

2 个回答
雾月
雾月 2022-09-12
这家伙很懒,什么也没写!

在每个章节的开始处将计数器的值写入 aux 文件中,

\usepackage{atveryend}
\makeatletter
\newcounter{problem} % 问题总个数
\protected\def\write@problem@counter #1{% 计数器写入辅助文件, #1 为第几个
  \immediate\write\@auxout{\string\global\string\@namedef
    {label/problem/atchap\number #1}{\number\value{problem}}}}
\AtBeginDocument{%
  \providecommand\totalproblemcount{0}% 总个数:\totalproblemcount
  \write@problem@counter{0}} % 第 0 个
\AfterLastShipout{\immediate\write\@auxout{%
    \string\gdef\totalproblemcount{\value{problem}}}% 总个数:\totalproblemcount
  \stepcounter{problem}%
  \write@problem@counter{\numexpr\value{chapter}+1}} % 第 n+1 个
% 得到保存的第 #1 个 problem 的值
\def\getproblemcount #1{\ifcsname label/problem/atchap\number\numexpr #1\relax\endcsname
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {\@nameuse{label/problem/atchap\number\numexpr #1\relax}}{-1}}
% #1+1 个与 #1 个的差 
\def\problemcounterdiff #1{%
  \ifnum\getproblemcount{#1+1}>\z@
    \the\numexpr\getproblemcount{#1+1}-\getproblemcount{#1}\relax
  \else 0???\fi}
\makeatother

然后在 \chapter 开始(或结尾)时,将第 \value{chapter} 个写入 aux 文件:

\renewcommand\chapter{
  ...
  \refstepcounter{chapter}
  ...
  \write@problem@counter{\value{chapter}}
  ...
}

还需在每个问题开始时都递增 problem 计数器。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览