Page Layout\thispagestyle and counters in the middle of long paragraphs

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

\thispagestyle and counters in the middle of long paragraphs

Post by frabjous »

I hope the following aren't stupid questions.

My first has to do with \thispagestyle command used in the middle of very long paragraphs. The command seems to take effect at the beginning of the paragraph, which may not be where you want it.

Consider this MWE. It produces a four page document, all one paragraph. The \thispagestyle occurs on page 2 of the paragraph, where the bold marker is, and the text switches to italics. But rather than affecting page 2, the empty style affects page 1, where the paragraph begins. I can't put it after the paragraph, or it would affect page 4. Is there any way to make it affect page 2?

Code: Select all

Code, edit and compile here:
\documentclass[letterpaper,oneside]{book}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\blindtext[10]
\textbf{This-Page-Style command is here!}\thispagestyle{empty}
\textit{\blindtext[10]}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Similarly, if counters are changed in the middle of the paragraph, as far as the headers/footers are concerned, the change ``takes place'' at the beginning of the paragraph. So if you have a custom header that refers to a custom counter, you can't control it properly. For this problem, consider the following:

Code: Select all

Code, edit and compile here:
\documentclass[letterpaper,oneside]{book}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{fancyhdr}
\newcounter{mycounter}
\setcounter{mycounter}{0}
\pagestyle{fancy}
\rhead{My counter: \themycounter}
\begin{document}
\blindtext[8]
\blindtext[1] \textbf{I'm adding to my counter. It will be 1.} \addtocounter{mycounter}{1}
\blindtext[3]
\blindtext[10] \textbf{I'm adding to my counter. It will be 2.} \addtocounter{mycounter}{1}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
In the output in the header, I get the counter at 0 on page 1, which is right, and then at 1 on page 2, which is fine, since I add to it on that page, but it lists it at 2 on page 3, even though the counter is not changed until page 4, at the end of the paragraph that begins on page 3.

Is there a way around these things?

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

\thispagestyle and counters in the middle of long paragraphs

Post by gmedina »

Hi,

it's very unusual to have such long paragraphs; however, for the first issue, you can use a "fictitious" header/footer (notice that due to the asynchronous way in which LaTeX builds pages, the condition for the \ifthenelse command is not exactly the expected):

Code: Select all

Code, edit and compile here:
\documentclass[letterpaper,oneside]{book}
\usepackage[english]{babel}
\usepackage{fancyhdr}
\usepackage{ifthen}
\usepackage{blindtext}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\thepage}
\fancyfoot[C]{\ifthenelse{1=\thepage}{\thispagestyle{empty}}{}}
\renewcommand\headrulewidth{0pt}
\begin{document}
\blindtext[20]
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

\thispagestyle and counters in the middle of long paragraphs

Post by frabjous »

Thanks very much for your reply.

Perhaps it would help to explain exactly what I'm trying to do, which wouldn't have been clear from my MWE. I'm trying to "republish" an old philosophy of mathematics text in a new form--in particular (yes, I know this is crazy)--in a PDF sized just right to be read on an iPhone or iPod touch. I'm using the geometry package to set the page size to the very tiny 50x75mm.

I didn't include that in my MWE since it didn't seem to be the cause of the problem, although it explains why I have paragraphs taking up multiple pages all over the place. In fact, most paragraphs take up at least two pages. To reproduce this in my MWE I used super-long paragraphs, but the paragraphs in what I'm working on are not inordinately long; the page size is just tiny.

What I'm trying to do with the headers it have to show not its pagination, but the pagination of the original. I have markers in the text where the original page breaks are, and I have them increase a counter. I want this counter to be shown in the header. But since this counter is often increased in the middle of a multi-page paragraph, it's not increasing right if I just put:
\cfoot{original page \theorigpage} for the footer.

Since posting the above, however, I've discovered that I can get part of it to work right if I use \markboth{\theorigpage}{\theorigpage} where the original page breaks are, and then use \cfoot{original page \leftmark} for the footer.

Since the original pages are much bigger, on most of my pages the original page doesn't change, so having it just read "original page XX" is fine for those pages. Ideally, however, on pages where there is a break, I'd like the footer instead to read "original pages XX-XXX". This is why I asked about the first question, since I could use a different page style to accomplish this. But as noted, it doesn't apply to the right page if the break is in the middle of a long paragraph.

If I understand your solution right, it's a matter of specifying exactly which page I want the style to apply to. This would be fine if only one page were involved, but since the original book is about 200 pages long, with a roughly 1000 page conversion, this happens approximately 200 times and so hard-coding it would be impractical, especially if I make any changes, etc.

Nevertheless, thanks for the suggestion. I'll see if I can tweak it. ifthen may provide some other way to deal with this.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

\thispagestyle and counters in the middle of long paragraphs

Post by frabjous »

I figured out a solution, at least for my situation in particular. (I can still imagine people with similar but not-quite-the-same situations where this might not work.) Still, I thought I'd share in case anyone else has a similar problem.

The key was to use \markboth both before and after I changed the counter mid-paragraph. Since \leftmark and \rightmark are treated differently (one goes on the first mark on the page, the other with the second, but if no command is given on a certain page it just inherits from the last), this allows you to compare them and treat the headers accordingly. So in particular, the command used where the original page breaks are might be defined something like this:

Code: Select all

Code, edit and compile here:
\newcommand{\origpagebreak}{\markboth{\theorigpage}{\theorigpage}%
\addtocounter{origpage}{1}%
\markboth{\theorigpage}{\theorigpage}}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
And then to set up my custom footer, I used this:

Code: Select all

Code, edit and compile here:
\newcommand{\newcommand{\mymarks}{%
\ifthenelse{\equal{\leftmark}{\rightmark}}
{page \leftmark} % if equal
{pages \rightmark--\leftmark}} % if not equal
\cfoot{\mymarks}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
There's a kinda/sorta similar example given in the fancyhdr documentation, but I have to confess I found that documentation very difficult to understand the first time through. Also, the example it uses doesn't involve using \markboth twice for the same entry, though this will be necessary sometimes.
Post Reply