place colored box around page numbers in header/footer

1,480 views
Skip to first unread message

triniboy

unread,
Feb 13, 2011, 11:38:04 PM2/13/11
to LaTeX Users Group
Hello,

Can you help me in getting a blue colored box just around the page
numbers in the header. I am using the fancy hdr class and can't seem
to get a colored box in the header to appear behind the page number.

Thanks for all your help and time.

Sincerely,
triniboy

Peter Flynn

unread,
Feb 14, 2011, 4:22:47 AM2/14/11
to latexus...@googlegroups.com
On Mon, Feb 14, 2011 at 4:38 AM, triniboy <rbsi...@gmail.com> wrote:
Hello,

Can you help me in getting a blue colored box just around the page
numbers in the header.  I am using the fancy hdr class and can't seem
to get a colored box in the header to appear behind the page number.

Around or behind? Two different things: \usepackage{xcolor} gives you two commands:

\colorbox{green}{\color{white}\thepage} will give you a green background and white text.

\fcolorbox{green}{white}{\thepage} will give you a green frame and a white background.

Use \fboxsep to set the amount of space around the text amd \fboxrule to set the thickness of the frame.

///Peter

triniboy

unread,
Feb 14, 2011, 11:25:29 PM2/14/11
to LaTeX Users Group
Hello,

Thanks for your help. I tried your suggestion, but did not get the
correct result. I have attached some of the code that I use in the
fancy header:

\pagestyle{fancy}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}
\lhead[\fancyplain{}{}]
{\fancyplain{}{\footnotesize\bfseries\itshape{Fifth Order Filter}}}
\chead[\fancyplain{}{}]
{\fancyplain{}{\footnotesize\bfseries\itshape{}}}
\rhead[\fancyplain{}{\rightmark}] {\fancyplain{}{\bf{\thepage}}
\colorbox{green}{\color{white}\thepage}}
\cfoot[\fancyplain{}{}] {\fancyplain{}{}} \lfoot[\fancyplain{}{}]
{\fancyplain{}{\footnotesize\bfseries\itshape{$11^{th}
~February~2011$}}}
\rfoot[\fancyplain{}{}]
{\fancyplain{}{\footnotesize\bfseries\itshape{John Doe}}}

With the above code, I get the page number appearing twice. Once with
just a plain number and next to that, is the colored boxed number.
Also, as the new chapter begins, I don't get any colored box.

Can you help me modify the code to get the correct header design:

_______
| |
| 2 |
Fifth Order Loop Filter________________________|_____|

with the page number enclosed with a colored box.

Any help will be greatly appreciated.

Thanks,
triniboy

On Feb 14, 4:22 am, Peter Flynn <anglebrac...@gmail.com> wrote:

Peter Flynn

unread,
Feb 15, 2011, 4:40:37 AM2/15/11
to latexus...@googlegroups.com
On Tue, Feb 15, 2011 at 4:25 AM, triniboy <rbsi...@gmail.com> wrote:
Hello,

Thanks for your help.  I tried your suggestion, but did not get the
correct result.  I have attached some of the code that I use in the
fancy header:

Please do not post code fragments. Post a Minimum Working Example: otherwise we all have to guess at what documentclass you are using, and all the other packages, which just makes unnecessary extra work.
 
\pagestyle{fancy}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}
\lhead[\fancyplain{}{}]

RTFM: fancyhdr.sty says very clearly "Fancyplain stuff shouldn't be used anymore (rather \fancypagestyle{plain} should be used), but it must be present for compatibility reasons." You need to fix this first. There is a lot of excess code in your example.
 
{\fancyplain{}{\footnotesize\bfseries\itshape{Fifth Order Filter}}}

Why the extra braces around {Fifth Order Filter}? \itshape does not take an argument.
 
\chead[\fancyplain{}{}]{\fancyplain{}{\footnotesize\bfseries\itshape{}}}

What is that for?
 
\rhead[\fancyplain{}{\rightmark}] {\fancyplain{}{\bf{\thepage}}

Don't use \bf, it's obsolete for over a decade. Use \textbf instead, or \bfseries.
What you have here prints the page number for the first time.
 
\colorbox{green}{\color{white}\thepage}}

And this prints it for the second time, this time in a green box.
You asked for the page number twice, and you got it twice.
 
\cfoot[\fancyplain{}{}] {\fancyplain{}{}} \lfoot[\fancyplain{}{}]
{\fancyplain{}{\footnotesize\bfseries\itshape{$11^{th}
~February~2011$}}}

