4

The following code typesets a 12 page document; text only appears on every other page and there's an empty page between two text pages:

\documentclass{article}
\usepackage{atbegshi}
\usepackage{lipsum}

\makeatletter
\AtBeginShipout{%
  \ifodd\c@page
    \begingroup
      \AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox
      \global\AtBegShi@Discardedtrue
      \mbox{}\thispagestyle{empty}\clearpage
    \endgroup
  \else
  \fi%
}
\makeatother

\begin{document}

\lipsum[1-30]

\end{document}

Now I am trying to number sequentially the pages with text in them; with the code above pages with text are numbered 1,3,5,... but I would like them to be 1,2,3,... I did some attempts but none of them produced the desired result. How can I achieve the consecutive numbering?

The idea is to wrap this into a command that will allow to change the behaviour of the text flow inside a group: when invoked, the command will cause the text to be typeset in every other page but maintaining the consecutive numbering for the pages with text; once the group ends the text and numbering must return to their normal behaviour.

6
  • 1
    What is an odd page? The third absolute page would have page number two. Aug 19, 2012 at 6:18
  • 2
    I do not recommend explcitly or implicitly calling \shipout inside \AtBeginShipout. An empty page can output by \AtBeginShipoutOriginalShipout\null (absolute page counters have to be corrected manually). \clearpage might even add further float pages. Aug 19, 2012 at 6:22
  • 2
    In the shipout routine the page and therefore its page number is known, \label based approaches like \checkoddpage are not needed. Aug 19, 2012 at 6:24
  • @HeikoOberdiek thanks for your comments; I quite don't understand the second one... this is the first time I am trying to do something with the shipout mechanism and I am kind of lost. Where am I calling \shipout inside \AtBeginShipout? Why don't you recommend doing so? Aug 19, 2012 at 13:35
  • The shipout routine is called by the asynchronous output routine, in the middle of shipout the example adds material to the main vertical list and calls \clearpage. Thus it can happen that in the middle of shipping out a page material is added somewhere later and the output routine is called again and might want to ship out a page while shipping out a previous page. A mess already. atbegshi is not reentrant, one of the reasons are possible global assignments of the shipout box. Aug 19, 2012 at 14:02

1 Answer 1

5

This seems to work for me. The trick is to use a different counter for the number of shipouts instead of the pages.

\documentclass{article}
\usepackage{changepage}
\usepackage{etoolbox}
\usepackage{atbegshi}
\usepackage{lipsum}

\newcounter{mypage}

\makeatletter
\AtBeginShipoutInit{\setcounter{mypage}{0}}
\AtBeginShipout{
  \ifnumodd{\value{mypage}}{
    \AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox
    \addtocounter{page}{-1}
  }{
    \mbox{}\thispagestyle{empty}\clearpage
  }
  \addtocounter{mypage}{1}
}

\makeatother

\begin{document}

First page \pageref{test}
Last page \pageref{test2}

\label{test}
\lipsum[1-30]
\label{test2}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .