22

My problem is that I would like to have either blank or figures on the left pages, and text on the right ones, with the numbering of the right pages to be 1,2,3,4,5 and not 1,3,5,7.

It means, in a book-derived class, to have right pages, with the odd layout, but with also an even number half the time.

It also means that I would like to have, by default, a blank page everywhere on a left page, except if a picture has been defined somehow before on the previous page.


Update:

I've tried it. There is a little \makeatother missing, but I spotted it right away. It seems to work fine alternating pages and inserting blank or figure pages. However, I have two problems:

  • The class (book-derived) sometimes introduces blank pages of its own. I can tweak it, but would prefer to be able to turn on/ turn off the action of the blankpage routine along the course of the document.
  • The layout of the text pages depends on the odd/even number of the page. Since my text pages have odd and even numbers (which is the intended result), the layout is now alternatively odd or even, which is not what I would like, since there they will all be on the right hand side of the doc when open...

Do you have any clue as to how to manage this? I think the second problem is way more difficult...

Thank you already for this answer both simple and effective for achieving the result.

EDIT 2 (19/12/2011) : The answer from Bruno Le Floch seems to me more complete and I recommend it.

Martigan
  • 3,146
  • you can produce two oneside documents and combine the two documents with pdfpages. – Marco Daniel May 13 '11 at 14:15
  • True, it is my fallback option. However, I won't be able to get the list of pictures on a right page, and in the table of contents... – Martigan May 13 '11 at 14:39
  • Welcome to TeX.SX. I merged your "guest" account to the registered one. You are now able to edit the question. I moved your answer post already to it and deleted that post to keep the answer section tidy. – Martin Scharrer May 17 '11 at 17:32
  • I think Heiko's atbegshi package (as used in my answer to reversing pages from now onwards) may be useful: save pages as they arrive, treat figures completely independently, increase \count0 by two at each page (perhaps with afterpage), but have \renewcommand\thepage{\the\numexpr\count0/2\relax}, or something like that. – Bruno Le Floch Dec 12 '11 at 20:17

2 Answers2

19

This is an enhancement of code that I have found on c.t.t. It uses the afterpage package to skip every second page and then if you add a figure with the \addfig command it will output it on the next page. It seems to work reasonable well (to my own surprise also ;-)

\documentclass[english,oneside]{book}
\usepackage{babel}
\usepackage{afterpage}

