Sie sind hier

How to separate the page number in the head with a vertical line

Bild von Markus Kohm

Auf Deutsch: Wie man die Seitenzahl im Kopf mit einem vertikalen Strich abtrennt

The KOMA-Script classes normally set the page number in the footer. In a separate article we already explained how to move the page number from the page footer to the header. If you additionally want an vertical line between the rest of the header and the page number, you also have to use a page style package like scrlayer-scrpage.

A vertical line of thickness 1 pt, which has the height and depth of a row, can be used for example:

\rule[-\dp\strutbox]{1pt}{\normalbaselineskip}

The line has the desired width (= thickness) of 1 pt. The total height is

\normalbaselineskip</code>, which is the normal baseline distance. Normally, the entire rule would be output above the baseline. However, it should extend below it by the normal depth of a line. For this reason, the optional argument is used to make a corresponding vertical shift downwards.
 
For a single-sided document, this specification can be inserted in the header before the page number, <latex>\pagemark

, is printed. Usually, you will want a certain distance between the rule and the page number, for example, \, (reduced space), \nobreakspace (unbreakable space) or \enskip (half quad spacing, that is 0.5 em).

This would be a possible solution for a single-sided document:

\documentclass{scrreprt}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{blindtext}
\clearpairofpagestyles
\ihead{\headmark}
\ohead{\rule[-\dp\strutbox]{1pt}{\normalbaselineskip}\nobreakspace\pagemark}
\begin{document}
\blinddocument
\end{document}

Here a slightly different way than in the above linked article was chosen to move the page number into the header. First, \clearpairofpagestyles was used to delete all fields in the page styles scrheadings and plain.scrheadings. Then \ihead was used to assign the column title, \headmark, to the inner header element of scrheadings. In a single-sided document, \ihead corresponds to the statement \lohead, thus setting the left odd element. Finally, \ohead was used to place the separator, spacing and page number in the outer element of scrheadings. The use of \ohead is equivalent to \rohead in single-sided documents, i.e. it sets the right odd element. Why the page number was omitted in plain.scrheadings is explained in the article linked above. If you still want to have it on the chapter start page, simply use the star form \ohead*.

With a double-sided document, things are a bit more difficult. Here, the rule on right (in LaTeX terminology odd) pages must be placed before the page number, as in single-sided documents. On left (even in LaTeX terminology) pages, however, the rule must be placed after the page number. scrlayer-scrpage offers the necessary distinction between \rohead and \lehead:

\documentclass{scrbook}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{blindtext}
\clearpairofpagestyles
\ihead{\headmark}
\rohead{\rule[-\dp\strutbox]{1pt}{\normalbaselineskip}\nobreakspace\pagemark}
\lehead{\pagemark\nobreakspace\rule[-\dp\strutbox]{1pt}{\normalbaselineskip}}
\begin{document}
\blinddocument
\end{document}

On closer examination of the example, you may wish to place the page number not within the normal header line but rather above the marginal note column. The vertical line and space should be placed within the distance between the text area and the marginal note column. There are two basic procedures for this. First, you can use option headwidth=textwithmarginpar to extend the header beyond the marginal note column and then use a box of width \marginparwidth to place the page number itself flush with the marginal note column:

\documentclass{scrbook}
\usepackage[automark,headwidth=textwithmarginpar]{scrlayer-scrpage}
\usepackage{blindtext}
\usepackage{showframe}
\clearpairofpagestyles
\ihead{\headmark}
\rohead{%
  \rule[-\dp\strutbox]{1pt}{\normalbaselineskip}\nobreakspace
  \makebox[\marginparwidth][l]{\pagemark}%
}
\lehead{%
  \makebox[\marginparwidth][r]{\pagemark}%
  \nobreakspace\rule[-\dp\strutbox]{1pt}{\normalbaselineskip}%
}
\begin{document}
\blinddocument
\end{document}

On the other hand, with unchanged head width, the complete construct could be pushed over a box of width 0 into the respective border:

\documentclass{scrbook}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{blindtext}
\usepackage{showframe}
\clearpairofpagestyles
\ihead{\headmark}
\rohead{%
  \makebox[0pt][l]{%
    \makebox[\marginparsep][r]{%
      \rule[-\dp\strutbox]{1pt}{\normalbaselineskip}\nobreakspace
    }%
    \makebox[\marginparwidth][l]{\pagemark}%
  }%
}
\lehead{%
  \makebox[0pt][r]{%
    \makebox[\marginparwidth][r]{\pagemark}%
    \makebox[\marginparsep][l]{%
      \nobreakspace\rule[-\dp\strutbox]{1pt}{\normalbaselineskip}%
    }%
  }%
}
\begin{document}
\blinddocument
\end{document}

In both examples the package showframe was inserted to illustrate the different page areas with frames.

While the two examples don't show any significant difference at first glance, such a difference becomes clear when headsepline is used to display a separator line under the head. In the first case, this now extends over the text area and the complete marginal note column. In the second case, however, it remains limited to the width of the text area and thus does not extend below the vertical rule and the page number.

In some cases, it is desired that the vertical rule be extended to the top edge of the page. The exact length would then be 1in+\topmargin+\headheight. However, if you use this value for \rule, scrlayer-scrpage will automatically increase the head height, because the long rule will also increase the effective head height. You can turn off this automatism in scrlayer-scrpage with an option. But you can also use an additional \raisebox statement to make a less high head appear:

\documentclass{scrbook}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{blindtext}
\usepackage{showframe}
\clearpairofpagestyles
\ihead{\headmark}
\rohead{%
  \makebox[0pt][l]{%
    \makebox[\marginparsep][r]{%
      \raisebox{0pt}[\ht\strutbox][\dp\strutbox]{%
        \rule[-\dp\strutbox]{1pt}{\dimexpr 1in+\topmargin+\headheight}%
      }%
      \nobreakspace
    }%
    \makebox[\marginparwidth][l]{\pagemark}%
  }%
}
\lehead{%
  \makebox[0pt][r]{%
    \makebox[\marginparwidth][r]{\pagemark}%
    \makebox[\marginparsep][l]{%
      \nobreakspace
      \raisebox{0pt}[\ht\strutbox][\dp\strutbox]{%
        \rule[-\dp\strutbox]{1pt}{\dimexpr 1in+\topmargin+\headheight}%
      }%
    }%
  }%
}
\begin{document}
\blinddocument
\end{document}

To allow calculations as argument of \rule, the TeX extension \dimexpr was used here. With the first argument of \raisebox, no vertical shift was requested with 0 pt. The optional arguments \ht\strutbox and \dp\strutbox were used to specify the height and depth of a standard line. In fact, 0 pt could have been specified here as well.

For the last example, it should be noted that material that reaches to the edge should always extend a little beyond the edge. Especially in book projects, this is the only way to ensure that no white margins are created during printing due to minimal shifts. It is therefore recommended to extend the line by three to six millimeters or simply one to three tenths of an inch.

Comments for "How to separate the page number in the head with a vertical line" abonnieren