2

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.

le

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

ro

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{}
\fancyhead[LE]{\begin{picture}(0,0)\put(0,0){\thepage\space\vheadline\space\leftmark}\end{picture}}
\fancyhead[RO]{\rightmark\space\begin{picture}(0,0)\put(0,0){\vheadline\space\thepage}\end{picture}}
\voffset-10mm
\renewcommand\headrulewidth{0pt}
\def\vheadline{%
    \begingroup\color{gray}\rule[-1pt]{1pt}{1000pt}\endgroup}


\begin{document}

\title{Simple Book Example}

\mainmatter

\chapter{The First Chapter}

\lipsum[1-10]

\end{document}
2

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]{%
    \makebox[0pt][r]{\thepage\space\smash{\vheadline}}%
    \space\leftmark
}
\fancyhead[RO]{%
    \rightmark\space
    \makebox[0pt][l]{\smash{\vheadline}\space\thepage}%
}

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]{}

enter image description here

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{}

\fancyhead[LE]{%
    \makebox[0pt][r]{\thepage\space\smash{\vheadline}}%
    \space\leftmark
}
\fancyhead[RO]{%
    \rightmark\space
    \makebox[0pt][l]{\smash{\vheadline}\space\thepage}%
}
\voffset-10mm
\renewcommand*\headrulewidth{0pt}
\newcommand*\vheadline{\textcolor{gray}{\rule[-1pt]{1pt}{1000pt}}}

\usepackage{lipsum}
\begin{document}
\title{Simple Book Example}
\mainmatter
\chapter{The First Chapter}
\section{A section}
\lipsum[1-10]
\end{document}
| improve this answer | |
  • 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
  • I did not read this second question. Now I have updated my answer. – esdd Nov 23 '16 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.