1

I want to create a counter that counts the total number of \parts. I have the totcount package installed, and I have read the package documentation, without being able to even remotely create something that works even a little bit (so I don't have an MWE).

It seems no one else has the same problem as me, because I can't find any relatable question which has a solution that can be applied to my problem.

I'm using the online-based LaTeX solution called Overleaf, but my experience tells me that everything is working the same way as the 'offline' solutions.

Any help is much appreciated.

2
  • Are all \parts of your document numbered, or are there unnumbered parts as well (presumably created by \part*)? – Mico Mar 28 '20 at 13:21
  • All of them are numbered – L. Nielsen Mar 28 '20 at 13:32
5

According to the documentation of the totcount package, use \regcount to register a counter and \total to retrieve the count:

\regtotcounter{part}
\newcommand*{\partcount}{\total{part}}

Example:

\documentclass{article}

\usepackage{totcount}

\regtotcounter{part}
\newcommand*{\partcount}{\total{part}}

\begin{document}

This document has \partcount{} parts.

\part{A}

\part{B}

\part{C}

\end{document}

enter image description here

Note that you need to run LaTeX twice to get the count.


You can use the fmtcount package (documentation) if you want the number to be spelled out in English (three instead of 3). The \numberstringnum only accepts nonnegative values, so the first run needs special treatment (where the count is -1): (using the etoolbox package)

\newcommand*{\partcount}{%
  \ifnumcomp{\totvalue{part}}{<}{0}{???}{\numberstringnum{\totvalue{part}}}%
}

Example:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{fmtcount}
\usepackage{totcount}

\regtotcounter{part}
\newcommand*{\partcount}{%
  \ifnumcomp{\totvalue{part}}{<}{0}{???}{\numberstringnum{\totvalue{part}}}%
}

\begin{document}

This document has \partcount{} parts.

\part{A}

\part{B}

\part{C}

\end{document}

First run:

enter image description here

Second run:

enter image description here

The package offers more styles, such as Three, THREE, third, 3rd, 3rd, etc. See the documentation for the corresponding commands.

4
  • This works as intended, but, because all numbers below 10 are required to be spelled out (a requirement from my university), is it then possible to make it spell out the number ("three" in this case)? – L. Nielsen Mar 28 '20 at 15:10
  • @L.Nielsen Does your university really want all single digit numbers to be spelled out? What about page numbers, sectional division numbers, float numbers, equation numbers, etc.? What is your university? --- GOM – Peter Wilson Mar 28 '20 at 19:12
  • 1
    @L.Nielsen I've updated the answer to address that. – L. F. Mar 29 '20 at 2:40
  • @PeterWilson Unfortunately yes. I'm a student on Aalborg University in Denmark. Solely numbers in-line: "I have 10 fingers but only two hands", page numbers, figure numbers, chapters, sections, and so on aren't included in the requirement. – L. Nielsen Mar 29 '20 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.