1

I came across a document on the Internet and found the headers used in this document are very appealing to me. How to make a header with the format: <chpater-name> | <page-number> and similar colors/fonts?

2 Answers2

1

This is from the linked document you provided:

Linked picture

This is what the code I am providing produces:

enter image description here

The code:

\documentclass{book}

\usepackage{xcolor} \usepackage{fancyhdr} \usepackage{lipsum} \usepackage[letterspace=200]{microtype} % https://tex.stackexchange.com/a/62351/245306

% https://tex.stackexchange.com/a/400110/245306 \newcommand\mybar{\kern1pt\rule[-\dp\strutbox]{.8pt}{\baselineskip}\kern1pt}

\pagestyle{fancy} \fancyhead{} \fancyfoot{} \renewcommand{\chaptermark}[1]{\markboth{#1}{}} % Changed this to include chapter name only

\fancyhead[R]{{\color{gray} \sffamily \textls{\MakeUppercase\leftmark}} \textbf{\mybar\ \thepage}}

\renewcommand{\headrulewidth}{0pt} % Removes head rule

\begin{document} \mainmatter \chapter{Introduction} \lipsum[1-10] \chapter{Methodology} \lipsum[1-10] \end{document}

The code is quite ugly but does as you require, the referenced TEX.SE answers for microtype and the custom command \mybar were adopted herein and if you want to customise them further, they are explained in more detail in the answers. Hope this helps, if you need anything further then comment and I'll try to explain more.

Edit: Deleted then edited the code and image as I was using \chaptername by mistake and not \leftmark after customising it.

  • Would it possible to do it using the package scrlayer-scrpage instead of fancyhdr? I am using the document class scrbook and LaTeX warned me that it is not recommended to use fancyhdr and scrbook together. – livemyaerodream May 06 '22 at 07:12
  • Personally, I'd recommend that you open a new question and link to this question. Put the code I put here in your question with the image of how it looks and ask how to implement this header in the scrbook class as I have no experience with it! –  May 06 '22 at 10:04
1

Here is a suggestion for a KOMA-Script class using package scrlayer-scrpage:

\documentclass[headings=optiontoheadandtoc]{scrbook}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage[letterspace=200]{microtype}% https://www.ctan.org/pkg/microtype

\usepackage[automark,markcase=upper]{scrlayer-scrpage}% \clearpairofpagestyles \lehead{\pagemark\mypagenumberbar\leftmark} \rohead{\leftmark\mypagenumberbar\pagemark}

\renewcommand{\chaptermarkformat}{}% no chapter number in page header \newcommand\mypagenumberbar{{\usekomafont{pagenumber}{\enskip\rule[-\dp\strutbox]{1pt}{\baselineskip}\enskip}}} \setkomafont{pageheadfoot}{\color{gray}\sffamily\textls}% font settings for page header and footer \setkomafont{pagenumber}{\normalcolor}% normal color for page numbers

\begin{document} \frontmatter \tableofcontents \mainmatter \chapter[head=General]{General Suggestions} \lipsum[1-10] \chapter[head=Delivery]{Delivery of the Thesis} \lipsum[1-10] \chapter{Cover Sheet} \lipsum[1-10] \end{document}

enter image description here

Additional remarks:

Class option headings=optiontoheadandtoc activates the extended interpretation of the optional argument of sectioning commands. Eg. \chapter[head=General]{General Suggestions} puts »General« in the running head, but »General Suggestions« in ToC.

The starred versions of \lehead and \rohead uses the mandantory argument for both page styles scrheadings and plain (alias of plain.scrheadings). Therefore you get the header on chapter pages, too.


It is also possible to use package scrlayer-scrpage together with a standard class:

\documentclass{book}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage[letterspace=200]{microtype}% https://www.ctan.org/pkg/microtype

\usepackage[markcase=upper]{scrlayer-scrpage}% \clearpairofpagestyles \lehead{\pagemark\mypagenumberbar\leftmark} \rohead{\leftmark\mypagenumberbar\pagemark}

\renewcommand{\chaptermark}[1]{\markboth{\MakeMarkcase{#1}}{}}% no chapter number in page header \newcommand\mypagenumberbar{{\usekomafont{pagenumber}{\enskip\rule[-\dp\strutbox]{1pt}{\baselineskip}\enskip}}} \setkomafont{pageheadfoot}{\color{gray}\sffamily\textls}% font settings for page header and footer \setkomafont{pagenumber}{\normalcolor}% normal color for page numbers

\begin{document} \frontmatter \tableofcontents \mainmatter \chapter{General Suggestions}\chaptermark{General} \lipsum[1-10] \chapter{Delivery of the Thesis}\chaptermark{Delivery} \lipsum[1-10] \chapter{Cover Sheet} \lipsum[1-10] \end{document}

enter image description here

esdd
  • 85,675
  • Is it possible to have the vertical bar always at the same place? I use your solution, but if there is a "wider" number like for example "100" is wider than "1", the vertical bar moves to the left or the right next to the the pagenumber – Bolle Feb 27 '23 at 08:52
  • @Bolle Yes, it is possible: put \pagemark in a box of a fixed width. Therefore you could replace \pagemark in \lehead* and \rohead* by \makebox[\widthof{999}][r]{\pagemark}. \widthof{999} needs package calc and defines the width of the box. r declares the alignment of the page number in the box. r can be replaced by l or the default c. – esdd Mar 15 '23 at 08:32