
Как можно подсчитать общее количество разделов?в пределахкаждая глава?
Я пытаюсь использовать 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
, который доступен в версии 0.2 на CTAN с 05.09.2015.
Здесь используется assoccnt
пакет (автора которого я случайно хорошо знаю ;-))
Он связывает счетчик totalsections
со section
счетчиком — каждый раз, когда section
счетчик увеличивается, totalsections
счетчик также увеличивается.
Однако автоматического сброса для использования не предусмотрено \chapter*
. В этом случае это можно сделать автоматически, добавив код к \chapter
using \xpretocmd
из xpatch
пакета
ПримечаниеАвтору 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
Затем он рассматривается как поддельный toc, считанный ранее (и временно сохраненные значения), перезаписанные в ходе выполнения.
\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
файл уже в первом запуске. Это, конечно, не удается, поскольку такого файла нет, если он был удален или документация компилируется в первый раз.