# Count number of succeeding sections within the text

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.

• \the\numexpr 6-\thesection\relax – Steven B. Segletes Dec 1 '20 at 18:56
• 6 is not known and varies. @StevenB.Segletes – mhr Dec 1 '20 at 20:35

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.

\documentclass{article}

\usepackage{fmtcount,xfp}
\usepackage{lipsum}

\makeatletter
\AtEndDocument{%
\def\@currentlabel{\inteval{\value{section}-1}}%
\label{foolabel}%
}
\newcommand{\foo}{%
\ifcsname r@foolabel\endcsname
\expandafter\numberstringnum\expandafter{\expandafter\@firstoftwo\r@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}

\let\oldsection\section
\renewcommand{\section}{\stepcounter{totsection}\oldsection}

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

\newcommand{\foo}{%
\ifcsname r@foolabel\endcsname
\expandafter\numberstringnum\expandafter{\expandafter\@firstoftwo\r@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.

• Doesn't work in my case. What if the document contains Appendices and/or References after the last \section? – mhr Dec 1 '20 at 20:30
• @mhnrahmani: Shouldn't matter, unless they're called with \section* (an unnumbered section). You should provide a complete minimal example that shows the type of setup you have to deal with. Can you do that? – Werner Dec 1 '20 at 20:42
• @mhnrahmani: I've added an update to my answer that counts numbered and unnumbered sections. – Werner Dec 1 '20 at 20:46
• Well, the reason it doesn't work for me is that my template resets the section counter in order to categorize the appendices with A.B.C letters I guess. I am working on my own local repo. but heres the template I am working on: overleaf.com/latex/templates/mdpi-article-template/fvjngfxymnbr – mhr Dec 1 '20 at 21:19
• @mhnrahmani: What about my updated option that uses a new counter? – Werner Dec 1 '20 at 21:24

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}
\newcommand\sectionsleft{\the\numexpr
\getrefnumber{mylastsection}-\thesection\relax}
\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}


• Isn't there a way to retrieve sections count without manually labeling the last section like so? (Take into account what if an extra section with another label adds to the document later? Then the label used in \newcommand should update as well) – mhr Dec 1 '20 at 20:34
• @mhnrahmani If you place the label immediately before the \end{document}, you should be covered. – Steven B. Segletes Dec 1 '20 at 20:42
• @mhnrahmani Better still, you can place in the preamble the following: \AtEndDocument{\label{mylastsection}}. – Steven B. Segletes Dec 1 '20 at 20:44
• @mhnrahmani You should also note that using a \label/\ref approach, two compilations are required to resolve any changes in the total number of sections. – Steven B. Segletes Dec 1 '20 at 20:50

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}