I am writing a document where each paragraph is categorized. I'm encoding the category as a simple numeric variable: 1, 2, 3, &c. On each page I want to know and possibly display the highest category value of any content on the page. I'm using a simple counter and wrapping each paragraph in a macro that updates the counter value if the paragraph category is higher than anything encountered so far. I'm resetting the counter every page using either everypage or everyshi. So far it almost works.
Also, I really, really don't want paragraphs to be broken across pages. When I set
\widowpenalties 1 10000
to try to enforce this, the category counter behaves in the strangest way. This strange behavior is my trouble.
The page category counter always resets two paragraphs too late into the next page. I'm completely mystified. Can anyone please shed light on this?
I'll happily accept suggestions of other ways to accomplish what I want, too.
Here is a not-very-minimal working example. It's 9 pages long. There are four pages at the beginning that show the behavior without setting widowpenalties. Then there's a separation page before four more identical pages with widowpenalties turned up to 10,000.
When you run the code, each paragraph is marked with two like 2-1 The first number is the highest category yet encountered on that page and the second number is that paragraph's category. To see the odd behavior, notice that n page 2, you'll see the first two paragraphs marked 1-1 but on page 7 you'll see the exact same two paragraphs marked 3-1.
\documentclass{article}
\usepackage{xifthen}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{everypage}
\newcommand\settext[3]{\csdef{#1#2}{#3}}
\newcommand\gettext[2]{\csuse{#1#2}}
\newcounter{pageCategoryCntr}
\setcounter{pageCategoryCntr}{1}
\AddEverypageHook{\setcounter{pageCategoryCntr}{1}}
%\zmakeperpage{pageCategoryCntr}
%\EveryShipout{\setcounter{pageCategoryCntr}{1}}
\newcommand{\category}[2]{
\ifthenelse{#1 > \value{pageCategoryCntr}}{
\setcounter{pageCategoryCntr}{#1}
}{}
\thepageCategoryCntr\ - #1 -
#2
}
\begin{document}
\category{1}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{1}{\lipsum[2]}
\category{3}{\lipsum[3]}
\category{1}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{1}{\lipsum[4]}
\category{1}{\lipsum[5]}
\category{1}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{1}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{3}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{1}{\lipsum[4]}
\category{1}{\lipsum[5]}
\newpage
\widowpenalties 1 10000
\clubpenalties 1 10000
\raggedbottom
Now another copy of the same but we set the widowpenalties and
clubpenalties to avoid breaking paragraphs across pages.
\newpage
\category{1}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{1}{\lipsum[2]}
\category{3}{\lipsum[3]}
\category{1}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{1}{\lipsum[4]}
\category{1}{\lipsum[5]}
\category{1}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{1}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{3}{\lipsum[2]}
\category{1}{\lipsum[3]}
\category{1}{\lipsum[4]}
\category{1}{\lipsum[5]}
\end{document}