个人想做一份笔记, 里面会分多个章节, 每个章节出现一定量习题. 我现在想在每页的页脚处, 记录总共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}
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}
在每个章节的开始处将计数器的值写入 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
计数器。
谢谢. 请问
\topic
可以改成\begin{topic} \end{topic}
这种形式吗?我试了下
\NewDocumentEnvironment
, 但不知道其他怎么弄了已经更新答案
@u19850 您好. 十分感谢. 请问可以提供个收款方式吗? 我点充值, 但是充不了
@u68992 最后还有一个问题, 您的解决方案似乎和
\maketitle
和\tableofcontents
有冲突. 当添加\tableofcontents
后, 运行第二次就会报错.是我重定义
\section
导致的,我更新了答案,但是使用\section*
还是会有一些问题,等后面再修复一下@u68992 不用,论坛免费交流