TransWikia.com

Count number of succeeding sections within the text

TeX - LaTeX Asked by mhr on April 26, 2021

Writing about structure of the remainder of my article in the Introduction section, I want to dynamically get how many sections are left to the end of the manuscript.

So in case my article consists of six sections in total, I would need a command foo once called within the body of the first section, renders to "five" and so on.

3 Answers

The following stores the value of the section counter minus 1 as a label at the end of the document. It's then retrieved using a direct form of ref and turned into a string using fmtcount.

enter image description here

documentclass{article}

usepackage{fmtcount,xfp}
usepackage{lipsum}

makeatletter
AtEndDocument{%
  def@currentlabel{inteval{value{section}-1}}%
  label{foolabel}%
}
newcommand{foo}{%
  ifcsname r@foolabelendcsname
    expandafternumberstringnumexpandafter{expandafter@firstoftwor@foolabel}%
  else
    0%
  fi
}
makeatother

begin{document}

sloppy
section{Introduction}
There are foo{} remaining sections.

section{First section}lipsum[1-50]
section{Second section}lipsum[1-50]
section{Third section}lipsum[1-50]
section{Fourth section}lipsum[1-50]
section{Fifth section}lipsum[1-50]

end{document}

Since it uses the label-ref system to extract the last sectional number, you need to compile twice with every change in the number of sections (so the references can settle).


If you're dealing with unnumbered sections and want to include them as well (typically a reference section), then you can use the following setup:

documentclass{article}

usepackage{fmtcount,xfp}
usepackage{lipsum}

newcounter{totsection}

letoldsectionsection
renewcommand{section}{stepcounter{totsection}oldsection}

makeatletter
AtEndDocument{%
  def@currentlabel{inteval{value{totsection}-1}}%
  label{foolabel}%
}


newcommand{foo}{%
  ifcsname r@foolabelendcsname
    expandafternumberstringnumexpandafter{expandafter@firstoftwor@foolabel}%
  else
    0%
  fi
}
makeatother

begin{document}

sloppy
section{Introduction}
There are foo{} remaining sections.

section{First section}lipsum[1-50]
section{Second section}lipsum[1-50]
section{Third section}lipsum[1-50]
section{Fourth section}lipsum[1-50]
section{Fifth section}lipsum[1-50]
section*{Sixth section}lipsum[1-50]

end{document}

It uses a different counter totsection that counts a section whether it's numbered or not.

Correct answer by Werner on April 26, 2021

EDITED to auto-calculate the number of sections in the document.

If you want the code to calculate the total number of sections, the refcount package can help. One can use an AtEndDocument hook to set the label to capture the final section number.

documentclass{article}
usepackage{refcount}
newcommandsectionsleft{thenumexpr 
  getrefnumber{mylastsection}-thesectionrelax}
AtEndDocument{label{mylastsection}}
begin{document}
section{First}

My sections left are sectionsleft.

section{Second}

My sections left are sectionsleft.

section{Third}
section{Fourth}
section{Fifth}

My sections left are sectionsleft.

section{Last}

There are sectionsleft{} sections remaining.
end{document}

enter image description here

Answered by Steven B. Segletes on April 26, 2021

The package totcount does this with minimal intervention.

documentclass{article}
usepackage{totcount}

regtotcounter{section}

begin{document}

This paper consists of total{section} sections.

section{A}
section{B}
section{C}
section{D}
section{E}
section{F}

end{document}

enter image description here

Answered by egreg on April 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP