0

I have problems with how header and footers are displayed in my document. I have made a minimal example where the problems still exist:

Problem 1: Header ruler moves up and down from front- to mainmatter
Problem 2: footer image moves up and down from front- to mainmatter
Problem 3: I want "Sections" to be displayed ALWAYS in the header also at the beginning of a chapter. Right now there is an empty line underneath the chapter, followed by the header ruler.

enter image description here

enter image description here

This my "code":

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

\usepackage{tocloft}% to alighn the TOC numbers on the right
\usepackage{setspace}
\usepackage{epstopdf}
\usepackage{lipsum}
\usepackage{textcomp}
\usepackage{listings}

\usepackage{fancyhdr}
    \pagestyle{fancy}
    \pagestyle{fancyplain}
    \fancyhead{}
    \fancyfoot{}
    \fancyfoot[L]{\rule{2cm}{1cm}}
    \fancyfoot[RO, LE] {\thepage}
    \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
}}

\lfoot[LE,LO]{\rule{2cm}{1cm}}

\usepackage[
   bottom,      % Footnotes appear always on bottom. This is necessary
                % especially when floats are used
   stable,      % Make footnotes stable in section titles
   %perpage,     % Reset on each page
   %para,       % Place footnotes side by side of in one paragraph.
   %side,       % Place footnotes in the margin
   ragged,      % Use RaggedRight
   %norule,     % suppress rule above footnotes
   multiple,    % rearrange multiple footnotes intelligent in the text.
   %symbol,     % use symbols instead of numbers
]{footmisc}



\begin{document}
\renewcommand{\cftchapaftersnum}{.}

\frontmatter
\pagenumbering{Roman}
\fancyfoot[RO, LE] {\small\thepage}
\thispagestyle{empty}
\fancyfoot[R] {\thepage}
\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\renewcommand\lstlistingname{Code listing}
\renewcommand\lstlistlistingname{List of Listings}
\rhead{\small{List of Listings}}
\addcontentsline{toc}{chapter}{List of Listings}
\lstlistoflistings

\mainmatter
\pagenumbering{arabic}
\onehalfspacing
\fancyhead{}
\pagestyle{plain}
\lhead{}
\rhead{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
\fancyfoot[R] {\thepage}
\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\chapter{Some Chapter}
\lipsum

\section{Some section}
\lipsum

\end{document}

2nd attempt

Now that header and footer are no longer moving around I start feeling happy.

Still there are the other issues I have pointed out.

  1. Now List of listings appears twice in the header (once as chapter and below as section)

  2. The first section still is not printed in the header (only the chapter is printed and an empty line)

This is what it looks like:

Here is the current code:

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

\usepackage{tocloft}% to alighn the TOC numbers on the right
\usepackage{setspace}
\usepackage{epstopdf}
\usepackage{textcomp}
\usepackage{listings}

%%# IN NEW BEGIN
\usepackage{lipsum}
\usepackage[headheight=30pt,
            a4paper,
            left=42mm,
            right=29mm,
            top=32mm,
            bottom=38mm
]{geometry}  % or %\setlength{\headheight}{30pt}
%% IN NEW END

\usepackage{fancyhdr}
    %\pagestyle{fancy}
    %\pagestyle{fancyplain}
    %\fancyhead{}
    %\fancyfoot{}
    \fancyhead[L]{}
    \fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
    \fancyfoot[L]{\rule{2cm}{1cm}}
    \fancyfoot[R]{\thepage}
    \fancyfoot[C]{}
    \fancypagestyle{plain}{%
        \fancyhf{} % clear all header and footer fields
        \fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
        \fancyfoot[L]{\rule{2cm}{1cm}}
        \fancyfoot[R]{\thepage}
        \fancyfoot[C]{}
        \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
    }}}
    \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
    }}


\begin{document}
\renewcommand{\cftchapaftersnum}{.}

%%# IN NEW BEGIN
\pagestyle{fancy}
%%# IN NEW END

\frontmatter
\pagenumbering{Roman}

%% OUT NEW BEGIN
%\fancyfoot[RO, LE] {\small\thepage}
%\thispagestyle{empty}
%\fancyfoot[R] {\thepage}
%% OUT NEW END

