# How to add “cont.” section title to the top of pages?

I have a document that contains few small sections. What I would like is if the section name was repeated at the top of page body (not in the header), if new section doesn't start on that page. And the repeated name should include something like “(cont.)”. So, for example:

Page 1:
----
Section 1

Some text …
----

Page 2:
----
Section 1 (cont.)

More text …

Section 2

Even more text …
----

Page 3:
----
Section 3

The end.
----


How can I do this in LaTeX?

-
This can be done by putting things in the page headers; not the actual text block. Perhaps this might be helpful: How to change header to list first section on page and not last section –  Werner Jan 28 '12 at 21:44
@Werner, but how can I turn that off for pages that start with a section title? –  svick Jan 28 '12 at 21:49
is the continuation line by its own in the body or part of running text? –  Frank Mittelbach Jan 30 '12 at 9:16
@FrankMittelbach, it should be on a line of its own. –  svick Jan 30 '12 at 11:52

Here is a solution based on titleps, which provides working top marks (see the explanation by Frank). Each header saves globally the bottom \sectiontitle, which is compared in the next header with the top one. (Sorry for using TeX macros like def or \let):

\documentclass{article}

\usepackage{lipsum}
\usepackage[outermarks]{titleps} % let's assume top/bot

\def\presectiontitle{}

\newpagestyle{main}{
{\sectiontitle
\ifx\sectiontitle\presectiontitle
\ (cont.)
\fi
\bottitlemarks
\global\let\presectiontitle\sectiontitle}
{\thepage}}

\pagestyle{main}

\begin{document}

\section{sec1}

\lipsum[1-5]

\section{sec2}

\lipsum[1-3]

\section{sec3}

\lipsum[1-6]

\section{sec4}

\lipsum[1-4]

\end{document}

-
Yes, this works almost the way I want it. Except it doesn't work correctly with \pagebreak. Do you know how to fix that? –  svick Jan 31 '12 at 21:57
With explicit \pagebreaks you have to emit the 'premark' by hand with \pretitlemark. See the manual for further info. –  Javier Bezos Feb 1 '12 at 8:31
Thanks, that works. –  svick Feb 1 '12 at 10:50

An automated solution is beyond my LaTeX knowledge but a starting point (at least for the manual setting) could be the following:

\documentclass[]{article}
\usepackage{blindtext}
\usepackage[automark]{scrpage2}
\clearscrplain

\begin{document}

\section{sec1}

\blindtext[12]

\section{sec2}

\blindtext[2]

\blindtext[2]

\end{document}


Automating it could be tricky since one would need to track if the text for the current is split at the pagebreak or not.

-

For an automated solution the TeX engine offers the mark mechanism (and this in fact the only way to get this right). This means that you have to put marks in your document and in the output routine when the page is being build you have to evaluate those marks and act depending on the result. TeX offers three types of marks: \topmark (the value of the last mark from the previous page) \firstmark (the first mark on the current page) and \botmark the last mark on the current page. With those three a solution is possible.

Unfortunately, LaTeX only makes use of \firstmark and \botmark in its running header support. This means that one has to augment the LaTeX commands to also support \topmark.

The basic idea is this: If I want to know the truth about the top of the page I need a sequence like this representing my section code:

\mark{<section-title>}                % this is not set by standard LaTeX headings
\penalty <break before section>
\skip <skip before section if no break is taken>
<section-title>
\mark{<section-title>}


Now if a page break happens at the \penalty then the \topmark will contain the section title (because of the first \mark that was executed as the last mark on the previous page) and the \firstmark will also contain the section title because of the second \mark command after the title. So when they are identical you know that your section is at the top of the page.

However if the heading is not at the top of the page but some way down the page then both \mark commands will be on the same page and thus \topmark will have the value from the section of the previous page, i.e., will differ from \firstmark and thus you know you need to produce a "continuation" line.

That's roughly the outline of the algorithm that needs to get implemented. There is some support for this in the package extramarks, however the whole topmark mechanism doesn't quite work with LaTeX's float/marginpar mechanism (which is why LaTeX doesn't use \topmark in the first place). Any float will clobber the \topmark so the above only works for simple documents (e.g., indexes and the like -- The LaTeX Companion index for example uses this approach to repeat top-level index entries if they run across columns or pages).

If it is needed for more complex documents then you need to extend the LaTeX output routine to keep track of the topmark values and properly restore them when doing float management (not at all trivial).

So sorry, nothing directly available as a package afaik. The stuff from TLC2 is a bit too specific to be of any use.

## Update

Looks like there is a package to support this after all: titleps by Javier Bezos which emulates (but doesn't use) \topmark. So this is well worth a look. The basic approach outlined above would remain the same.

-
The recently released titleps package by Javier Bezos claims "access to top-, first- and botmarks in a single header/footer". –  lockstep Jan 30 '12 at 17:58
@lockstep if so good, but I just quickly looked at the code and I didn't saw any adjustments to the OR and those are necessary! So not sure what "compatible with floats means". Not saying that I done a careful study so I may well be wrong here. –  Frank Mittelbach Jan 30 '12 at 19:45
@Frank. What it means is \topmark is not used at all internally. Instead, the package emulates the behaviour of \topmark (and in fact adds a further mark named nexttopmark, which, I think, can be used for this particular problem, but I've had no time to test it). –  Javier Bezos Jan 31 '12 at 11:53
@Javier sounds good. So you emulate and keep track of the marks from the previous page, thanks for the info. I updated the solution accordingly. –  Frank Mittelbach Jan 31 '12 at 12:46
After some tests it seems is not feasible in a simple way, after all (interestingly «continues» is simpler than «continued»). By just comparing the values of the marks this cannot be decided, but I'm slightly sick and I'm not inspired. –  Javier Bezos Jan 31 '12 at 14:15