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}
% adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
\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}

\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 printpageorpages Xdepending on whether or not the environment spans multiple pages (since1 pagesis grammatically incorrect). – Nov 23 '18 at 05:21\thecounterin 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 08:36