\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\renewcommand\lstlistingname{Code listing}
\renewcommand\lstlistlistingname{List of Listings}
\rhead{\small{List of Listings}}
\addcontentsline{toc}{chapter}{List of Listings}
\lstlistoflistings

\mainmatter

%% IN NEW BEGIN
\renewcommand{\sectionmark}[1]{\markright{\thesection~#1}}
%% IN NEW END

\pagenumbering{arabic}
\onehalfspacing

%% OUT NEW BEGIN
%\fancyhead{}
%\pagestyle{plain}
%\lhead{}
%\rhead{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
%\fancyfoot[R] {\thepage}
%% OUT NEW END

\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\chapter{Some Chapter}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\section{Some section}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.


\end{document}`
  • Your example is not really a reasonable size for anyone to look at. Please remove all packages that are not needed to show the problem, and similarly remove all text that is not needed. You should be able to make an example of just a few lines without any external images. (You can use \rule{2cm}{1cm} in place of an image) – David Carlisle Jan 22 '15 at 11:18
  • I agree with David, this is by far not a minimal example. If the problem is caused by some other package, you can find out yourself by consecutively eliminating all packages until the problem disappears. Also, since you are using a KOMA Script class anyways, why don't you use its own capabilities instead of fancyhdr? – Manuel Weinkauf Jan 22 '15 at 11:23
  • I striped it down to a minimum where it will still build and have the (unwanted) effects: – latenite Jan 22 '15 at 11:37
  • Is there a reason for doing it all by hand? – Johannes_B Jan 22 '15 at 12:08
  • Is this the way to reply to answers??? The footer in the lower left still jumps around. Also List of listings ist displayed twice now. i.imgur.com/wdlCEVw.jpg i.imgur.com/uyFiAVT.jpg – latenite Jan 22 '15 at 13:03
2

When you compile you get many warnings, one of them is

Package Fancyhdr Warning: \headheight is too small (12.0pt): Make it at least 17.99998pt. We now make it that large for the rest of the document. This may cause the page layout to be inconsistent, however.

So add

\usepackage[headheight=18pt]{geometry}  % or
%\setlength{\headheight}{18pt}

to solve this. Also the E option is useless in one side document.

  • 1
    Why not 18pt, which is the suggested value? – egreg Jan 23 '15 at 18:30
  • @egreg Because I was sleepy then :-). Thanks for pinging. I have corrected it. – user11232 Jan 24 '15 at 0:56
1

Here is a suggestion using scrbook and scrlayer-scrpage.

Note that I define \rightbotmark to use the last right mark occuring on the chapter page. Normally the first one is used but on chapter pages the first one is set by \chapter.

\documentclass[
  12pt,
  oneside,
  parskip=full-,
  chapterprefix,
  footheight=33pt,
  headheight=28pt,
  listof=totoc
]{scrbook}
\usepackage{setspace}

\usepackage{scrhack}
\usepackage{listings}
\renewcommand\lstlistingname{Code listing}
\renewcommand\lstlistlistingname{List of Listings}

\usepackage[
  a4paper,
  left=42mm,
  right=29mm,
  top=32mm,
  bottom=38mm,
  %showframe % to show the pagelayout
]{geometry}

\usepackage[autooneside=false]{scrlayer-scrpage}
\automark[chapter]{chapter}
\automark*[section]{}
\KOMAoptions{onpsinit=\setstretch{1}}% singlespacing in header
\clearpairofpagestyles
\makeatletter
\providecommand*{\rightbotmark}{\expandafter\@rightmark\botmark\@empty\@empty}
\makeatother
\addtokomafont{pageheadfoot}{\small}
\ohead%
  [\ifstr{\leftmark}{\rightbotmark}{}{\leftmark}\\\rightbotmark]% pagestyle plain
  {\ifstr{\leftmark}{\rightmark}{}{\leftmark}\\\rightmark}% pagesstyle scrheadings
\ifoot*{\rule{2cm}{1cm}}
\ofoot*{\pagemark}


\ModifyLayer[
contents={%
  \usekomafont{pageheadfoot}\usekomafont{pagehead}\usekomafont{headsepline}%
  \rlap{\rule[-2pt]{\layerwidth}{1.9pt}}%
  \rule[-3.6pt]{\layerwidth}{.6pt}%
  }]{scrheadings.head.below.line}
\ModifyLayer[clone=scrheadings.head.below.line]{plain.scrheadings.head.below.line}


\usepackage{xcolor}
\addtokomafont{headsepline}{\color{purple}}

\usepackage{blindtext}% dummy text

\begin{document}

\frontmatter
\pagenumbering{Roman}
\lstlistoflistings

\mainmatter
\onehalfspacing
\chapter{Some Chapter}
\blindtext
\section{Some section}
\Blindtext
\chapter{Next Chapter}
\Blindtext[3]
\section{Some section}
\Blindtext

\blinddocument
\end{document}

enter image description here

enter image description here

0

Not perfect, but a beginning:

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

\usepackage{tocloft}% to alighn the TOC numbers on the right
\usepackage{setspace}
\usepackage{epstopdf}
\usepackage{textcomp}
\usepackage{listings}
\usepackage{lipsum}

\usepackage{fancyhdr}
    %\pagestyle{fancy}
    %\pagestyle{fancyplain}
    %\fancyhead{}
    %\fancyfoot{}
    \fancyhead[L]{}
    \fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
    \fancyfoot[L]{\rule{2cm}{1cm}}
    \fancyfoot[R]{\thepage}
    \fancyfoot[C]{}
    \fancypagestyle{plain}{%
        \fancyhf{} % clear all header and footer fields
        \fancyhead[R]{\small\nouppercase\leftmark\\\small\nouppercase\rightmark}
        \fancyfoot[L]{\rule{2cm}{1cm}}
        \fancyfoot[R]{\thepage}
        \fancyfoot[C]{}
        \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
    }}}
    \renewcommand{\headrule}{{%
        \hrule height 1.9pt width\headwidth
        \vspace{1.6pt}%
        \hrule height 0.9pt width\headwidth
    }}



\begin{document}
\renewcommand{\cftchapaftersnum}{.}

\pagestyle{fancy}

\frontmatter
\pagenumbering{Roman}
\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\renewcommand\lstlistingname{Code listing}
\renewcommand\lstlistlistingname{List of Listings}
\addcontentsline{toc}{chapter}{List of Listings}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\sectionmark{}
\lstlistoflistings

\mainmatter
\renewcommand{\sectionmark}[1]{\markright{\thesection~#1}}
\pagenumbering{arabic}
\onehalfspacing


\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}

\chapter{Some Chapter}
\lipsum[1]

\section{Some section}
\lipsum


\end{document}

This reserves the space for the second line also on the frontmatter pages without printing anything, so that headrule remains the same height. Also, it starts printing section names as soon as there is a section name, and reserves the empty second line in the header when there is none. I only did not succeed in printing the section name on the page where a new chapter starts, when the section starts on the same page, although I would have thought redefining plain should have done the trick.

  • i.imgur.com/kNv5nJB.jpg – latenite Jan 23 '15 at 17:31
  • i.imgur.com/FgGCrf5.jpg – latenite Jan 23 '15 at 17:31
  • Sorry I could not post the links to the images in the question above. There are still the other two issues I had. How can I make the header have the 1st section on the page of the chapter. And How do I make the "List of listings" appear only once? Thanks in advance. – latenite Jan 23 '15 at 17:32
  • @latenite This site operates a policy of one question per... well, per question, really. Things get messy when you ask multiple questions in one thread. (If person A answers question 1, person B answers question 2, and person C answers question 3, whose answer is the most useful and should be accepted?) The questions you've asked are really unrelated. One has to do with the format of headers and footers. That one is answered. (Although, if you are using Koma, you ought not do it this way.) The others have to do with the content of the 'marks' set. That's not really fancyhdr's business. – cfr Jan 24 '15 at 0:01
  • @latenite (Or maybe you are not using Koma now? Maybe look at titleps in that case?) – cfr Jan 24 '15 at 0:03

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.