
Wie kann man die Gesamtzahl der Abschnitte zählen?innerhalbjedes Kapitel?
Ich versuche, dietotcount
Paket zu verwenden, aber es gibt die Gesamtzahl der Abschnitte deszuletztKapitel, nicht die Gesamtzahl der Abschnitte desaktuellKapitel.
Im folgenden MWE wäre das gewünschte Verhalten, 3 Abschnitte für das erste Kapitel und 1 Abschnitt für das zweite und letzte Kapitel zu melden.
totcount
meldet nur 1 Abschnitt (den Zählerwert für das letzte Kapitel).
\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}
Wie kann man die Gesamtzahl der Abschnitte in jedem Kapitel zählen?
Hinweis: totcount
meldet 0 Abschnitte bei Verwendung \section*
, siehe Kommentar von Sigur.
Antwort1
BearbeitenDie Vorausschau-Version, um die Abschnittsnummern pro Kapitel im Voraus zu kennen, finden Sie ganz am Ende dieses Beitrags. Sie erfordert zwei Kompilierungsläufe.
Diese Frage führte zu einem neuen Paket cntperchap
, das in der Version 0.2 seit dem 5.9.2015 auf CTAN verfügbar ist.
Dabei wird das assoccnt
Paket verwendet (dessen Autor ich zufälligerweise recht gut kenne ;-))
Es ordnet totalsections
dem section
Zähler einen Zähler zu. Jedes Mal, wenn der section
Zähler erhöht wird, totalsections
wird auch der Zähler erhöht.
Es gibt jedoch kein automatisches Zurücksetzen für \chapter*
die Verwendung. In diesem Fall kann dies automatisch erfolgen, indem Code vor das Paket „ \chapter
using from“ gestellt wird.\xpretocmd
xpatch
NotizDer Autor assoccnt
sollte dies unbedingt in sein Paket integrieren ;-)
\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}
BearbeitenEinige neue Versionen, die eine Abschnittsanzahl pro Kapitel verwenden (erfordert zwei Durchläufe, um erfolgreich zu sein).
Erklärung: Bei jedem neuen Kapitel wird die kumulierte Anzahl der Abschnitte in eine externe Datei geschrieben, kurz \jobname.seccnt
gesagt . Diese Datei wird beim nächsten Latex-Kompilierungslauf erneut gelesen und die Werte werden in einer Liste gespeichert. Das Makro durchläuft diese Liste, bis es an der richtigen Stelle ist, und gibt dann die Anzahl der Abschnitte in diesem Kapitel aus, auch im Voraus. (Das Makro sollte erweiterbar sein, ich denke, das ist der Fall.)foo.seccnt
etoolbox
\GetTotalSectionCounter
Derzeit ist es notwendig, die foo.seccnt
Datei manuell zu entfernen, wenn sich die Anzahl der Kapitel/Abschnitte geändert hat.
Ich werde versuchen, diesen Nachteil zu umgehen.
\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}
BearbeitenDies ist die Version ohne explizites Löschen der foo.seccnt
Datei
Ich habe den addtocontents
Ansatz verwendet LaTeX
, die Abschnittsnummern in eine separate Datei schreiben zu lassen, so wie es mit den toc
zugehörigen Sachen gemacht wird. Das foo.seccnt
wird dann als gefälschtes Inhaltsverzeichnis behandelt, das vorher eingelesen (und die Werte zwischengespeichert) und im Lauf neu geschrieben wird.
\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}
BearbeitenDer OP gsl hat einen Fehler in diesem Code festgestellt. Ich konnte ihn darauf zurückführen, dass bereits beim ersten Durchlauf \@startfaketoc
versucht wird, die externe Datei einzulesen foo.seccnt
. Dies schlägt natürlich fehl, da es keine solche Datei gibt, wenn sie gelöscht wurde oder das Dokument zum allerersten Mal kompiliert wird.