
如何計算總節數之內每章?
我正在嘗試使用該totcount
包,但它返回了該包的總節數最後的章節,而不是章節總數目前的章節。
在下面的 MWE 中,所需的行為是為第一章報告 3 個部分,為第二章和最後一章報告 1 個部分。
totcount
僅報告 1 個部分(最後一章的計數器值)。
\documentclass[12pt]{book}
\usepackage{totcount}
\regtotcounter{section}
\begin{document}
\chapter*{First Chapter}
The total number of sections in this chapter should be 3.
The totcount package reports: \total{section}
\section{First Section}
First text
\section{Second Section}
Second text
\section{Third Section}
Third text
\chapter*{Last Chapter}
The total number of sections in this chapter should be 1.
The totcount package reports: \total{section}
\section{First and only section}
Section text
\end{document}
如何計算每一章內的章節總數?
注意:totcount
如果使用 則報告 0 個部分\section*
,請參閱 Sigur 的評論。
答案1
編輯提前了解每章章節編號的前瞻性版本位於本文的最後。它需要運行兩次編譯。
這個問題導致了一個新的軟體包cntperchap
,自 2015 年 9 月 5 日起,該軟體包在 CTAN 上的版本 0.2 中可用
這使用了這個assoccnt
包(我無意中很了解它的作者;-))
它將一個計數器totalsections
與section
另一個計數器關聯起來-每次section
計數器增加時,totalsections
計數器也會步進。
但是,沒有自動重置\chapter*
使用情況。在這種情況下,可以透過在\chapter
using \xpretocmd
from xpatch
package前面添加一些程式碼來自動完成
筆記作者assoccnt
確實應該將其合併到他的包中;-)
\documentclass[12pt]{book}
\usepackage{assoccnt}
\usepackage{xpatch}
\newcounter{totalsections}[chapter] % Does not work with `chapter*`
% Automatically provide for resetting of the section counter each time
%`\chapter` or `\chapter*` is used -- in this setup this is requested.
\xpretocmd{\chapter}{\setcounter{totalsections}{0}}{}{}
\DeclareAssociatedCounters{section}{totalsections}
\begin{document}
\chapter*{First Chapter}
The total number of sections in this chapter should be 3.
\section{First Section}
First text
\section{Second Section}
Second text
\section{Third Section}
Third text
There \number\value{totalsections} sections in this chapter
\chapter*{Last Chapter}
\section{First and only section}
Section text
There \number\value{totalsections} sections in this chapter
\end{document}
編輯某些新版本使用每章的節數(需要運行兩次才能成功)。
說明: 每次使用新章節時,會將累積的節數寫入外部文件,\jobname.seccnt
簡稱foo.seccnt
。在下一次 Latex 編譯運行時再次讀取該文件,並將值儲存到清單中etoolbox
。巨集\GetTotalSectionCounter
將在該列表中前進,直到到達正確的位置,然後列印出本章中的節數,甚至是前面的節數。 (宏應該是可擴展的,我覺得是這樣的)
foo.seccnt
目前,如果章節數量發生更改,則需要手動刪除該檔案。
我會盡力克服這個缺點。
\documentclass{book}
\usepackage{ifthen}
\usepackage{assoccnt}
\usepackage{xpatch}
\usepackage{pgffor} % Only for quick usage of a lot of sections, demo only
\newwrite\seccountfile%
\newread\seccountinfile%
\listgadd{\seccountlist}{}% Initialize an empty list
\newcounter{currentchapter}
\newcounter{totalsections}
\newcounter{togglecounter}
\DeclareAssociatedCounters{section}{totalsections}
\newcommand{\getsectioncountnumbers}{%
\setcounter{togglecounter}{0}%
\whiledo {\value{togglecounter} < 1}{%
\read\seccountinfile to \gandalf%
\ifeof\seccountinfile%
\stepcounter{togglecounter}%
\else%
\listxadd{\seccountlist}{\gandalf}%
\fi%
}%
}
\xpretocmd{\chapter}{%
\stepcounter{currentchapter}%
\immediate\write\seccountfile{%
\number\value{totalsections}%
}%
\setcounter{totalsections}{0}
}{}{}
\makeatletter
\newcounter{tempcount@a}
\newcommand{\@getsectiontotalcounter}[1]{%
\setcounter{tempcount@a}{0}%
\renewcommand*{\do}[1]{%
\ifnumequal{\value{#1}}{\value{tempcount@a}}{%
##1\listbreak%
}{%
\stepcounter{tempcount@a}%
}%
}%
\dolistloop{\seccountlist}%
}
\newcommand{\GetSectionTotalCounter}[1][currentchapter]{%
\ifdef{\seccountlist}{%
\@getsectiontotalcounter{#1}%
}{}%
}
\makeatother
\AtBeginDocument{%
\IfFileExists{\jobname.seccnt}{%
% Open for reading
\immediate\openin\seccountinfile=\jobname.seccnt%
\getsectioncountnumbers%
}{%
% Open for writing
\immediate\openout\seccountfile=\jobname.seccnt%
}%
}
\AtEndDocument{%
\immediate\write\seccountfile{%
\number\value{totalsections}%
}%
\immediate\closeout\seccountfile%
}
\begin{document}
\chapter*{First}
This chapter will have \GetSectionTotalCounter sections
\section{First}
\section{Second}
\chapter*{Second}
This chapter will have \GetSectionTotalCounter sections
\section{First}
\section{Second}
\section{Third}
\section{Fourth}
% Now a really large chapter
\chapter*{Third}
This chapter will have \GetSectionTotalCounter sections
\foreach \x in {1,...,100} {%
\section{\x}
}
\end{document}
編輯foo.seccnt
這是沒有明確刪除檔案的版本
我使用該addtocontents
方法將LaTeX
節號寫入一個單獨的文件,就像處理toc
相關內容一樣。然後將其foo.seccnt
視為偽造的目錄,在運行中重寫之前讀入(以及臨時儲存的值)。
\documentclass{book}
\usepackage{ifthen}
\usepackage{assoccnt}
\usepackage{xpatch}
\usepackage{pgffor}
\listgadd{\seccountlist}{}% Initialize an empty list
\newcounter{currentchapter}
\newcounter{totalsections}
\newcounter{togglecounter}
\DeclareAssociatedCounters{section}{totalsections}
\makeatletter
\newcommand{\getsectioncountnumbers}{%
\setcounter{togglecounter}{0}%
\whiledo {\value{togglecounter} < 1}{%
\read\tf@seccnt to \seccountnumberfromfile%
\ifeof\tf@seccnt
\stepcounter{togglecounter}%
\else%
\listxadd{\seccountlist}{\seccountnumberfromfile}%
\fi%
}%
}
\xpretocmd{\chapter}{%
\stepcounter{currentchapter}%
\addtocontents{seccnt}{%
\number\value{totalsections}%
}%
\setcounter{totalsections}{0}
}{}{}
\newcounter{tempcount@a}
\newcommand{\@getsectiontotalcounter}[1]{%
\setcounter{tempcount@a}{0}%
\renewcommand*{\do}[1]{%
\ifnumequal{\value{#1}}{\value{tempcount@a}}{%
##1\listbreak%
}{%
\stepcounter{tempcount@a}%
}%
}%
\dolistloop{\seccountlist}%
}
\newcommand{\GetSectionTotalCounter}[1][currentchapter]{%
\ifdef{\seccountlist}{%
\@getsectiontotalcounter{#1}%
}{}%
}
% This is a modified version from \@starttoc, being defined latex.ltx
\def\@startfaketoc#1{%
\begingroup
% Generate the file handle first
\expandafter\newwrite\csname tf@#1\endcsname%
\makeatletter
% Read first before deleting it
\ifcsdef{tf@#1}{%
\IfFileExists{\jobname.#1}{%
\immediate\openin\csname tf@#1\endcsname \jobname.#1\relax
\getsectioncountnumbers%
}{}
}{%
\typeout{No section count numbers so far}
}%
\if@filesw
% Write only if not `\nofiles` is specified
\immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
\fi
\@nobreakfalse
\endgroup%
}
\AtBeginDocument{%
\@startfaketoc{seccnt}
}
\AtEndDocument{%
% Write the last section count to the file
\addtocontents{seccnt}{%
\number\value{totalsections}%
}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter*{First}
This chapter will have \GetSectionTotalCounter sections
\section{First}
\section{Second}
\chapter*{Second}
This chapter will have \GetSectionTotalCounter sections
\section{First}
\section{Second}
\section{Third}
\section{Fourth}
% Now a really large chapter
\chapter*{Third}
This chapter will have \GetSectionTotalCounter sections
\foreach \x in {1,...,100} {%
\section{\x}
}
\chapter{Fourth}
This chapter will have \GetSectionTotalCounter sections
\section{A single section}
\end{document}
編輯OP gsl 指出此程式碼中存在一些錯誤。我可以追蹤到這樣一個事實:在第一次運行中已經\@startfaketoc
嘗試讀取外部檔案。foo.seccnt
這當然會失敗,因為如果刪除該文件或第一次編譯文檔,則不存在這樣的文件。