# How to define two labels inside an environment to compute the number of pages the environment spans?

I would like to count the number of pages that a certain environment (named myenv) spans. In principle, I set two labels, one in the beginning and one in the end of the environment. These labels should be unique for every call of the environment, which is the part that still fails (.tex:19: Missing \endcsname inserted):

\documentclass{scrartcl}
\usepackage{blindtext}

\usepackage{refcount}
\newcommand{\pageRange}[2]{%
\number\numexpr\getpagerefnumber{#2}-\getpagerefnumber{#1}+1\relax}

\newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
\setcounter{howOftenCalled}{0}
\newenvironment{myenv}[1]{
\stepcounter{howOftenCalled}% increase counter
\label{startPage:\ref{howOftenCalled}}% define the first label
{\LARGE The following text covers \pageRange{startPage:\ref{howOftenCalled}}{endPage:\ref{howOftenCalled}} pages:}\\% compute the number of pages between the first and the second label
\blindtext[#1]
}{\label{endPage:\ref{howOftenCalled}}\clearpage}% defined the second label

\begin{document}
\begin{myenv}{8}
\end{myenv}
\clearpage
\begin{myenv}{15}
\end{myenv}
\end{document}


Your code works except that you cannot retrieve the value of your counter with \ref{howOftenCalled}. If you use \number\value{howOftenCalled} instead, everything works as expected.

\documentclass{scrartcl}
\usepackage{blindtext}

\usepackage{refcount}
\newcommand\NumberOfPages[1]{\edef\tmp{\number\numexpr\getpagerefnumber{endPage:#1}-\getpagerefnumber{startPage:#1}+1\relax}%
\ifnum\tmp=1\relax%
1 page%
\else%
\tmp\space pages%
\fi}

\newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
\setcounter{howOftenCalled}{0}
\newenvironment{myenv}[1]{%
\stepcounter{howOftenCalled}% increase counter
\label{startPage:\number\value{howOftenCalled}}% define the first label
{\LARGE The following text covers
\NumberOfPages{\number\value{howOftenCalled}}:}\\% compute the number of pages between the first and the second label
\blindtext[#1]
}{\label{endPage:\number\value{howOftenCalled}}\clearpage}% defined the second label

\begin{document}
\begin{myenv}{8}
\end{myenv}
\clearpage
\begin{myenv}{15}
\end{myenv}
\clearpage
\begin{myenv}{1}
\end{myenv}
\end{document}


top of p. 1:

top of p. 3:

[![enter image description here][2]][2]

top of p. 7:

ACKNOWLEDGEMENTS: Big thanks to @Andrew and @UlrikeFischer, who made suggestions how to make the code more elegant and secure!

• Note that you may have to remove the aux file if you just copy this code into your old file that caused the problem. – marmot Nov 23 '18 at 4:32
• +1 I would use \newcommand\NumberOfPages[1]{\number\numexpr\getpagerefnumber{endPage:#1}-\getpagerefnumber{startPage:#1}+1\relax}, which would be used as \NumberOfPages{thehowOftenCalled}. Perhaps this macro should should print page or pages X depending on whether or not the environment spans multiple pages (since 1 pages is grammatically incorrect). – Andrew Nov 23 '18 at 5:21
• I wouldn't use \thecounter in contexts like labels where you actually actually want something "simple". The \the-commands are meant for typesetting, the default definition uses internally \@arabic` and if some package, e.g. for arabic or hebrew, redefines this, your code will explode. Use e.g. \number\value{howOftenCalled} instead. – Ulrike Fischer Nov 23 '18 at 8:36