在构建了第二个页码计数器并为每个部分重置后,我想知道如何获取某个部分的页数。每个部分都从新的一页开始。
我如何计算每个部分所占的页数并将其放在标题中,例如“第 1 页,共 2 页”?
这是我目前的代码:
\documentclass[a4paper,oneside]{scrartcl}
\usepackage[automark,headsepline,footsepline]{scrpage2}
\usepackage{etoolbox}
\usepackage{lipsum}
\setcounter{secnumdepth}{-1}
% new counter for pages of section
\newcounter{sectionpagecounter}
\newcommand{\resetsectionpagecount}{\setcounter{sectionpagecounter}{1}}
\makeatletter
\patchcmd\@outputpage{\stepcounter{page}}{\stepcounter{page}\stepcounter{sectionpagecounter}}{}{} % inc for every page
\patchcmd\@xsect{\ignorespaces}{\resetsectionpagecount\ignorespaces}{}{} % reset on new section
\makeatother
% header stuff
\ihead{\rightmark}
\chead{}
\ohead{Sectionpage \arabic{sectionpagecounter}}
\ifoot{}
\cfoot{}
\ofoot{\pagemark}
\pagestyle{scrheadings}
\begin{document}
\section{My Section A}
\lipsum
\clearpage
\section{My Section B}
\lipsum
\clearpage
\section{My Section C}
\lipsum
\end{document}
答案1
好问题。下面是第一个可以改进的解决方案(参见代码下方的注释):
\documentclass{scrartcl}
\usepackage[automark,headsepline,footsepline]{scrpage2}
\usepackage{refcount}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{pgffor}
% new counter for pages of section
\newcounter{sectionpagecounter}
\newcommand{\resetsectionpagecount}{\setcounter{sectionpagecounter}{1}}
\makeatletter
\patchcmd\@outputpage{\stepcounter{page}}{\stepcounter{page}\stepcounter{sectionpagecounter}}{}{} % inc for every page
\patchcmd{\@xsect}{\ignorespaces}{\resetsectionpagecount\ignorespaces}{}{} % reset on new section
\pretocmd{\section}{\SectionPage}{}{}
\makeatother
\ihead{\rightmark}
\chead{}
\ohead{Sectionpage \arabic{sectionpagecounter} of \number\value{section\romannumeral\value{section}}}
\ifoot{}
\cfoot{}
\ofoot{\pagemark}
\pagestyle{scrheadings}
\newcounter{tmp}
\stepcounter{tmp}
% We create 100 new counters: sectioni, sectionii,...,sectionic, sectionc
\loop
\ifnum\value{tmp}<101
\newcounter{section\romannumeral\value{tmp}}
\stepcounter{tmp}
\repeat
% command to label each section using its counter
\newcommand\SectionLab{\label{\thesection}}
% the main command: gets the number of pages by subtracting the number of pages
% between two consecutive labels (i.e., between two \section commands)
\newcommand\SectionPage{%
\setcounter{section\romannumeral\number\numexpr\value{section}+1\relax}{%
\number\numexpr\getpagerefnumber{\number\numexpr\thesection+2\relax}
-\getpagerefnumber{\number\numexpr\thesection+1\relax}\relax}%
}
% We need a last label at the end of the document
\newcounter{Lastpg}
\AtBeginDocument{%
\AtEndDocument{%
\setcounter{Lastpg}{\value{page}}
\stepcounter{page}\label{\number\numexpr\thesection+1\relax}}%
\setcounter{page}{\value{Lastpg}}
}%
\begin{document}
\section{Test Section One}
\SectionLab
\lipsum[1-72]
\clearpage
\section{Test Section Two}
\SectionLab
\lipsum[1-2]
\clearpage
\section{Test Section Three}
\SectionLab
\lipsum[1-40]
\clearpage
\section{Test Section Four}
\SectionLab
\lipsum[1-60]
\end{document}
这个想法是\label
在每个\section
命令中放置一些 s,并使用这些标签来计算页数;这个数字存储在一个单独的计数器中(在目前的形式下,代码允许 100 个部分)。还有一些事情可以改进,我会在接下来的几天里尝试这样做:
每次
\section
发出时,必须\SectionLab
在 \section` 命令后立即添加(这可以轻松实现自动化)。问题中发布的代码中用于重置页面计数器的当前方法不是最佳的,因为它在
\@xsect
不够小心的情况下就挂接了,并且较低的分段命令\subsection
(例如)也会重置计数器。
答案2
昨晚我通过将标签写入辅助文件解决了这个问题:
\documentclass[a4paper,oneside]{scrartcl}
\usepackage[automark,headsepline,footsepline]{scrpage2}
\usepackage{etoolbox}
\usepackage{lipsum}
\setcounter{secnumdepth}{-1}
% new counter for sections (why is there no section counter?)
\newcounter{sectioncounter}
% new counter for pages of section
\newcounter{sectionpagecounter}
\newcommand{\resetsectionpagecount}{\setcounter{sectionpagecounter}{1}}
\newcommand{\sectionpagescount}{\pageref{test\arabic{sectioncounter}}}
\makeatletter
\patchcmd\@outputpage{\stepcounter{page}}{\stepcounter{page}\stepcounter{sectionpagecounter}}{}{} % inc for every page
\patchcmd\@xsect{\ignorespaces}{\addtocounter{sectionpagecounter}{-1}\immediate\write\@auxout{\string\newlabel{test\arabic{sectioncounter}}{{}{\arabic{sectionpagecounter}}}}\resetsectionpagecount\stepcounter{sectioncounter}\ignorespaces}{}{} % reset on new section
\makeatother
% header stuff
\ihead{\rightmark}
\chead{}
\ohead{Sectionpage \arabic{sectionpagecounter} of \sectionpagescount}
\ifoot{}
\cfoot{}
\ofoot{\pagemark}
\pagestyle{scrheadings}
\begin{document}
\section{Test Section One}
\lipsum[1-72]
\clearpage
\section{Test Section Two}
\lipsum[1-2]
\clearpage
\section{Test Section Three}
\lipsum[1-40]
\clearpage
\section{Test Section Four}
\lipsum[1-60]
\end{document}
标签名为“testSECTIONNUMBER”。pdflatex 必须至少运行两次。