3

I know that I can use \thepage to get the number of the current page. However I'd like to print out the page-number of the previous page. How can I go about that?

Background:
I don't number the first page in my document and want to start the numbering with 1 on the second page. However I can't use \setcounter{page}{1} on that site as I want every mechanism (namely the bindingoffset of the geometry package) to treat the page-numbering as if the first page does have the number 1. In short I want the pages with a (then) even page number to be treated as odd pages and vice versa.

4

You can get the pagenumber of the previous page with the following expression: \the\numexpr\value{page}-1\relax

  • This will only work for arabic page numbering (which might be all the OP needs, of course). – David Purton Nov 8 '18 at 13:52
  • In my case it is indeed all I want so thanks :) – Raven Nov 8 '18 at 17:09
2

Using \thepage is not guaranteed to yield the number of the current page.

Example:

\documentclass{article}

\usepackage{lipsum} % for mock text

\setlength{\textheight}{2cm} % to make smaller pictures

\begin{document}

\lipsum*[2] \\
--\thepage-- is wrong

\end{document}

enter image description here

You need to use the \label-\ref mechanism to ensure correctness.

\documentclass{article}
\usepackage{refcount}

\usepackage{lipsum} % for mock text

\newcounter{printpagecount}
\newcommand{\printpage}[1]{%
  \refstepcounter{printpagecount}%
  \label{\theprintpagecount}%
  \the\numexpr\getpagerefnumber{\theprintpagecount}+(#1)\relax
}

\setlength{\textheight}{2cm} % to make smaller pictures

\begin{document}

\lipsum*[2] \\
\printpage{0} (current) and \printpage{-1} (previous)

\end{document}

enter image description here

If instead your aim is to confuse readers by having odd numbered pages on the left and even numbered ones on the right, just define suitably \thepage:

\documentclass[twoside,a5paper]{article}
\usepackage{geometry}

\usepackage{lipsum} % for mock text

\renewcommand{\thepage}{\the\numexpr\value{page}-1\relax}


\begin{document}

\thispagestyle{empty}

\lipsum[1-30]


\end{document}

enter image description here

A5 paper used just for making a smaller picture.

1

It occurred to me that the way I originally did this is a bit silly and completely unnecessary…

You can just temporarily change the value of the page counter and call \thepage. Who knows what I was thinking. It was probably late at night.

\documentclass{article}
\newcommand{\thepagelessone}{%
  \addtocounter{page}{-1}%
  \thepage
  \stepcounter{page}}
\begin{document}
\setcounter{page}{5}
\verb|\thepage|: \thepage; \verb|\thepagelessone|: \thepagelessone

\pagenumbering{roman}
\setcounter{page}{7}
\verb|\thepage|: \thepage; \verb|\thepagelessone|: \thepagelessone
\end{document}

output


Original silly solution

Here's a solution using expl3. It correctly handles the various styles of page numbering (arabic, roman, etc.)

It works by expanding \thepage once, then detokenizing the result. The current page number (\c@page) is removed from the end of the token list and replaced by this number less one.

\documentclass{article}
\usepackage{xparse}
\makeatletter
\ExplSyntaxOn
\tl_new:N \l__raven_thepage_tl
\int_new:N \l__raven_page_int
\cs_new:Nn \__raven_page_less_one:n
  {
    \tl_set:Nn \l__raven_thepage_tl { #1 }
    \tl_remove_once:Nn \l__raven_thepage_tl { \c@page }
    \int_set_eq:NN \l__raven_page_int \c@page
    \int_decr:N \l__raven_page_int
    \tl_put_right:Nn \l__raven_thepage_tl { \l__raven_page_int }
    \tl_use:N \l__raven_thepage_tl
  }
\cs_generate_variant:Nn \__raven_page_less_one:n { o }
\NewDocumentCommand \thepagelessone { }
  {
    \__raven_page_less_one:o { \thepage }
  }
\ExplSyntaxOff
\makeatother
\begin{document}
\setcounter{page}{5}
\verb|\thepage|: \thepage; \verb|\thepagelessone|: \thepagelessone

\pagenumbering{roman}
\setcounter{page}{7}
\verb|\thepage|: \thepage; \verb|\thepagelessone|: \thepagelessone
\end{document}

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.