Text FormattingFancyHDR Package - Define Custom Styles?

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
RiW
Posts: 29
Joined: Wed Sep 01, 2010 12:11 am

FancyHDR Package - Define Custom Styles?

Post by RiW »

Hi,

I'm relatively new to Latex and am getting a little confused with the fancyhdr documentation. I'm writing a report and want to have a standard header/footer for all pages, apart from those before the main report (i.e. title, abstract and contents).

By default, Latex seems to define the first page of every chapter differently so I've managed to uniformly format my pages using:

Code: Select all

%Define Fancy Header (using fancyhdr and pagesLts)
\pagestyle{fancyplain}
\lhead{}
\chead{}
\rhead{}
\lfoot{TITLE}
\cfoot{}
\rfoot{\thepage\ / \lastpageref{pagesLTS.arabic}}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}

%Redefine Plain Header
\fancypagestyle{plain}{
\fancyhf{} % clear all header and footer fields
\fancyfoot[L]{TITLE}
\fancyfoot[R]{\thepage\ / \lastpageref{pagesLTS.arabic}}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}}
The title and abstract environments, by default, don't show a header / footer (perhaps because they use pagestyle{empty}?). The contents page, however, uses the above definition.

Ideally I'd like to create a new style which simply shows the page number e.g.
\cfoot{\thepage\} and then use this for the abstract and contents page.

However, I can't determine from the documentation when to define items as cfoot{.etc.} and when to use \fancyfoot and more importantly, how to define a new style and then deploy it for a multi-page contents page.

Any tips / guidance would be greatly appreciated.

Thanks

PS - Apologies as I meant to post this in the Page Layout section, not text formatting.
Last edited by RiW on Wed Sep 15, 2010 3:01 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

FancyHDR Package - Define Custom Styles?

Post by west.logan »

I'm not an expert by any means but I was trying to do something similar with fancyheader. Basically, I ended up defining fancyheader twice as such:

Code: Select all

\begin{document}
\maketitle

\chead{center header} 
\cfoot{}
\renewcommand{\headrulewidth}{0pt}
\thispagestyle{fancy}

\newpage
\chead{}
\cfoot{\thepage}
\lhead{Left Header \\ Left Header 2}
\rhead{Right Header \\ Right Header 2} 
\renewcommand{\headrulewidth}{0.4pt}
\pagenumbering{roman} %i.e. i, ii, iii
\pagestyle{fancy}

\section{new section}
So basically I defined the parameters for "fancyheader" twice, once for the title page (in my case) and then again for the rest of the document (except that I changed the page numbering to arabic after the table of contents pages and such) .

I hope this answers at least part of what you were asking.
RiW
Posts: 29
Joined: Wed Sep 01, 2010 12:11 am

Re: FancyHDR Package - Define Custom Styles?

Post by RiW »

Ok, I think I see what you've done. Will give it a try and let you know. As an aside, is it possible to define multiple new styles or can you only use prenamed styles - such as plain, empty, fancyplain etc.?
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: FancyHDR Package - Define Custom Styles?

Post by west.logan »

That would be nice but I don't know how either. If you do find out, let me know.
RiW
Posts: 29
Joined: Wed Sep 01, 2010 12:11 am

Re: FancyHDR Package - Define Custom Styles?

Post by RiW »

Ok, got it sorted based on your suggestion of redefining styles after the introductionary sections (so thanks west.logan!).

I ended up redefining plain, rather than using fancyplain, as Latex defaults all the first pages of chapters to this style anyway.

I suspect this method is frowned upon, compared to setting up proper styles, although I couldn't work out how to do that.

Thanks!
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

FancyHDR Package - Define Custom Styles?

Post by gmedina »

Hi,
west.logan wrote:That would be nice but I don't know how either. If you do find out, let me know.
the following example shows two different customized styles in action:

Code: Select all

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}% to generate filler text

\pagestyle{fancy}

\newcommand\mystyleone{%
  \fancyhf{}
  \fancyhead[C]{Center}
  \fancyhead[L]{Left}
  \fancyhead[R]{Right}
  \fancyfoot{\thepage}
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0.4pt}
}

\newcommand\mystyletwo{%
  \fancyhf{}
  \fancyfoot[C]{Center}
  \fancyfoot[L]{Left}
  \fancyfoot[R]{Right}
  \fancyhead{\thepage}
  \renewcommand{\footrulewidth}{0pt}
  \renewcommand{\headrulewidth}{0.4pt}
}

\begin{document}

\mystyleone
\lipsum[1-5]

\clearpage

\mystyletwo
\lipsum[1-5]

\end{document}
I would, however, suggest the titlesec package to define customized page styles (it's more flexible than, and at least as powerful as, fancyhdr).
1,1,2,3,5,8,13,21,34,55,89,144,233,...
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: FancyHDR Package - Define Custom Styles?

Post by west.logan »

That is far more elegant, and just the sort of thing I wanted to do previously. Thank you!
I will also check out the titlesec documentation.
RiW
Posts: 29
Joined: Wed Sep 01, 2010 12:11 am

Re: FancyHDR Package - Define Custom Styles?

Post by RiW »

Just to echo west.logan's thanks... that's what I was aiming for & will have a look at titlesec too. Thanks!
Post Reply