
총 섹션 수를 어떻게 계산할 수 있습니까?이내에각 장?
나는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
사용하기 위해 일부 코드를 추가하여 자동으로 수행할 수 있습니다.\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
다음 라텍스 컴파일 실행 시 이 파일을 다시 읽고 값이 목록에 저장됩니다 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
물론 이는 실패합니다. 삭제되었거나 문서가 처음으로 컴파일된 경우 해당 파일이 없기 때문입니다.