1

I would like to create a fancy header that looks like this. I am writing a two-sided book with a5paper. Header with line after chapter name

  • 3
    Welcome to TeX.SX! Is this for the page header or the chapter heading? – egreg May 6 '15 at 7:02
  • My apologies for the unclear question. It is for the page header of the book. – pyro May 6 '15 at 11:59
6

scrlayer-scrpage

Here is a suggestion using scrlayer-scrpage.

\documentclass[a5paper]{book}
\setlength\headheight{13.1pt}

\usepackage{xcolor}
\colorlet{headbgcolor}{green!50!blue!70}

\usepackage[markcase=noupper]{scrlayer-scrpage}
\clearpairofpagestyles
\automark[chapter]{chapter}
\renewcommand\chaptermarkformat{\chaptername\ \thechapter:\enskip}

\lehead*{%
  \makebox[0pt][r]{
    \colorbox{headbgcolor}{\makebox[\textwidth][r]{\pagemark\enskip}}\hspace*{1em}}%
  \headmark\hspace*{1em}\headrulefill%
}
\rohead*{%
  \headrulefill\hspace*{1em}\headmark%
  \makebox[0pt][l]{%
    \hspace*{1em}\colorbox{headbgcolor}{\makebox[\textwidth][l]{\enskip\pagemark}}}%
}

\addtokomafont{pagenumber}{\bfseries\color{white}}
\newcommand\headrulefill{\leaders\hrule width 0pt height 3pt depth -2.8pt \hfill}

\usepackage{blindtext}% dummy text
\begin{document}
\blinddocument
\end{document}

enter image description here

If the chapter page should not get the header remove the stars after \lehead and \rohead.

fancyhdr

And here is an example using fancyhdr

\documentclass[a5paper]{book}
\setlength\headheight{13.1pt}

\usepackage{xcolor}
\colorlet{headbgcolor}{green!50!blue!70}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand\chaptermark[1]{\markboth{\chaptername\ \thechapter:\enskip#1}{}}
\fancyhf{}

\fancyhead[LE]{%
  \makebox[0pt][r]{
    \colorbox{headbgcolor}{\makebox[\textwidth][r]{\bfseries\textcolor{white}{\thepage}\enskip}}\hspace*{1em}}%
  {\itshape\leftmark}\hspace*{1em}\headrulefill%
}
\fancyhead[RO]{%
  \mbox{}\headrulefill \hspace*{1em}{\itshape\leftmark}%
  \makebox[0pt][l]{%
    \hspace*{1em}\colorbox{headbgcolor}{\makebox[\textwidth][l]{\enskip\bfseries\textcolor{white}{\thepage}}}}%
}
\renewcommand\headrulewidth{0pt}

\def\headrulefill{\leaders\hrule width 0pt height 3pt depth -2.8pt \hfill}

\usepackage{blindtext}% dummy text
\begin{document}
\blinddocument
\end{document}

If the chapter page should get the same header you have to redefine the fancypagestyle plain, too.

\fancypagestyle{plain}{
  \fancyhf{}
  \fancyhead[LE]{%
    \makebox[0pt][r]{
      \colorbox{headbgcolor}{\makebox[\textwidth][r]{\bfseries\textcolor{white}{\thepage}\enskip}}\hspace*{1em}}%
    {\itshape\leftmark}\hspace*{1em}\headrulefill%
  }
  \fancyhead[RO]{%
    \mbox{}\headrulefill \hspace*{1em}{\itshape\leftmark}%
    \makebox[0pt][l]{%
      \hspace*{1em}\colorbox{headbgcolor}{\makebox[\textwidth][l]{\enskip\bfseries\textcolor{white}{\thepage}}}}%
  }
}
  • Two superb solutions! I have tried both and they gave the outputs that I needed. Since I am more familiar with fancyhdr, I will use your second option. Only one slight issue: for the fancyhdr solution, the page number's font is not consistent - even page has normal font and odd page has italized font. That could be easily fixed right? – pyro May 7 '15 at 3:02
  • Alhough I am told to avoid saying thanks, I would still like to say thanks to @esdd for posting a solution for me. This solution has opened up some huge possibilities for designing colorful and eye-capturing page headers instead of boring ones that I have seen latex do. I think more examples like these really help end-users like me work in the latex-ing world. – pyro May 7 '15 at 3:07
  • @pyro I have changed the fancyhdr example: now the page numbers should have normal font on both even and odd pages. – esdd May 7 '15 at 7:44
  • Fabulous! Now they are perfect! TQVM! – pyro May 7 '15 at 12:40

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.