# Align vertical line in fancyhdr to text block

I’m having trouble defining a vertical line as part of my page header using fancyhdr.

On left pages the line does not align with the text block.

As it should and as it works fine on the right side.

What is wrong in my code? (See MWE below.) In addition I would like to change the text on the right side to the title of the chapter instead of the title of the section.

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{lipsum}

\usepackage[framemethod=TikZ]{mdframed}

\renewcommand{\chaptermark}[1]{\markboth{\chaptername\space\thechapter}{}}
\renewcommand{\sectionmark}[1]{\markright{##1}{}}
\fancyhf{}
\voffset-10mm
\begingroup\color{gray}\rule[-1pt]{1pt}{1000pt}\endgroup}

\begin{document}

\title{Simple Book Example}

\mainmatter

\chapter{The First Chapter}

\lipsum[1-10]

\end{document}


I would use \makebox[0pt] to hide the width of the page number and the rule and \smash to hide the height and the depth of the rule.

\fancyhead[LE]{%
\space\leftmark
}
\rightmark\space
}


If the text of the chapter title should be on odd pages instead the text of the section title change the redefinition of \chaptermark and \sectionmark to

\renewcommand*{\chaptermark}[1]{\markboth{\chaptername\space\thechapter}{#1}}
\renewcommand*{\sectionmark}[1]{}


Code:

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand*{\chaptermark}[1]{\markboth{\chaptername\space\thechapter}{#1}}
\renewcommand*{\sectionmark}[1]{}
\fancyhf{}

\space\leftmark
}
\rightmark\space
}
\voffset-10mm

• This works great. Thank you! One thing is missing though. How do I add the title of the chapter instead of the title of the section on right pages? Replacing \sectionmark with \chaptermark leads to the section title with the section number for some reason?! – user3347232 Nov 23 '16 at 20:21