2

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}
  • Please provide a minimum working example which shows how you are trying to use fancyhdr, in what context you are trying to use it and what, exactly, the obstacle is to your success. However, a one-sided document does not have different page layout for odd and even pages. All pages are odd for practical purposes because they are all on the right-hand side. So your question seems rather confused and unclear. – cfr Sep 10 '16 at 0:06
  • @cfr I have added the working example in the question. Hope this helps. – Tanvir Sep 10 '16 at 1:18
  • 2
    It makes no sense to have different headers and footers for odd and even pages in a one-sided document. – cfr Sep 10 '16 at 1:59
  • How is \btypeout defined? – cfr Sep 10 '16 at 2:02
  • @cfr \newcommand\btypeout[1]{\bhrule\typeout{\space #1}\bhrule} – Tanvir Sep 10 '16 at 2:38
3

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

  • I must confess that, yes, it doesn't make any sense to have different header and footer for one-sided document. Point 2 is what I need. But, anyways, the above example was helpful. To add, I have set the margins using \usepackage{vmargins}. How will be able to set same margins for even numbered pages? – Tanvir Sep 10 '16 at 3:03
  • 1
    I don't know vmargins. If you mean vmargin, don't use it. Use geometry instead. vmargin shouldn't be used in new code. – cfr Sep 10 '16 at 3:13
  • Sorry, it is vmargin. I could solve it using \usepackage{vmargin}. I added the following \setlength{\oddsidemargin}{3.5cm} \setlength{\evensidemargin}{3.5cm}. I will use geometry as you have mentioned. Many thanks for the help. – Tanvir Sep 10 '16 at 3:22
  • 1
    Really, you should not do that.vmargin causes problems. It is not at all a good approach. You are just storing up trouble for later. It will be easier to switch to geometry. Unless you are very close to a submission deadline for a long document e.g. your doctoral dissertation is due tomorrow. Then emergency hacks are probably best - you can deal with the fallout at your leisure. If it isn't a situation of that kind, though, it is better to avoid the fallout in the first place. – cfr Sep 10 '16 at 3:23
  • Okay. I am going to change it to geometry. Will post if there are any problems. – Tanvir Sep 10 '16 at 3:25
1

I had one but with book documentclass, given below It is explained page 10 of fancyhdr document. But MWE will give details of your documentclass.

\documentclass{book}



\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]%
{\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
\renewcommand{\sectionmark}[1]%
{\markright{\MakeUppercase{\thesection.\ #1}}}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}

\fancyhf{}
\renewcommand{\chaptermark}[1]{%
    \markboth{\chaptername
        \ \thechapter.\ #1}{}}

\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\nouppercase \rightmark}
\fancyhead[RE]{\nouppercase \leftmark}



\usepackage{lipsum}


\begin{document}

    \tableofcontents

    \chapter{First}
    \section{First within First}
    \lipsum
    \section{Second within First}
    \lipsum
    \chapter{Second}
    \section{First within Second}
    \lipsum
    \section{Second within Second}
    \lipsum




\end{document}
  • @RamaKrishnaMagesty This work perfectly for a two sided document. I want the same format to work in a one sided document too. How to do that? – Tanvir Sep 10 '16 at 1:16

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.