Sie sind hier

How to separate the page number in the foot by vertical lines falling off the edges

Bild von Markus Kohm

Auf Deutsch: Wie man die Seitenzahl im Fuß durch randabfallende vertikale Striche abtrennt

By default, the KOMA-Script classes set the page number in the footer. For single-sided documents, this is done in the middle. To place a vertical line, which reaches to the bottom edge of the page, before and after the page number, it is sufficient to redefine \pagemark. To do this, a corresponding \rule statement must be added before and after the actual page number \thepage.

The first problem is how long this line should be and how far it should extend below the baseline (it should not be on the baseline). There is no predefined length for the bottom edge of the page. However, you can calculate it: \paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip) would be the height from the bottom edge of the paper to the baseline of the foot. So everything above the baseline of the foot is subtracted from the paper height. This would already give the desired shift of the line below the baseline. In addition to the length of the line, there is also the part that is to stand on the baseline. For example, \ht\strutbox would be the height of a normal line (for comparison: \normabaselineskip would be the total height, i.e. height + depth of a normal line and thus corresponds to \ht\strutbox+\dp\strutbox).

These considerations result, for example:

\documentclass{scrreprt}
\usepackage{blindtext}
\renewcommand*{\pagemark}{%
  \rule[-\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)]
  {1pt}
  {\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)+\ht\strutbox}%
  \nobreakspace
  {\usekomafont{pagenumber}{\thepage}}%
  \nobreakspace
  \rule[-\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)]
  {1pt}
  {\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)+\ht\strutbox}%
}
\begin{document}
\blinddocument
\Blindtext
\end{document}

As you can see, the line was also separated from the page number by the width of one word space with \nobreakspace. Other specifications such as \,, \enskip, \quad or a concrete distance with \hspace would also be possible here.

Instead of using the height of a line, you can also use the height of the footer. KOMA-Script has for the total height the length \footheight, which you can also set by option. From this we subtract the depth of a normal line \dp\strutbox. Besides, it is not very nice to specify the giant construct for the line twice. So we also define a command for it:

\documentclass{scrreprt}
\usepackage{blindtext}
\newcommand*{\pagenumberseparatorline}{%
  \rule[-\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)]
  {1pt}
  {\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)+\footheight-\dp\strutbox}%
}  
\renewcommand*{\pagemark}{%
  \pagenumberseparatorline\nobreakspace
  {\usekomafont{pagenumber}{\thepage}}%
  \nobreakspace\pagenumberseparatorline
}
\begin{document}
\blinddocument
\Blindtext
\end{document}

With double-sided documents, on the other hand, the page number is located on the outside of the footer. Of course we could use the same construct there. However, this does not look good immediately. It is better if the line is only on the inside of the page number, but not on the side towards the outer edge. To achieve this, you have to check in the definition of \pagemark whether you are on a left (even in LaTeX terminology) or on a right (odd in LaTeX terminology) page and then set either one or the other line (including distance):

\documentclass{scrbook}
\usepackage{blindtext}
\newcommand*{\pagenumberseparatorline}{%
  \rule[-\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)]
  {1pt}
  {\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)+\footheight-\dp\strutbox}%
}  
\renewcommand*{\pagemark}{%
  \ifodd\value{page}\pagenumberseparatorline\nobreakspace\fi
  {\usekomafont{pagenumber}{\thepage}}%
  \ifodd\value{page}\else\nobreakspace\pagenumberseparatorline\fi
}
\begin{document}
\blinddocument
\Blindtext
\end{document}

The TeX primitive \ifodd is used here to test if the value of the counter page, \value{page}, is odd. Within the text area, the value of this counter should never be used directly. For example, \Ifthispageodd should be used there instead. But in the header or footer, there is no need to worry about the text wrapping to another page. Therefore the counter is reliable here.

Under no circumstances should \thepage be used for the test at this point. This statement outputs the representation of the value of a counter. This can also be Roman numbers or letters or even strings composed of chapter number and page number for example. Specifying \arabic{page} would not be correct either. This is also a representation and not a counter value.

If you take a closer look at the result, you will probably want to move the page number to the outer margin. Usually, the number itself should then be placed below the marginal note column and the line in the space between the number and the text. For this we have to dig a little deeper into our bag of tricks. But with a \makebox of width 0 and a suitable alignment option, you can easily move material to the right or left of the current position.

