Better solution
After finding out about refcount
we can skip the calculations with counters (and avoid running into problems with hyperref
and other packages that redefine \ref
so that it is more than just some digits).
I have now borrowed the idea of egreg to outsource the calculations. It makes a much cleaner code.
Code
\documentclass{article}
\usepackage{lastpage}
\usepackage{refcount}
\newcounter{tempcounter}
\newcommand*{\pageorpages}[1]{#1~page\ifnum#1=1 \else s\fi}
\makeatletter
\newcommand*{\textpages}{%Calculation of _text_ pages
\ifx\r@DontYouDareUseThisLabelApndxStart\@undefined% without appendix
\number\numexpr\getpagerefnumber{LastPage}+1-\getpagerefnumber{FirstTextPage}\relax
\else % with appendix
\number\numexpr\getpagerefnumber{DontYouDareUseThisLabelApndxStart}-\getpagerefnumber{FirstTextPage}\relax
\fi%
}
\newcommand*{\apndxpages}{% Calculation of _appendix_ pages.
\ifx\r@DontYouDareUseThisLabelApndxStart\@undefined0\else % to prevent errors if this is called accidently
\number\numexpr\getpagerefnumber{LastPage}-\getpagerefnumber{DontYouDareUseThisLabelApndxStart}+1\relax
\fi
}
\newcommand*{\howmanypages}{% \frontmatter is already defined in some classes
The following text consists of \pageorpages{\textpages}%
\ifx\r@DontYouDareUseThisLabelApndxStart\@undefined\else % with appendix
\space plus an appendix of \pageorpages{\apndxpages}%
\fi.
\clearpage % again, no sketchy calculations
\label{FirstTextPage}
}
\makeatother
\newcommand*{\apndx}{
\clearpage
\section*{Appendix}
\label{DontYouDareUseThisLabelApndxStart} % and I mean it!
}
\usepackage{lipsum}
\begin{document}
\lipsum
\howmanypages
\section{Easy steps}
\lipsum
\section{Advanced steps}
\lipsum
\apndx
\lipsum
\end{document}
Bad Solution (breaks with hyperref
and what not)
With the use of one auxiliary counter you can accomplish something like this very easily.
\apndx
The macro apndx
uses a \clearpage
so that it actually starts on an own page. Otherwise the calculation may be sketchy.
The label apndxstart
is not only used to get the actual page number, but also to check if there is even an appendix (via the macro \r@apndxstart
).
\howmanypages
This macro checks whether a appendix is used:
\ifx\r@apndxstart\@undefined
and then proceed to calculate the actual page numbers.
lastpage
package
The lastpage
packages is used to get the last page number (with or without appendix).
Code
\documentclass{article}
\usepackage{lastpage}
\newcounter{tempcounter}
\makeatletter
\newcommand*{\howmanypages}{% \frontmatter is already defined in some classes
\ifx\r@apndxstart\@undefined % without appendix
\setcounter{tempcounter}{\pageref{LastPage}}
\addtocounter{tempcounter}{1}
\else% with appendix
\setcounter{tempcounter}{\pageref{apndxstart}}%
\fi%
\addtocounter{tempcounter}{-\pageref{FirstPage}}
This text consists of \thetempcounter{} page\ifnum\thetempcounter=1\else s\fi
\ifx\r@apndxstart\@undefined % without appendix
.
\else % with appendix
\setcounter{tempcounter}{\pageref{LastPage}}%
\addtocounter{tempcounter}{-\pageref{apndxstart}}%
\addtocounter{tempcounter}{1}%
\space plus an appendix of \thetempcounter{} page\ifnum\thetempcounter=1\else s\fi.
\fi
\label{FirstPage}
}
\makeatother
\newcommand*{\apndx}{
\clearpage
\section*{Appendix}
\label{apndxstart}
}
\usepackage{lipsum}
\begin{document}
\lipsum
\howmanypages
\section{Easy steps}
\lipsum
\section{Advanced steps}
\lipsum
\apndx
\lipsum
\end{document}
\label{append}
within the appendix (easy) and to define in the preamble a macro like this :\def\apndx{\ref{append}}
. When the appendix is present,\apndx
gets a value, fine. Else the compiler sends a warning (normal) and\apndx
seems to get the value ?? and not\relax
.\ref
and\label
need a second compilation run.