[Tex/LaTex] Fancy header for an onesided document

fancyhdrheader-footer

I am trying to use the package fancyhdr for header and footer in my custom made template for writing thesis.
There are lot of help to the fancyhdr queries on TeX.SE, but I could not find the solution to the below.

For an oneside document class, I need the header of the document to the something like this :

For an even numbered page:

page no.                                   Chapter.x ChapterTitle

(left corner)                               (right corner)

-----------------------------------------------------------------

For an odd numbered page

Section.x (just number) SectionTitle           page no.                       

(left corner)                               (right corner)

-----------------------------------------------------------------

Here is a minimal example of my thesis:

\documentclass[a4paper,12pt,oneside]{book}


\usepackage{fancyhdr}

\pagestyle{fancy}

\fancyfoot{}

\fancyhead[RO,LO]{\thepage}

\fancyhead[LO]{\leftmark}

\fancyhead[RO]{\rightmark}

\renewcommand{\chaptermark}[1]{%
\btypeout{\chaptername \ \thechapter.\ \space #1}\markboth{\@chapapp\ \thechapter.\ #1}{%
\@chapapp\ \thechapter.\ #1}}

\lhead[]{\fancyplain{}{\nouppercase{\rightmark}}}
\rhead[\fancyplain{}{\sl{\leftmark}}]{\rm\thepage}

\usepackage{lipsum}


\begin{document}

\tableofcontents

\chapter{Introduction}
\lipsum
\section{Milky Way}
\lipsum
\section{Andromeda}
\lipsum
\chapter{Spectroscopy}
\section{Hubble}
\lipsum
\section{SALT}
\lipsum




\end{document}

Best Answer

Why do you believe that you want a one-sided layout?

There are several possibilities.

  1. You really intend to print single-sided. If so, then your layout makes no sense, but that is, after all, your business. Telling LaTeX you will print duplex doesn't commit you to anything. Just Go Ahead and Lie!

  2. You do not want different sized margins.

  3. You don't want blank pages when you start a new chapter.

All of these are perfectly compatible with telling LaTeX twoside. 2 and 3 don't even require you to lie about it.

On the other hand, if you say oneside then all pages get the same format. Sure, you could hack the output routine to check the page number and set the headers and footers accordingly, but that's just what twoside does anyway, so you might as soon use it.

The following satisfies 2 and 3. To satisfy 1, simply tell your printer to print single-sided. (Liar!) If your printer can't print duplex (and often even if it can), it will do this anyway. Saying twoside to LaTeX isn't saying anything to your printer. It only affects the default layout.

openany prevents empty pages before chapters. We load geometry with hmarginration=1:1 to ensure symmetrical margins. headheight=15pt is required to accommodate the header. (Check the console output for the value fancyhdr needs - it will warn you if the setting is too small.)

And that's really it. Sure it is a twoside layout - that's how we get different headers for odd and even pages. In all other respects, it is just like oneside.

That is, you should want this, even if you thought you wanted something else because the something else would be a great deal more work and much more fragile, and would be no different in appearance or functionality. Better Lie!

\documentclass[a4paper,12pt,openany]{book}
\usepackage[headheight=15pt,hmarginratio=1:1]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhf[roh,leh]{\thepage}
\fancyhf[reh]{\nouppercase{\leftmark}}
\fancyhf[loh]{\nouppercase{\rightmark}}


\usepackage{lipsum}
\begin{document}

\tableofcontents

\chapter{Introduction}
\lipsum
\section{Milky Way}
\lipsum
\section{Andromeda}
\lipsum
\chapter{Spectroscopy}
\section{Hubble}
\lipsum
\section{SALT}
\lipsum

\end{document}

two-sided layout for symmetry with no blanks

Related Question