\makeatletter
\newcommand\@addfig{\relax}
\newcommand\addfig[1]{\global\long\def\@addfig{#1}}
\newcommand\@putfig{\@addfig\addfig{\relax}}
\newcommand\blankpage{%
    \null
    \vfill
    \@putfig%
    \vfill
    \thispagestyle{empty}%
    \addtocounter{page}{-1}%
    \clearpage
    \afterpage{\blankpage}}
\makeatother

\usepackage{blindtext}

\begin{document}
\afterpage{\blankpage}
\blindtext[2]

\addfig{%
    \begin{figure}
    \centering
    \fbox{\rule{0pt}{3cm}\rule{6cm}{0pt}}
    \caption{Test figure}
    \end{figure}}

\Blindtext
\Blindtext

\addfig{%
    \begin{figure}
    \centering
    \fbox{\rule{0pt}{3cm}\rule{12cm}{0pt}}
    \caption{Test figure 2}
    \end{figure}}

\Blindtext
\Blindtext
\Blindtext
\end{document} 
Danie Els
  • 19,694
  • It works quite well (don't forget to add the makeatother at the end of the definition of commands) actually, and I solved my layout problem by stating oneside in the book class, that ensures that all the layout will be odd side, even for even pages (pun intended). – Martigan May 16 '11 at 09:31
  • @Martigan: I have changed the code by adding the missing \makeatother and changing it to a onesided book class – Danie Els May 18 '11 at 03:12
  • Thank you very much Danie. An other comment: I put the addtocounter{page}{-1} after the \clearpage, in order for my figures to have the same number in the table of figures than the text page opposite. If you keep the code as it is, you have the number of the previous text page. – Martigan May 18 '11 at 09:05
  • Is this the code that ended up in the semioneside package (http://mirror.ctan.org/macros/latex/contrib/semioneside/)? – Ulrich Schwarz Dec 14 '11 at 17:26
  • The semioneside package does seem to share much code. It also seems to share the same problems: "Paragraphs longer than one page can cause the whole mechanism to get out- of-sync. This seems to be due to TEX’s page breaking mechanism. When a paragraph breaks across several pages the \afterpage command is not executed at a page break (at least not reliably) and all gets mixed up. Beware of too long paragraphs!" (documentation for the semioneside package). – Village Dec 15 '11 at 12:29
  • @Ulrich Schwarz: My code is an enhancement from a c.t.t. posting in 2003. It is possible that the 'semionside' package from 2005 used the same original code. I did not know about it but will have a look, thanks. – Danie Els Dec 17 '11 at 09:58
14

I hope I got parity right. It seems that the OP wants the figures to come on the page following the one which references them, and that pages of text should be odd-numbered and on the right, which I would call recto, but Village asked for verso. I went for the OP's requirements. Also, it would make more sense I think to put figures on the page before they are referenced, so that the reader can see them as he reads the corresponding text, but I didn't manage to do that.

I use Heiko Oberdiek's atbegshi package to capture pages just before they are output to the pdf file (at "shipout" time), and add one page of figures per shipout. In order to keep the correct even/odd layout, I increase the page counter for each page of figures (this way, \value{page} is always the true page number in the pdf), but I change \thepage to display the number of text pages (total minus figures).

The figures are collected as we go in \sos@figures, which flushes itself when used (\sos@rest@figures). Each figure added with \addfig is placed in a \vbox, with \vfill between them, so that figures are nicely distributed along the page's height.

Somehow, \shipout\vbox{...} does not simply send the \vbox to the pdf centered on the page, so I move the box by \sos@top and \sos@right to put it at a place which looks like the left-page layout. Any better solution is welcome.

The mechanism can be turned on and off using \SOStrue and \SOSfalse (global switches). The example uses \lipsum, but with paragraph breaks disabled, so that \lipsum[1-10] builds one big paragraph.

\documentclass{book}
\usepackage{atbegshi}
\makeatletter
\newlength{\sos@top}
\newlength{\sos@right}
\newcounter{sos@pages}
\newif\ifSOS
\renewcommand{\thepage}{\the\numexpr\value{page}-\value{sos@pages}\relax}
\newcommand{\addfig}[1]{\g@addto@macro\sos@figures{\vbox{\centering#1}\vfill}}
\newcommand{\sos@reset@figures}
  {\gdef\sos@figures{\sos@reset@figures\vfill}}
\sos@reset@figures
\newcommand{\sos@shipout@figures}
  {%
    \begingroup
      \stepcounter{page}%
      \stepcounter{sos@pages}%
      \let\protect\relax
      \setbox\z@\vbox to\vsize{\sos@figures}%
      \let\protect\string
      \AtBeginShipoutOriginalShipout\vbox
        {\vbox to\sos@top{}\moveright\sos@right\box\z@}%
    \endgroup
  }
\AtBeginShipout{%
  \ifSOS\ifodd\c@page
    \begingroup
      \let\protect\string
      \AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox
      \global\AtBegShi@Discardedtrue
      \sos@shipout@figures
    \endgroup
  \fi\fi
}%
\newcommand{\SOSshipout}{\clearpage\sos@shipout@figures}
\renewcommand{\SOStrue}{\clearpage\global\let\ifSOS\iftrue}
\renewcommand{\SOSfalse}{\clearpage\global\let\ifSOS\iffalse}
\setlength{\sos@top}{2cm}
\setlength{\sos@right}{2cm}
\makeatother

% Test example
\usepackage{booktabs,caption}
\usepackage{lipsum}
\usepackage{hyperref}
\begin{document}

\title{Hello world}
\author{Bruno Le Floch}
\maketitle

\tableofcontents
\listoffigures
\listoftables

\part{Abc}

\SOStrue
\chapter{Hello}

\addfig{\begin{tabular}{p{5cm}p{5cm}}
    \toprule
    Abc def & ghijk lmno pq \\
    \midrule
    \lipsum[1] & \lipsum[2] \\
    \bottomrule
  \end{tabular}
  \captionof{table}{\label{tab:atable}A table}}

\addfig{%
  \rule{8cm}{3cm}%
  \captionof{figure}{A figure}}

\lipsum[1-10]

\addfig{\rule{1cm}{3cm}\captionof{figure}{Another figure}}
\addfig{\rule{8cm}{3cm}\captionof{figure}{A figure}}
\addfig{\rule{1cm}{3cm}\captionof{figure}{Another figure}}

\chapter{Bye}

\makeatletter
\renewcommand{\lips@par}{ } % now \lipsum[1-10] makes one big par
\makeatother

\addfig{\rule{8cm}{3cm}\captionof{figure}{That should be figure 5.}}
\addfig{\rule{1cm}{3cm}\captionof{figure}{Perhaps the sixth}}

\lipsum[1-10]

\addfig{\rule{8cm}{3cm}\captionof{figure}{Yet another one}}
\addfig{\rule{1cm}{3cm}\captionof{figure}{One last figure for now.}}

\SOSfalse

\chapter{Back to normal}

\addfig{\rule{8cm}{3cm}\captionof{figure}{That figure won't be lost.}}

\lipsum[11-15]

\addfig{\rule{4cm}{5cm}\captionof{figure}{Nor will that one.}}

\lipsum[16-20]

\lipsum[21-30]

See Table~\ref{tab:atable}.

\SOSshipout
\SOStrue

\chapter{Figures, again}

\addfig{\rule{5cm}{2cm}\captionof{table}{Let's pretend it's a table}}

\lipsum[21-25]

\addfig{\rule{5cm}{2cm}\captionof{table}{Let's pretend it's a table}}

\lipsum[26-30]

\addfig{\rule{4cm}{1cm}\captionof{table}{Last table}}

\end{document}

EDIT: the other advantage of leaving \c@page alone is that hyperref and references work. (Added to the example.)

Moriambar
  • 11,466
  • I've juste tried it. It seems to be quite good and solve nicely the problem of very long paragraph. The switch on/off feature is quite interesting too. I have two comments however. First one is that the \SOStrue seems to be working, not on the next page, but on the one after. Not a huge problem, but it requires to plan more carefully the switches. The second one is that I would like to know where to tweak things if I want the numbering being only on the right hand pages, with odd and even numbers, and the "invisible numbering" of the left hand pages being the same as the facing page. – Martigan Dec 19 '11 at 09:33
  • @Martigan: Tweaking things is difficult. Where do you want figures corresponding to a page to appear? On the left page facing them (hence before them) or on the left page following them? The first option is tricky: what should we do for the first page of a chapter, when normally the facing left page is part of the previous chapter (e.g., headers are from the previous chapter). – Bruno Le Floch Dec 19 '11 at 10:46
  • I would like the figure to appear facing the page it corresponds to. The problem of header is a moot point, since I would like to keep pages with no headers and footers (nor numbering) : \thispagestyle{empty} pour les pages avec figures. – Martigan Dec 19 '11 at 10:58
  • @AhmedMusa good idea. Martigan, I'll look into it in a couple hours/days. Don't have time now. – Bruno Le Floch Dec 19 '11 at 17:55
  • @Bruno Le Floch: What of \newcommand*\lipsumnopar[2][1]{{\def\lips@par{ }\lipsum[#1-#2]}}? Or is your redefinition of \lips@par not global? When \ifSOS is false, pending floats have to be flushed with \SOSshipout. Is this the case? – Ahmed Musa Dec 19 '11 at 19:39
  • I have tested the output and I do not see the problem with the box centering you mentioned. For me the best length for \sos@right and \sos@top is 0cm. – Martigan Dec 20 '11 at 10:39
  • @AhmedMusa Yes, \SOSshipout outputs the floats. Not sure what the best behaviour is, though. Martigan, that's somewhat odd, are you using the book class? – Bruno Le Floch Dec 20 '11 at 12:42
  • Not exactly, I am using the thloria class, which is book-derived. I don't think it makes a huge difference. However, I have to say that I am currently using the oneside option, and that my left and right margins are the same. – Martigan Dec 21 '11 at 10:13
  • @Martigan: The reason I had to shift the page is that left- and right- layouts are distincts, but here all pages are done witht the same (right-) layout. – Bruno Le Floch Dec 21 '11 at 20:54
  • I tweaked things to suit my needs. I suppressed the \renewcommand{\thepage}{\the\numexpr\value{page}-\value{sos@pages}\relax}, and the newcounter/stepcounter associated with it. I created a \newcounter{imp}, and a \newcommand{\testimp}{\the\numexpr\value{page}+\value{imp}\relax} I then did a \stepcounter{imp} during the \sos@shipout@figures and I replaced the \ifSOS\ifodd\c@pageby \ifSOS\ifodd\testimp: Continuous numbering on right pages. Same page number on facing left page : addtocounter{page}{+/-1} beginning of AtBeginShipOut and end of \sos@shipout@figures respectively. – Martigan Jan 01 '12 at 22:08
  • @Martigan I don't understand what your changes improve. The redefinition of \thepage is necessary with two-sided layouts (hence not for you), because the real value of the page counter must always be odd to get the odd layout, hence continuous numbering requires tricking LaTeX. I still need to get back to this answer to get figures on the correct side: simply moving \sos@shipout@figures earlier than the page's shipout will not be enough because of blank pages before chapter titles. Might be enough for your case, though. – Bruno Le Floch Jan 01 '12 at 23:49
  • This is why it is a tweak for my needs only (oneside option) and it is only a comment. However, I really need to have the facing (left - picture) pages with the same page number (not displayed but in the list of figures) as the right page (text). With the imp and testimp counters, I can test the "oddness" (text or figure alternatively) and still have continuous numbering on the text (right) pages (odd and even numbers, and not only odd OR even ones). I agree this is only for my needs, not suitable for the general case, and rather crude. However, if someone has the same need as me... – Martigan Jan 02 '12 at 08:22
  • @Martigan: let me think about this "facing" issue. First, we should clarify what exactly you need. (1) Do you want to have only-right-text-pages throughout your book or not? (2) If not, what should happen if a normal chapter ends on a left page (normally the next title would immediately follow, but here there may or may not be figures)? (3) Will there ever be too many figures on one page for the facing page? (4) If so what should be done? – Bruno Le Floch Jan 02 '12 at 10:11
  • (1) Exact: only-right-text pages. (2) Not a problem. (3) The number of figures is strictly monitored and controlled - no problem here. (4) Not a problem. – Martigan Jan 02 '12 at 10:26
  • @Martigan: Could you make your final code available somewhere, e.g., on pastebin; I would be interested in using it (in combination with the book class). Relevant preamble specifics would also be useful. – equaeghe Aug 27 '13 at 21:12