[Tex/LaTex] How to make a two page section start on an even page

double-sidedpage-breaking

Please excuse my lack of knowledge – I am completely new to LaTeX. I think I have a question regarding the height of a section… To be more specific, I am compiling a poetry book, using the verse package. Environment set up as follows:

\documentclass[12pt,a5paper,twoside]{article} 
\pdfpageheight=210mm
\pdfpagewidth=148mm
\usepackage[a5paper]{geometry}
\geometry
{
    portrait,
    top=2.3cm,
    left=2cm,
    right=1.3cm,
    bottom=2cm
} 
\usepackage{verse}

I want to know if a particular poem will break over two pages, and depending on the answer (and whether I am on an odd page currently), introduce a page break.

Can this be done in a macro of some sort at the beginning of the poem, linked to the \poemtitle ?

If the current page is even, I want to leave it, and if the current page is odd (and the poem spans two pages), I want to introduce a page break, so that two-page poems do not have a page turn between them. Is this possible somehow? I notice a similar question here which I don't think was answered yet:

Variable page break penalty for odd/even pages?

Best Answer

This solution manually performs the page breaks using \vsplit. Odd pages can only break between addblock environments. The drawback is that EVERYTHING must be written in addblock environments, otherwise it gets confused.

Box \pagebox is used to store the page so far (global). It is merged with the contents of the new block and tested. If the combined blocks are more than 2 pages, or if they are more than one page and one is on an odd page, then \pagebox is written to this page and we move to the next even page. When an even page is filled, \vsplit performs the page break and the first page is written.

Local registers \box0 and \box1 do not have to be saved globally. Their contents will be lost at \end{addblock}. Note: \stepcounter changes counters globally (in this case, the section number), so we only want to expand \BODY once.

\documentclass[12pt,a5paper,twoside]{article} 
\pdfpageheight=210mm
\pdfpagewidth=148mm
\usepackage[a5paper,showframe]{geometry}
\geometry
{
    portrait,
    top=2.3cm,
    left=2cm,
    right=1.3cm,
    bottom=2cm
} 
\usepackage{verse}
\usepackage{environ}
\usepackage{lipsum}% generic text

\newsavebox{\pagebox}
\newif\ifevenpage

\NewEnviron{addblock}{\setbox1=\vbox{\BODY\par}% only once for counters
  \setbox0=\vbox{\unvcopy\pagebox\unvcopy1}%
  \ifdim\ht0>2\textheight
    \unvbox\pagebox
    \newpage
    \ifevenpage
      \null\newpage
    \else
      \global\evenpagetrue
    \fi
    \setbox0=\box1
  \fi
  \loop\ifdim\ht0>\textheight
    \ifevenpage
      \setbox1=\vsplit0 to \textheight
      \unvbox1
      \pagebreak
      \global\evenpagefalse
      \setbox\pagebox=\copy0
    \else
      \unvbox\pagebox
      \newpage
      \global\evenpagetrue
      \setbox0=\box1
    \fi
  \repeat
  \global\setbox\pagebox=\box0
\ignorespaces}

\AtEndDocument{\unvbox\pagebox}

\begin{document}
  \begin{addblock}
    \section{first}
    \lipsum[1]
  \end{addblock}
  \begin{addblock}
    \section{second}
    \lipsum[1-4]
  \end{addblock}
  \begin{addblock}
    \section{third}
    \lipsum[1]
  \end{addblock}
  \begin{addblock}
    \section{fourth}
    \lipsum[2]
  \end{addblock}
  \begin{addblock}
    \section{fifth}
    \lipsum[1-4]
  \end{addblock}
  \begin{addblock}
    \section{sixth}
    \lipsum[2]
  \end{addblock}
\end{document}
Related Question