olsak / OpTeX

OpTeX - LuaTeX format with extended Plain TeX macros
http://petr.olsak.net/optex/
35 stars 13 forks source link

Example code for running head on page 127 of the manual #100

Closed pkvil closed 2 years ago

pkvil commented 2 years ago

On page 127 of the manual the following code is given as an example of how to implement running heads:

\addto\_chapx {\_edef\_runningchap {\_thechapnum: \_unexpanded\_ea{\_savedtitle}}}
\def \formathead #1#2{\isempty{#1}\iffalse #1: #2\fi}  
    \headline = {%  
        \ifodd \pageno
            \hfil \ea\formathead\firstmark{}{}%
        \else
            Chapter: \runningchap \hfil
        \fi
    }

I should first say that I am testing this with version 1.03, but I can't see changes that would make a difference in version 1.07. The first issue is that \_runningchap is defined, but \runningchap (without "_") is called. This is easily fixed though. The second issue is that the output shows the wrong chapter number in the header. This code:

\addto\_chapx {\_edef\_runningchap {\_thechapnum: \_unexpanded\_ea{\_savedtitle}}}
\def \formathead #1#2{\isempty{#1}\iffalse #1: #2\fi}
\headline = {%
%\ifodd \pageno % Commented because the chapters are short, to make \_runningchap show on all pages.
%\hfil \ea\formathead\firstmark{}{}%
%\else
Chapter: \_runningchap \hfil
%\fi
}

\chap First chapter
Lorem

\chap Second chapter
ipsum

\chap Third chapter
dolor

\bye

produces these headlines: Page 1: "Chapter 2: Second chapter" Page 2: "Chapter 3: Third chapter" Page 3: "Chapter 3: Third chapter"

If the text in each chapter is made longer so that it covers more than one page, the page before the next chapter gets the header incremented and shown like the following chapter.

I suspect that the issue is that \_runninghead is defined before the page break from \_printchap. Moving the definition to \_inchap after \_printchap seems to solve the issue.

The following code:

\_def\_inchap #1{\_par \_sectionlevel=1
   \_def \_savedtitle {#1}% saved to .ref file
   \_ifnonum \_else {\_globaldefs=1 \_incr\_chapnum \_chapx}\_fi
   \_edef \_therefnum {\_ifnonum \_space \_else \_thechapnum \_fi}%
   \_printchap{\_scantextokens{#1}}%
   \_edef\_runningchap {\_thechapnum: \_unexpanded\_ea{\_savedtitle}} % This line was added/moved from \addto\_chapx...
   \_resetnonumnotoc
}

%\addto\_chapx {\_edef\_runningchap {\_thechapnum: \_unexpanded\_ea{\_savedtitle}}}
\def \formathead #1#2{\isempty{#1}\iffalse #1: #2\fi}
\headline = {%
%\ifodd \pageno % Commented because the chapters are short, to make \_runningchap show on all pages..
%\hfil \ea\formathead\firstmark{}{}%
%\else
Chapter: \_runningchap \hfil
%\fi
}

\chap First chapter
Lorem

\chap Second chapter
ipsum

\chap Third chapter
dolor

\bye

Seems to fix the issue for me, producing the headlines: Page 1: "Chapter 1: First chapter" Page 2: "Chapter 2: Second chapter" Page 3: "Chapter 3: Third chapter"

Since I'm a beginner it's entirely possible that I'm just doing things wrong though, in which case I apologize for wrongfully submitting this.

olsak commented 2 years ago

You are right, my example was full of mistakes. Thank you for notification. I will correct it.

Your solution is OK, if there is no \nonum\chap used in the document. I think that \nonum\chap is very rare case so it is OK. But I corrected my example by adding \vfil\break to \_chapx because \_chapx is called only if no \nonum is used. There is new complication with it here: \_chapx is called when \globaldefs=1 and it is undesirable to call output routine in this state: following macros are broken. So, there is \globaldefs=0 before \vfil\break:

\addto\_chapx {\globaldefs=0 \vfil\break % headline of previous chapter is printed
   \xdef\_runningchap {\_thechapnum: \_unexpanded\_ea{\_savedtitle}}}
\def \formathead #1#2{\isempty{#1}\iffalse #1: #2\fi}  
\headline = {%
%\ifodd \pageno % Commented because the chapters are short, to make \_runningchap show on all pages..
%\hfil \ea\formathead\firstmark{}{}%
%\else
\ifx\_runningchap\undefined \else Chapter \_runningchap \fi \hfil
%\fi
}

I discovered new issue when testing your example: the chapter titles aren't at the same vertical position on various pages because the \prevdepth from previous line (at the previous page) is taken into account. I'll correct it by adding \prevdepth=0pt after \_vfill\_supereject in \_printchap macro.

pkvil commented 2 years ago

It seems to work fine now! Thank you.