[Tex/LaTex] Book fancyhdr confusion

fancyhdrheader-footer

I am writing my master's thesis and would like to have my headers as in this book. I would like my even pages with the page number in the left margin and the chapter aligned to the left. My odd pages should have the page number in the right margin, and the section number aligned to the right.

Somewhat opposite to the example presented in the fancyhdr manual (p. 13):

\documentclass{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{calc}
\fancyheadoffset[LE,RO]{\marginparsep+\marginparwidth}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\fancypagestyle{plain}{%
    \fancyhead{} % get rid of headers
    \renewcommand{\headrulewidth}{0pt} % and the line
}

However, I am unable to edit this code to my liking, after having fiddled with it for an hour. Can you guys and gals help a fella out?

Left page:

Left page.

Right page:

Right page.

Best Answer

The offset you're using seems too much; but that's irrelevant. Here's a way: just tell LaTeX that you want to reserve for the page number the exact amount of the offset.

Using a parameterized length ensures that changes, if any, are made in only one place, namely where you set the \pageoffset parameter.

\documentclass{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{calc}
\newlength{\pageoffset}
\setlength{\pageoffset}{2cm}% use whatever you like
\fancyheadoffset[LE,RO]{\pageoffset}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE]{\makebox[\pageoffset][l]{\bfseries\thepage}\bfseries\leftmark}
\fancyhead[RO]{\bfseries\rightmark\makebox[\pageoffset][r]{\bfseries\thepage}}
\fancypagestyle{plain}{%
    \fancyhead{} % get rid of headers
    \renewcommand{\headrulewidth}{0pt} % and the line
}

\usepackage{kantlipsum}
\begin{document}
\mainmatter
\chapter{Chapter title}
\section{Section title}
\kant[1-15]
\end{document}

Even page

enter image description here

Odd page

enter image description here