9

Is there a way to put a reminder of the current chapter/section/subsection at the top of each page under the header? Something like the following:

page 1page 2

but less awkward/ugly/distracting. :)

Ideas

Perhaps this can be done in a "start of new page" hook. Does such a thing exist?

I tried using the afterpage package as suggested in a LaTeX Community forum thread, but that didn't work (see afterpage + list = missing \item).

Motivation

I often find myself lost in long, complicated technical reference documents. After a period of mind-numbing reading, I have no idea where in the forest I am because all I see are trees. Printing the current chapter/section/subsection names at the top of each page would help me remember the context.

4
  • @N.N.: I don't think putting this information in the header would work because headers have a fixed height (right?). The top of a page might be in a subsubsection (depth 4 in the hierarchy, so maybe 4 lines of text) or just a chapter (only 1 line of text). Apr 7, 2012 at 23:28
  • How are you reading these long technical documents? I find reading them with the pdf table of contents visible helps me remember where I am. (Of course, this will not work for printed documents.)
    – mforbes
    Apr 9, 2012 at 6:07
  • @mforbes: I use Evince, but it doesn't do a good job of automatically highlighting the closest PDF bookmark as I scroll around. Apr 9, 2012 at 21:40
  • also see tex.stackexchange.com/q/122792/6185 Jul 12, 2013 at 17:15

2 Answers 2

3

I almost have something figured out. It involves patching the sectioning commands and using \afterpage to draw the reminders at the top of the next page.

Here is the code so far:

\makeatletter%
\newcounter{cont@level}%
\setcounter{cont@level}{-1}%
\newrobustcmd{\cont@err}[1]{\PackageError{cont}{can't patch \protect#1}{}}%
\apptocmd{\@part}{%
  \setcounter{cont@level}{0}\gdef\cont@part{#2}}{}{\cont@err{\@part}}%
\apptocmd{\@chapter}{%
  \setcounter{cont@level}{1}\gdef\cont@chapter{#2}}{}{\cont@err{\@chapter}}%
\apptocmd{\@sect}{%
  \global\defcounter{cont@level}{#2 + 1}%
  \csgdef{cont@#1}{#8}}{}{\cont@err{\@sect}}%
\newrobustcmd{\cont@showonnext}{%
  \afterpage{%
    \ifnumgreater{\value{cont@level}}{-1}{%
      \hbox{%
        \scriptsize%
        \itshape%
        \setlength{\fboxsep}{0.5pt}%
        \colorbox[gray]{0.9}{%
          \begin{minipage}[b]{\textwidth-2\fboxsep}%
            \noindent%
            \strut%
            Continued from:%
            \\\thepart{} \cont@part%
            \ifnumgreater{\value{cont@level}}{0}{%
              \\\thechapter{} \cont@chapter}{}%
            \ifnumgreater{\value{cont@level}}{1}{%
              \\\thesection{} \cont@section}{}%
            \ifnumgreater{\value{cont@level}}{2}{%
              \\\thesubsection{} \cont@subsection}{}%
            \ifnumgreater{\value{cont@level}}{3}{%
              \\\thesubsubsection{} \cont@subsubsection}{}%
            \strut%
          \end{minipage}%
        }%
        \strut%
        \vspace{\baselineskip}%
      }%
    }{}%
    \cont@showonnext%
  }%
}%
\cont@showonnext%
\makeatother%

The code is quite rough (I'm no TeXnician), but it mostly does what I want. It puts a gray box at the top of each page like the example in the question. Here are some of its problems:

  • It creates an extra page at the end of the document with a continued block. This should be easy to fix.
  • There are problems if a part starts at the top of a new page:
    • It doesn't know that the previous part ended. I'm not sure how to detect the end of a part. See my other question Macro that knows it is at the end of a section?
    • The text is wrong. I'm guessing this is because the command for the new part (e.g., \section) is executed before the page break decision is made. I think I'll have to use marks to fix this.
    • Sometimes it draws the reminder text under the section title (it's as if the original page break was changed in the output routine)
  • It doesn't work with the starred versions of \section, etc.
  • It was written with report in mind; it might not work right for other classes
  • The output is ugly. The code itself is probably ugly too. :)
1

A solution is to redefine \sectionmark and \subsectionmark. This can be done as follows:

\usepackage{fancyhdr}
\pagestyle{fancy}

\renewcommand{\subsectionmark}[1]{\markright{#1}}
\renewcommand{\sectionmark}[1]{\markboth{#1}{}}

This will produce the same look as in the pictures, but now without the section/subsection numbers and in ordinary letters instead of capital letters. If you still would want the section/subsection numbers, then a posibbility is:

\usepackage{fancyhdr}
\pagestyle{fancy}

\renewcommand{\subsectionmark}[1]{\markright{\thesubsection \quad #1}}
\renewcommand{\sectionmark}[1]{\markboth{\thesection \quad #1}{}}

There is one important thing to note. The two \renewcommand MUST appear AFTER the command \pagestyle{fancy}. Besides this there is infinitely many possibilities for choosing a formating of these 'reminders' should look.

What the commands \markright and \markboth do is that, \markright sets the value of \rightmark and \markboth sets the value of both \leftmark and \rightmark. These commands \leftmark and \rightmark can thus be used to customize the headder and footer, e.g. as follows:

\lfoot{}
\rfoot{}
\cfoot{ \thepage }
\rhead{ {\scshape \leftmark} }
\lhead{ {\scshape \rightmark} }
\chead{}

The reason why you would want to set the \rightmark to be blank when setting the \leftmark as done in the solutions with \renewcommand above, is that you would not want, e.g., to list the name of the last subsection of the previous section untill you create a subsection in the new section.

If you have choosen a documentclass that supports chapters then you should do the above construction with \chaptermark replacing \sectionmark and \sectionmark replacing \subsectionmark.

1
  • I think my example images may have been misleading; I revised them to get rid of the header. I don't think modifying the headers will work---if the top of the page is in a subsubsection, I might want 4 lines of text: one for the chapter name, one for the section name, one for the subsection name, and one for the subsubsection name. Apr 8, 2012 at 0:44

Not the answer you're looking for? Browse other questions tagged .