5

Suppose you have a document where you want to mention how many sections there are in the beginning. For example, something like this:

\documentclass{article}
\begin{document}

  Abstract\\There are ... sections in this document.

  \section{Section 1}
  \section{Section 2}
  \section{Section 3}

\end{document}

Here ... would be a macro (or something else) that says that there are 3 sections.

I, unfortunately, could not find a way to have that macro evaluated at the end (where it reports 3).

In this example, I want to know the number of sections in the end. There might be a solution using counters (in some way), but I am really looking for a solution where I can have some influence the order of evaluation of the macros.

3

You can use \AtEndDocument (and \AtBeginDocument to set the macro in the first run):

\documentclass{article}

\makeatletter
\AtEndDocument{
    \write\@auxout{\string\gdef\string\previousrunsections{\thesection}}%
}
\AtBeginDocument{%
    \ifcsname previousrunsections\endcsname
    \else
        \gdef\previousrunsections{??}%
    \fi
}
\makeatother
\begin{document}

  Abstract
  
  \noindent There are \previousrunsections{} sections in this document.

  \section{Section 1}
  \section{Section 2}
  \section{Section 3}

\end{document}

After at least two runs, you get:

enter image description here

If you need more control, the package etoolbox gives you a lot of hooks.

PD: don't use \\ to end lines or paragraphs in normal text!

3
  • I just quickly wrote the example:p. Normally I use \newline but this was just quickly written as an example.
    – Ömer
    Sep 1 '20 at 9:40
  • Hmm... looking at it probably my answer is wrong --- I didn't catch that you needed the number of sections at the start.
    – Rmano
    Sep 1 '20 at 9:59
  • Now it works, but @egreg answer is simpler ;-)
    – Rmano
    Sep 1 '20 at 10:33
6

You can put the total number wherever you want by using totcount.

\documentclass{article}
\usepackage{totcount}

\regtotcounter{section}

\begin{document}

\title{Title}
\author{Ömer}

\maketitle

\begin{abstract}
This is the abstract.

There are \total{section} sections in this document.
\end{abstract}

\section{Section 1}

\section{Section 2}

\section{Section 3}

\end{document}

enter image description here

2
  • This is a solution I found, but if I am correct, the total macro cannot be used for any expression. It can only be used for counters. But still thank you for the time you put in!
    – Ömer
    Sep 1 '20 at 10:44
  • 1
    @Ömer Well, it's hard to imagine what kind of things you'd like to evaluate.
    – egreg
    Sep 1 '20 at 12:01
2

This uses an xyz aux file to save the information.

\documentclass{article}
\newcommand\addxyzline[1]{\addtocontents {xyz}{#1}}
\makeatletter
\newcommand\writexyz{\@starttoc{xyz}}
\makeatother
\begin{document}
%\tableofcontents% CAN UNCOMMMENT TO SEE THAT toc WORKS FINE
\noindent Abstract\\There are \writexyz sections in this document.

\section{Introduction}
\section{Next}
\section{Third}
\addxyzline{\thesection}
\end{document}

Upon compilation, the .xyz file contains, in this case, the number 3, and the .aux file contains

\relax 
\@writefile{toc}{\contentsline {section}{\numberline {1}Introduction}{1}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {2}Next}{1}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {3}Third}{1}\protected@file@percent }
\@writefile{xyz}{3}

The output is thus:

enter image description here

Note: the given version works regardless of the name of your input file. If you prefer not working with the toc approach, you could have hardwired it to your document name, instead defining

\newcommand\writexyz{\input junk.xyz }

where in this case the document must be junk.tex.

1
  • This is a very nice solution which seems to give you a lot of power. Thank you for showing it!
    – Ömer
    Sep 1 '20 at 9:42

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.