2

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}
3

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}

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

enter image description here

top of p. 3:

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

top of p. 7:

enter image description here

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
    +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

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.