10

I would like to get the two different filled framed box in the odd and even side running head. Expected are: enter image description here

enter image description here

my MWE is:

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{tikz,lipsum}
\pagestyle{fancy}    
\begin{document}
\chapter{Chapter Title here}
\lipsum[1]
\section{A Level Head}
\lipsum[2-3]
\lipsum[4-12]
\end{document}  

How do get the two different frame box?

2
  • You want those boxes extending to paper width?
    – user11232
    Nov 23, 2014 at 7:18
  • @Harish: I want those boxes extend to text area
    – Balaji
    Nov 23, 2014 at 7:19

3 Answers 3

5

There are different ways of doing it (like using background, eso-pic packages) but you asked fancyhdr.

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{tikz,lipsum}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE]{\hspace*{-0.2\headwidth}\colorbox{green!20}{\makebox[\dimexpr0.2\headwidth-2\fboxsep][c]{\strut\thepage}}%
               \colorbox{green}{\makebox[\dimexpr\headwidth-2\fboxsep][l]{\strut\rightmark}}}
\fancyhead[LO]{\colorbox{green}{\makebox[\dimexpr\headwidth-2\fboxsep][r]{\strut\rightmark}}%
                \colorbox{green!20}{\makebox[\dimexpr0.2\headwidth-2\fboxsep][c]{\strut\thepage}}}
\renewcommand{\headrulewidth}{0pt}
\begin{document}
\chapter{Chapter Title here}
\lipsum[1]
\section{A Level Head}
\lipsum[2-3]
\lipsum[4-12]
\end{document}

enter image description here

4
  • How to set \thepage should be in marginplace and chaptermarks are in end of text position.
    – Balaji
    Nov 23, 2014 at 8:07
  • @Balaji Like that?
    – user11232
    Nov 23, 2014 at 8:26
  • If i have increase the \thepage fontsize the box is increase only \thepage. I want to increase both the boxes are same width. How do adjust that.
    – Balaji
    Nov 23, 2014 at 10:54
  • @Balaji: With this minimal setup, you have to do it manually. Add \vphantom{\Large\thepage} (assuming that \thepage is \Large) in the second box.
    – user11232
    Nov 23, 2014 at 12:42
7

Not very different from Harish's solution, but more modular.

\documentclass{book}
\usepackage{xcolor}
\usepackage{fancyhdr}
\usepackage{lipsum}

\pagestyle{fancy}

\renewcommand{\headrulewidth}{0pt}
\setlength{\headheight}{16.5pt}% fancyhdr tells you the length
\fancyhf{}
\fancyhead[LE]{%
  \small
  \makebox[0pt][r]{\balajipage{1cm}}%
  \balajihead{\quad\nouppercase{\leftmark}\hfill}%
}
\fancyhead[LO]{%
  \small
  \balajihead{\hfill\nouppercase{\rightmark}\quad}%
  \makebox[0pt][l]{\balajipage{1cm}}%
}

\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhf{}%
  \fancyfoot[L]{\balajipage{\textwidth}}%
}


\definecolor{balajipage}{rgb}{0.90,0.93,0.53}
\definecolor{balajihead}{rgb}{0.83,0.87,0.10}

\newcommand{\balajipage}[1]{%
  \begingroup\fboxsep=0pt
  \colorbox{balajipage}{\balajistrut
    \makebox[#1][c]{\thepage}%
  }%
  \endgroup
}
\newcommand{\balajihead}[1]{%
  \begingroup\fboxsep=0pt
  \colorbox{balajihead}{\balajistrut
    \makebox[\textwidth][s]{#1}%
  }%
  \endgroup
}
\newcommand{\balajistrut}{%
  \vrule width 0pt 
    height 1.5\ht\strutbox 
    depth 1.5\dp\strutbox
}

\begin{document}
\chapter{Chapter Title here}
\lipsum[1]
\section{A Level Head}
\lipsum[2-3]
\lipsum[4-12]
\end{document}

enter image description here

4
  • How do increase box in outer margin side like \thepage same size in outer margin.
    – Balaji
    Nov 24, 2014 at 4:37
  • @Balaji I can't understand what you mean.
    – egreg
    Nov 24, 2014 at 7:38
  • I have updated the image (odd side image - page no. 7). Please check the rule should go on upto paperwidth.
    – Balaji
    Nov 24, 2014 at 9:22
  • Use a wider length instead of \balajipage{1cm}, say \balajipage{3cm}
    – egreg
    Nov 24, 2014 at 9:43
4

This uses tcolorbox boxes and the \pagemarkwidth length to change the widths of the boxes.

The tcolorbox approach requires more setup, but is quite configurable too...

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{tcolorbox}%
\usepackage{tikz,lipsum}
\pagestyle{fancy}    
\usepackage{calc}%
\newlength{\pagemarkwidth}
\setlength{\pagemarkwidth}{20pt}
\renewcommand{\headrulewidth}{0pt}
\fancyhf{}
\fancyhead[RE]{%
\begin{tcolorbox}[colback=green,arc=0mm,auto outer arc,left=0mm,right=0mm,boxrule=0mm,top=0mm,bottom=0mm,width={\headwidth-\pagemarkwidth}]
\raggedright\rightmark
\end{tcolorbox}
}
\fancyhead[LE]{%
\begin{tcolorbox}[colback=yellow,arc=0mm,auto outer arc,left=0mm,right=0mm,boxrule=0mm,top=0mm,bottom=0mm,width={\pagemarkwidth}]
\centering\bfseries\thepage
\end{tcolorbox}
}

\fancyhead[LO]{%
\begin{tcolorbox}[colback=green,arc=0mm,auto outer arc,left=0mm,right=0mm,boxrule=0mm,top=0mm,bottom=0mm,width={\headwidth-\pagemarkwidth}]
\raggedleft\leftmark
\end{tcolorbox}
}
\fancyhead[RO]{%
\begin{tcolorbox}[colback=yellow,arc=0mm,auto outer arc,left=0mm,right=0mm,boxrule=0mm,top=0mm,bottom=0mm,width={\pagemarkwidth}]
\centering\bfseries\thepage
\end{tcolorbox}
}

\begin{document}

\chapter{Chapter Title here}
\lipsum[1]
\section{A Level Head}
\lipsum[2-3]
\lipsum[4-12]
\end{document}  

enter image description here

1
  • Well, since the OP changed the question to something not prevalent in the original version (page numbers at the marginpar), my solution does not answer the question in this range. This is annoying :-(
    – user31729
    Nov 25, 2014 at 5:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .