or
Caleb
koma
In a previous life I [asked a question](https://topanswers.xyz/tex?q=340) about page break penalties and weighting them differently depending on whether they landed on odd or even pages.

The basic answer to that using KOMA-Script classes (which I happened to be using) ran something like this:

```tex
\ifthispageodd{\nopagebreak[3]}{\nopagebreak[4]}
```

This worked okay, and it is something I use in producion every day. However it has one significant draw back: **it onnly works on the second pass**. This means it sometimes lets me down pretty hard because I have one project that does _many_ passes, basically fitting arbitrary content into a fixed page count by iterating over  the intput incrementally taking predefined steps to shrink things until it all fits.

Unless I did three passes of every itteration in that process, I don't get accurate break weights.

Is there any way to expidite this and know what page I'm on in the current rendering pass (not the previous one) and set a penalty based on that?
Top Answer
Skillmon
I didn't test this, so I'm entirely unsure whether it does what it should in your context.

The idea is to measure the height of your score and test for a few things (I hope the comments are enough). One could of course test for other things (e.g. if this page is even and already started, will the score fit on the remaining space of this page plus the next page).

All you have to do is make sure that the scores are typeset inside the `myscores` environment, the environment should handle the rest.

```tex
\makeatletter
\newsavebox\mybox  
\newlength\myabsheight
% this is analogue to how `\cleardoublepage' works
\newcommand*\cleartoevenpage
  {%
    \clearpage
    \if@twoside
      \ifodd\c@page
        \hbox{}\newpage
        \if@twocolumn
          \hbox{}\newpage
        \fi
      \fi
    \fi
  }
\newenvironment{myscores}  
  {\setbox\mybox\vbox\bgroup}  
  {%  
    \egroup  
    \myabsheight=\dimexpr\ht\mybox+\dp\mybox\relax
    \ifdim\pagetotal=0pt
      % we're at the start of a page  
      \ifdim\myabsheight>\textheight
        % score will be bigger than one page, so we want to start on the next
        % even page
        \cleartoevenpage
      \fi
    \else
      \ifdim\myabsheight>\dimexpr\pagegoal-\pagetotal\relax
        % score will not fit on the remaining space of this page
        \ifdim\myabsheight>\textheight
          % score will not fit on a single page
          \cleartoevenpage
        \else
          % score will fit on a single page
          \clearpage
        \fi
      \fi
    \fi
    \unvbox\mybox
  }
\makeatother
```
Can I add a page break penalty that varries by context?
Skillmon replying to Caleb
bear in mind that the value of page counters is set asynchronously, so you can't trust neither `\c@page` nor `\c@abspage` in the midst of a page, they are only somewhat reliable after a `\clearpage`.
Skillmon
ok, I just wanted to test `\cleartoevenpage` and there seems to be an inconsistency with `abspage`, that seems to be one page behind... A bit counter-intuitiv.
Skillmon
Instead of the `\clearpage` and `\cleartoevenpage` macros you could also insert penalties there of course.
Caleb replying to Skillmon
Thanks, I'll give that a shot.
Skillmon
Quick idea (no idea how robust this is): Instead of doing this with KOMA's `\ifthispageodd` we could use `\usepackage{zref-abspage}` and then decide on the penalties using `\ifodd\value{abspage}`.