I would like to create a fancy header that looks like this. I am writing a two-sided book with a5paper.
[Tex/LaTex] How to create a header that has a colored box with page number, Chaptername, and also a line across the textwidth after it
colorheader-footerrules
Related Solutions
\fancypagestyle{plain}{%
\fancyhf{}
\fancyhead[L]{\rule[-2ex]{0pt}{2ex}\small Thesis Title}
\fancyhead[R]{\small My Name}
\fancyfoot[L]{\small Project Name}
\fancyfoot[C]{-- \thepage\ --}
\fancyfoot[R]{\small Submission Date}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}}
\pagestyle{plain}
The "modern" notation for fancyhdr
is preferable. Note that \small
is not a command taking an argument, but rather a declaration.
Adjust the dimension of the strut, that is, the invisible \rule[-2ex]{0pt}{2ex}
to suit you.
A strut is an invisible object that, however, contributes to the height (what's above the baseline) or the depth (what's below) of the line in which it is inserted. A rule of width zero is the ideal object, as we can control precisely its size. The \rule
command has two mandatory arguments (width and height). With the optional argument we can dictate that it has also a depth. With this trick, the headline has a depth of 2ex (or what we prefer), so TeX will move down the headrule in order not to overlap the headline. It might turn out to be necessary to increase the space reserved for the headline; in this case fancyhdr
will issue a warning that can be found in the .log
file.
Note that LaTeX has a \strut
command, which inserts an invisible rule roughly as high and deep as a parenthesis (in this application it's insufficient).
1. Redefining scrheadings
The redefinition of scrheading
is described in chapter 5 in the KOMAscript-manual.
Add the following three lines to your MWE:
\clearscrheadfoot
\ohead{\rightmark}
\cfoot[\pagemark]{\pagemark}
You manipulate chapter
or section
in the header by changing between \leftmark
and \rightmark
in the second line, i.e.
\ohead{\rightmark} % section-names in the header
\ohead{\leftmark} % chapter-names in the header
If you use the option twoside
to the class, you can use the command
\ohead{\headmark}
and have chapter
names in the left-side heading and section names in the right-side headings, automagically.
Page number x of y
To have page number as ‘6 of 10’, add the package lastpage and redefine the cfoot
to:
\cfoot[\pagemark]{\pagemark{} of \pageref{LastPage}}
NB! If you want the same page numbering in the part
- and chapter
-sides, add the same commands to the optional argument of \cfoot
, i.e.
\cfoot[\pagemark{} of \pageref{LastPage}]% for pagestyle `scrplain`
{\pagemark{} of \pageref{LastPage}}% for pagestyle `scrheading`
3. Section number in heading
To remove the section (or chapter-) number from the heading, in KOMAscript
you use the commands:
\renewcommand*{\sectionmarkformat}{}
and
\renewcommand*{\chaptermarkformat}{}
Unfortunately, this does not work in classicthesis
(the spaced smallcaps disappear). Instead, you have to use
\renewcommand{\sectionmark[1]{\markright{\spacedlowsmallcaps{#1}}}
4. Complete MWE
Your MWE with all your requirements implemented:
\documentclass[dottedtoc, headinclude, footinclude=true]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[pdfspacing]{classicthesis}
\usepackage{lastpage,lipsum,kantlipsum} % Generates dummy text.
\pagestyle{scrheadings} % <--- Affects the page header
% and page number in the footer.
\clearscrheadfoot
\ohead{\rightmark} % comment this line and uncomment the next
% to switch to `chapter name` in the heading
%\ohead{\leftmark} % comment out to
\cfoot[\pagemark]{\pagemark}
\cfoot[\pagemark{} of \pageref{LastPage}]% for pagestyle `scrplain`
{\pagemark{} of \pageref{LastPage}}% for pagestyle `scrheading`
\renewcommand{\sectionmark}[1]{\markright{\spacedlowsmallcaps{#1}}}
% Remove section number from heading
\begin{document}
\tableofcontents
\part{Liechtenstein}
\chapter{Liechtenstein}\kant[1-6]\clearpage
\part{Germany}
\chapter{Germany}\kant[1-3]\clearpage
\section{Bavaria}\kant[1-6]\clearpage
\section{Hesse}\kant[1-6]
\end{document}
Best Answer
scrlayer-scrpage
Here is a suggestion using
scrlayer-scrpage
.If the chapter page should not get the header remove the stars after
\lehead
and\rohead
.fancyhdr
And here is an example using fancyhdr
If the chapter page should get the same header you have to redefine the fancypagestyle
plain
, too.