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?
- 325
2 Answers
This is from the linked document you provided:
This is what the code I am providing produces:
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.
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}
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}
- 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
\pagemarkin a box of a fixed width. Therefore you could replace\pagemarkin\lehead*and\rohead*by\makebox[\widthof{999}][r]{\pagemark}.\widthof{999}needs packagecalcand defines the width of the box.rdeclares the alignment of the page number in the box.rcan be replaced bylor the defaultc. – esdd Mar 15 '23 at 08:32




scrlayer-scrpageinstead offancyhdr? I am using the document classscrbookand LaTeX warned me that it is not recommended to usefancyhdrandscrbooktogether. – livemyaerodream May 06 '22 at 07:12scrbookclass as I have no experience with it! – May 06 '22 at 10:04