# Evaluate expression/macro at the end

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.

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:

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

• I just quickly wrote the example:p. Normally I use \newline but this was just quickly written as an example.
– Ömer
Sep 1, 2020 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. Sep 1, 2020 at 9:59
• Now it works, but @egreg answer is simpler ;-) Sep 1, 2020 at 10:33

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}


• 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, 2020 at 10:44
• @Ömer Well, it's hard to imagine what kind of things you'd like to evaluate. Sep 1, 2020 at 12:01

This uses an xyz aux file to save the information.

\documentclass{article}
\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}
\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:

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.

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