1

I am trying to create a new counter, which is counting the page numbers to store them and reuse them later. I always remove the 3 pages, which are not numbered at the beginning of the document and I would then just add this value of \CurrPage to my counter. So far I've managed to extract the page number from Roman using zref, but this value can't be added to my counter.

\documentclass[a4paper, fontsize = 12pt,headsepline]{scrartcl}
% Page Numbering Roman
\pagenumbering{Roman}
\usepackage{lipsum}
\usepackage{calc}
\usepackage[abspage,user,lastpage]{zref}


\makeatletter
\newcommand*{\CurrPage}{%
\zlabel{page.num} 
\number\numexpr
\zref@extract{page.num}{abspage}%
-3%
\relax
}
\makeatother

\begin{document}
% Counter Definition Framework
\newcounter{Page.Num.Framework}
\lipsum[1-25]

Current Page Num: {\CurrPage}

%\addtocounter{Page.Num.Framework}{\CurrPage}

\end{document}

If uncommented, the \addtocounter command returns the error:

Missing number, treated as zero. \addtocounter{Page.Num.Framework}{\CurrPage}
1

You have two problems: A general one and one of using calc.

First the general one: \CurrPage expands to first setting a label and next using the value generated by the label. Setting the label is not a number, so adding \zlabel{…} to a counter always fails. This problem could be solved by a simple change of \CurrPage:

\newcommand*{\CurrPage}{%
  \number\numexpr
  \zref@extract{page.num}{abspage}%
  -3%
  \relax
  \zlabel{page.num}%
}

Now, \CurrPage first results in a number an then in generation of a label. With this:

\documentclass[a4paper, fontsize = 12pt,headsepline]{scrartcl}
% Page Numbering Roman
\pagenumbering{Roman}
\usepackage{lipsum}
%\usepackage{calc}
\usepackage[abspage,user,lastpage]{zref}


\makeatletter
\newcommand*{\CurrPage}{%
  \number\numexpr
  \zref@extract{page.num}{abspage}%
  -3%
  \relax
  \zlabel{page.num}%
}
\makeatother

\begin{document}
% Counter Definition Framework
\newcounter{Page.Num.Framework}
\lipsum[1-25]

Current Page Num: {\CurrPage}

Before: \arabic{Page.Num.Framework}
\addtocounter{Page.Num.Framework}{\CurrPage}
After: \arabic{Page.Num.Framework}

\end{document}

works (but is still ugly).

The special problem of using calc is, that calc allows calculations in \addtocounter and with that package the argument of \addtocounter must be a numeric expression without commands that do not expand to numbers. So

\documentclass[a4paper, fontsize = 12pt,headsepline]{scrartcl}
% Page Numbering Roman
\pagenumbering{Roman}
\usepackage{lipsum}
\usepackage{calc}
\usepackage[abspage,user,lastpage]{zref}


\makeatletter
\newcommand*{\CurrPage}{%
  \number\numexpr
  \zref@extract{page.num}{abspage}%
  -3%
  \relax
  \zlabel{page.num}%
}
\makeatother

\begin{document}
% Counter Definition Framework
\newcounter{Page.Num.Framework}
\lipsum[1-25]

Current Page Num: {\CurrPage}

Before: \arabic{Page.Num.Framework}
\addtocounter{Page.Num.Framework}{\CurrPage}
After: \arabic{Page.Num.Framework}

\end{document}

still fails and you have to split of \CurrPage in a label generation and label usage:

\documentclass[a4paper, fontsize = 12pt,headsepline]{scrartcl}
% Page Numbering Roman
\pagenumbering{Roman}
\usepackage{lipsum}
\usepackage{calc}
\usepackage[abspage,user,lastpage]{zref}


\makeatletter
\newcommand*{\StoreCurrPage}{\zlabel{page.num}}
\newcommand*{\UseCurrPage}{%
  \numexpr
  \zref@extract{page.num}{abspage}%
  -3%
  \relax
}
\makeatother

\begin{document}
% Counter Definition Framework
\newcounter{Page.Num.Framework}
\lipsum[1-25]

Before: \arabic{Page.Num.Framework}
\StoreCurrPage\addtocounter{Page.Num.Framework}{\UseCurrPage}
After: \arabic{Page.Num.Framework}

\end{document}
  • Thank you, well I just copy&pasted a somehow "working" version, so I was not aware of the underlying concepts. – hannes101 Mar 27 at 19:54

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.