1

I have a question regarding the following code:

    \documentclass[a4paper]{book}
    \setlength\headheight{0cm}

    \usepackage{xcolor}
    \colorlet{headbgcolor}{green!50!blue!70}
    \setlength{\topmargin}{-1.5cm}
    \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

     \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}}}}%
     }
     }
     \usepackage{mathptmx} 
     \begin{document}
     \blinddocument
     \end{document}

The purpose of this code is to give a filled horizontal rectangle in the header of each page, which contains the page number (the side of the page the rectangle is on changes depending on whether the page is odd or even). I was wondering if anyone could suggest a slight amendment to the code, so that, instead of inside a rectangle, the page number sits inside a pentagon, as shown in the pictureenter image description here

1

A partial solution with a TikZ signal shape follows. You'll have to adjust signal length to reach page border.

    \documentclass[a4paper]{book}
    \usepackage{tikz}
    \usetikzlibrary{shapes.symbols}

    \setlength\headheight{0cm}

    \usepackage{xcolor}
    \colorlet{headbgcolor}{green!50!blue!70}
    \setlength{\topmargin}{-1.5cm}
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \renewcommand\chaptermark[1]{\markboth{\chaptername\ \thechapter:\enskip#1}{}}
    \fancyhf{}

    \fancyhead[LE]{%
    \makebox[0pt][r]{\tikz[baseline] \node[anchor=base, signal, font=\bfseries, text=white, signal to=east, fill=headbgcolor, text width=\marginparwidth, align=right]{\thepage};
\hspace*{1em}}%
    {\itshape\leftmark}\hspace*{1em}\headrulefill%
    }

    \fancyhead[RO]{%
    \mbox{}\headrulefill \hspace*{1em}{\itshape\leftmark}%
    \makebox[0pt][l]{%
    \hspace*{1em}\tikz[baseline] \node[anchor=base, signal, font=\bfseries, text=white, signal to=west, fill=headbgcolor, text width=\marginparwidth, align=left]{\thepage};}%
     }
     \renewcommand\headrulewidth{0pt}

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

     \usepackage{blindtext}% dummy text

     \fancypagestyle{plain}{
     \fancyhf{}
     \fancyhead[LE]{%
     \makebox[0pt][r]{
     \tikz[baseline] \node[anchor=base, signal, font=\bfseries, text=white, signal to=east, fill=headbgcolor, text width=\marginparwidth, align=right]{\thepage};\hspace*{1em}}%
{\itshape\leftmark}\hspace*{1em}\headrulefill%
     }
     \fancyhead[RO]{%
     \mbox{}\headrulefill \hspace*{1em}{\itshape\leftmark}%
     \makebox[0pt][l]{%
     \hspace*{1em}\tikz[baseline] \node[anchor=base, signal, font=\bfseries, text=white, signal to=west, fill=headbgcolor, text width=\marginparwidth, align=left]{\thepage};}%
     }
     }
     \usepackage{mathptmx} 
     \begin{document}
     \blinddocument
     \end{document}

enter image description here

  • How does one change the signal length? – wrb98 Nov 22 '16 at 10:35
  • @wrb98 In this case you can change the value of text width – Ignasi Nov 22 '16 at 10:45
  • Thank you that works fine now. I've also wondered for a while how to change the font of the number in the green pentagon to (ideally) the \sffamily part of the font from the fourier package, whilst keeping the mathptmx fonts for the rest of the document. – wrb98 Nov 22 '16 at 11:22
  • Scratch that. I've figured out a neat way of doing it, but thanks all the same. – wrb98 Nov 22 '16 at 15:53

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.