Never do this. Superscript ordinals are a Victorian throwback which died in the 1930s. Microsoft resurrected them with Word very unnecessarily. Their only use is to mimic a 19th or early 20th century document. And even if you do want them for that purpose, either use \textsuperscript, or the endorg package. Keep math mode for mathematics. Best of all, just do it normally: 15th Feb with no superscript, otherwise it looks as if you are using a wordprocessor :-)
 
\rfoot[\fancyplain{}{}]
{\fancyplain{}{\footnotesize\bfseries\itshape{John Doe}}}

With the above code, I get the page number appearing twice.  

Because you asked for it twice.

Also, as the new chapter begins, I don't get any colored box.

That is correct. Read the documentation for the fancyhdr package. Chapter starts are usually unnumbered. You probably don't want the same header/footer style on chapter starts as on other pages: fancyhdr lets you define your own if you don't want it plain.
 
Can you help me modify the code to get the correct header design:

                                               _______
                                               |     |
                                               | 2   |
Fifth Order Loop Filter________________________|_____|

Never use ASCII graphics without using a monospace font :-)
 
with the page number enclosed with a colored box.

Please go and read some documentation about how to use LaTeX. I suspect you may innocently have picked it up from someone you work with, who "learned" it long ago -- not your fault at all -- resulting in some rather obsolete practices.

\documentclass[twoside]{report}
\usepackage{fancyhdr,xcolor,engord}
% redefine the format of the date
\renewcommand{\today}{\engordnumber\day\space\ifcase\month January\or
  February\or March\or April\or May\or June\or July\or August\or
  September\or October\or November\or December\fi\space\number\year}
% redefine \maketitle so that it doesn't forget the author's name!
\makeatletter
\renewcommand{\maketitle}{%
  \thispagestyle{empty}%
  \null\begingroup\raggedright
  \vspace{3cm}\noindent{\sffamily\Huge\bfseries\@title\par}%
  \vspace{2cm}{\sffamily\LARGE\@author\par}%
  \vspace{2cm}{\sffamily\large\@date\par}%
  \endgroup\clearpage\thispagestyle{empty}\clearpage
}
% arrange the headers and footers
\pagestyle{fancy}
\renewcommand{\headrulewidth}{.1pt}
\renewcommand{\footrulewidth}{.1pt}
\fancyhead[LO]{\footnotesize\bfseries\itshape\rightmark}
\fancyhead[RE]{\footnotesize\bfseries\itshape\leftmark}
\fancyhead[RO,LE]{\footnotesize\bfseries

  \colorbox{green}{\color{white}\thepage}}
\fancyfoot[C]{}
\fancyfoot[LO,RE]{\footnotesize\itshape\today}
\fancyfoot[RO,LE]{\footnotesize\bfseries\itshape\@author}
\makeatother
\begin{document}
\title{Custom page headings with colourbox numbers}
\author{RB Singh}
\maketitle
\cleardoublepage
\chapter{Fifth Order Filter}
Stuff
\section{Other stuff}
Material
\clearpage
More stuff
\clearpage
More stuff
\chapter{Yet more}
Text
\end{document}


///Peter

Ashish Revar

unread,
Feb 16, 2011, 12:25:11 AM2/16/11
to latexus...@googlegroups.com
Hello Peter,

I tried your solution but this one is not working, showing errors related to runaway arguments. I tried to solve but not successful.
What's wrong with it?

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.



--
Thanks and Regards,
Ashish Revar,
Nirma University,
ashis...@gmail.com
9909298846


jon

unread,
Feb 16, 2011, 1:45:55 AM2/16/11
to LaTeX Users Group
On Feb 16, 12:25 am, Ashish Revar <ashishre...@gmail.com> wrote:
> I tried your solution but this one is not working, showing errors related to
> runaway arguments. I tried to solve but not successful.
> What's wrong with it?

it works as given. (had to try it.)
cheers,
jon.

Ashish Revar

unread,
Feb 16, 2011, 1:52:00 AM2/16/11
to latexus...@googlegroups.com
Hello there,
This is what I am getting when I compile the code given in previous post.
Can you please tell me the problem?

error.JPG

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.

error.JPG

Peter Flynn

unread,
Feb 16, 2011, 6:24:33 AM2/16/11
to latexus...@googlegroups.com
On Wed, Feb 16, 2011 at 6:52 AM, Ashish Revar <ashis...@gmail.com> wrote:
Hello there,
This is what I am getting when I compile the code given in previous post.
Can you please tell me the problem?

error.JPG

You're using Windows. When you copied and pasted the code, Google Groups added an extra and unwanted blank line at line 25 (and probably at line 17 as well).

Just remove the blank line and it will work. Always look at the code: the error message means exactly what it says: there is a \par (blank line) around line 25 after {\footnotesize\bfseries which should not be there.

///Peter

error.JPG
Reply all
Reply to author
Forward
0 new messages