\documentclass{scrbook}
\usepackage{blindtext}
\newcommand*{\pagenumberseparatorline}{%
  \rule[-\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)]
  {1pt}
  {\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)+\footheight-\dp\strutbox}%
}  
\renewcommand*{\pagemark}{%
  \ifodd\value{page}%
    \makebox[0pt][l]{%
      \makebox[\marginparsep][r]{\pagenumberseparatorline\nobreakspace}%
      {\usekomafont{pagenumber}{\thepage}}%
    }%
  \else
    \makebox[0pt][r]{%
      {\usekomafont{pagenumber}{\thepage}}%
      \makebox[\marginparsep][l]{\nobreakspace\pagenumberseparatorline}%
    }%
  \fi
}
\begin{document}
\blinddocument
\Blindtext
\end{document}

An alternative method, without testing for even or odd pages and without redefining \pagemark, is to use package scrlayer-scrpage. You can use the \lefoot and \rofoot statements to produce different output on left and right pages. You can move to the outer margin as above. However, with the naive approach:

\documentclass{scrbook}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{blindtext}
\newcommand*{\pagenumberseparatorline}{%
  \rule[-\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)]
  {1pt}
  {\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)+\footheight-\dp\strutbox}%
}  
\lefoot*{%
  \makebox[0pt][r]{%
    \pagemark
    \makebox[\marginparsep][l]{\nobreakspace\pagenumberseparatorline}%
  }%
}
\rofoot*{%
  \makebox[0pt][l]{%
    \makebox[\marginparsep][r]{\pagenumberseparatorline\nobreakspace}%
    \pagemark
  }%
}
\begin{document}
\blinddocument
\Blindtext
\end{document}

the problem arises that the page number sticks to the bottom margin from the second page on. The reason for this is the warning:

Package scrlayer-scrpage Warning: \footheight to low.
(scrlayer-scrpage)                At least 130.06195pt needed,
(scrlayer-scrpage)                but only 17.0pt found.
(scrlayer-scrpage)                I'll enlarge \footheight, for further
(scrlayer-scrpage)                processing, but you should do this yourself,
(scrlayer-scrpage)                e.g., setting typearea's option
(scrlayer-scrpage)                `footheight=130.06195pt'.
(scrlayer-scrpage)                I'll also increase \footskip on input line 22.

Because the line is very deep, scrlayer-scrpage wrongly assumes that the foot needs more space overall and enlarges it accordingly. In fact, the line should protrude downwards from the actual foot. So instead of following the advice, we better explain that the actual depth is not relevant. This is very easy to achieve with the second optional argument of a \raisebox:

\documentclass{scrbook}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{blindtext}
\newcommand*{\pagenumberseparatorline}{%
  \raisebox{0pt}[0pt][0pt]{%
    \rule[-\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)]
    {1pt}
    {\dimexpr\paperheight-(1in+\topmargin+\headheight+\headsep+\textheight+\footskip)+\footheight-\dp\strutbox}%
  }%
}  
\lefoot*{%
  \makebox[0pt][r]{%
    \pagemark
    \makebox[\marginparsep][l]{\nobreakspace\pagenumberseparatorline}%
  }%
}
\rofoot*{%
  \makebox[0pt][l]{%
    \makebox[\marginparsep][r]{\pagenumberseparatorline\nobreakspace}%
    \pagemark
  }%
}
\begin{document}
\blinddocument
\Blindtext
\end{document}

Here we see the advantage of defining the line as an independent command. So only its definition had to be supplemented by the \raisebox. Since we do not want any (further) vertical shift of the line, the first argument is 0 pt. The first optional argument specifies the resulting height. Here we should have used \dimexpr\footheight-\dp\strutbox. Strictly speaking, however, the height of the line should not matter either. So the height was also simply specified as 0 pt. For the depth we could also have specified the normal depth of a line, \dp\strutbox. But, as already mentioned, the line as a whole and especially its depth should not matter. So we also used 0 pt for the second optional argument. This means that the length of the line is 0 for TeX, so it has no influence on the determination of the height of the foot by scrlayer-scrpage. Alternatively, the automatic correction by scrlayer-scrpage could have been switched off by option. However, it is usually better to switch off the influence of individual elements instead.

By using the star forms \lefoot* and \rofoot*, it was ensured that the modified output of the page number is also used on chapter start pages. These are created with the plain page style plain.scrheadings.Without the asterisk, the modified output would only be used on the remaining pages with the page style headings.

It should be noted that in book printing, material that reaches to the edge of the page should always be placed slightly beyond the page margin. Otherwise, minimal shifts in the book printing process can result in ugly white margins. Therefore, the line should be extended at least 3-6 mm or 0.1-0.3 inch in length and shifted further down. Usually it does not bother if you are a bit more generous here.

Comments for "How to separate the page number in the foot by vertical lines falling off the edges" abonnieren