2

After hours of debugging, I finally found out that the header of my bibliography's last page is changed by using spacing in the text. Now my world is turned upside down.

Please have a look at this correct example. Both \begin{spacing} and \end{spacing} are commented out:

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

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\newcommand{\plainplain}{%
  \fancypagestyle{plain}{%
      \renewcommand{\headrulewidth}{0pt}
      \fancyhead[LO]{}
      \fancyhead[RE]{}
}}
\newcommand{\myplain}{%
  \fancypagestyle{plain}{%
    \fancyhead[LO]{\bfseries\rightmark}
    \fancyhead[RE]{\bfseries\leftmark}
    \renewcommand{\headrulewidth}{0.5pt}
    }}
\plainplain
\pagestyle{plain}

\usepackage{lipsum, filecontents}

\begin{document}

\frontmatter
\pagenumbering{Roman}

\tableofcontents
\clearpage

\listoffigures
\clearpage

\listoftables
\clearpage

\mainmatter % implies arabic pagenumbering
\myplain
\pagestyle{plain}

% \begin{spacing}{1.5}

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]

\clearpage
\plainplain
\pagestyle{plain}

\backmatter
\pagenumbering{roman}
\setcounter{page}{5}

\nocite{mybook1}
\nocite{mybook2}
\nocite{mybook3}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook10}

\bibliographystyle{plainnat}

\begin{filecontents}{bibliography.bib}
@book{mybook1,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}

\bibliography{bibliography}
% \end{spacing}
\end{document}

Note that there is only a small roman page number on the top right; the same applies to the previous bibliography pages.

enter image description here

This is the erroneous version where I use spacing; there are just two lines more (\begin{spacing} and \end{spacing}):

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

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\newcommand{\plainplain}{%
  \fancypagestyle{plain}{%
      \renewcommand{\headrulewidth}{0pt}
      \fancyhead[LO]{}
      \fancyhead[RE]{}
}}
\newcommand{\myplain}{%
  \fancypagestyle{plain}{%
    \fancyhead[LO]{\bfseries\rightmark}
    \fancyhead[RE]{\bfseries\leftmark}
    \renewcommand{\headrulewidth}{0.5pt}
    }}
\plainplain
\pagestyle{plain}

\usepackage{lipsum, filecontents}

\begin{document}

\frontmatter
\pagenumbering{Roman}

\tableofcontents
\clearpage

\listoffigures
\clearpage

\listoftables
\clearpage

\mainmatter % implies arabic pagenumbering
\myplain
\pagestyle{plain}

\begin{spacing}{1.5}

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]

\clearpage
\plainplain
\pagestyle{plain}

\backmatter
\pagenumbering{roman}
\setcounter{page}{5}

\nocite{mybook1}
\nocite{mybook2}
\nocite{mybook3}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook10}

\bibliographystyle{plainnat}

\begin{filecontents}{bibliography.bib}
    \documentclass[12pt, a4paper, oneside, onecolumn]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\newcommand{\plainplain}{%
  \fancypagestyle{plain}{%
      \renewcommand{\headrulewidth}{0pt}
      \fancyhead[LO]{}
      \fancyhead[RE]{}
}}
\newcommand{\myplain}{%
  \fancypagestyle{plain}{%
    \fancyhead[LO]{\bfseries\rightmark}
    \fancyhead[RE]{\bfseries\leftmark}
    \renewcommand{\headrulewidth}{0.5pt}
    }}
\plainplain
\pagestyle{plain}

\usepackage{lipsum, filecontents}

\begin{document}

\frontmatter
\pagenumbering{Roman}

\tableofcontents
\clearpage

\listoffigures
\clearpage

\listoftables
\clearpage

\mainmatter % implies arabic pagenumbering
\myplain
\pagestyle{plain}

\begin{spacing}{1.5}

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]

\clearpage
\plainplain
\pagestyle{plain}

\backmatter
\pagenumbering{roman}
\setcounter{page}{5}

\nocite{mybook1}
\nocite{mybook2}
\nocite{mybook3}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook10}

\bibliographystyle{plainnat}

\begin{filecontents}{bibliography.bib}
@book{mybook1,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}

\bibliography{bibliography}
\end{spacing}
\end{document}

Look at the incorrect result. It should not look like that. Also, the bibliography's last page is the only page that is wrong. All the other pages are like in my example of the correct version.

enter image description here

Question: Why does spacing change the header of my bibliography's last page?

Xiphias
  • 1,233
  • 1
    Please, have a look at package lipsum or blindtext – Johannes_B Feb 04 '14 at 18:05
  • @Johannes_B Thank you for that hint. I already used lipsum for text, but I didn't know that I could use the command in my bibliography as well. Makes sense, though, as it is part of the tex file. I changed it, and now the code is shorter. – Xiphias Feb 04 '14 at 18:11
  • 1
    Quickfix: Put a \clearpage before \end{spacing} – Johannes_B Feb 04 '14 at 18:14
  • Thank you for that quickfix! It works! This is like magic. As I searched about one and a half hour for the reason behind the problem, would you be so kind to point into the direction where I can find out what happened? I always try to understand TeX, but for this I do not have an explanation. – Xiphias Feb 04 '14 at 18:17
  • I am sorry, i didn't take a close look at your code. Right now i don't know what's going on. – Johannes_B Feb 04 '14 at 18:18
  • 2
    You shouldn't be using the spacing environment to begin with. – egreg Feb 04 '14 at 18:23
  • Is it a "bad" package? I found a more TeX-like alternative here using \linespread directly: http://tex.stackexchange.com/questions/61155/linespacing-without-packages – Xiphias Feb 04 '14 at 18:25
  • @Tobias I said the spacing environment, not the package. – egreg Feb 04 '14 at 18:44
  • @Tobias You asked "where I can find out what happened?" What happened is you enclosed the document inside a group (the spacing environment). Within that group you made changes to the headers. These changes are lost at the end of that group (at \end{spacing}). The last page is output after that group ends and so it gets the headers that were in effect before the group was started. Most things in (La)TeX are like this: changes made inside a group revert to their previous definitions at the end of the group. – Dan Feb 04 '14 at 21:06

1 Answers1

3

You shouldn't be using the spacing environment around the whole document to begin with; this is bound to give problems, and you found out one of them. Use the spacing environment only for small chunks of text. The correct way is (assuming you must use increased interline space) to declare \onehalfspacing.

You also shouldn't redefine the plain style several times: you can name how many page styles you desire.

\documentclass[12pt,a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}
\usepackage{lipsum, filecontents}

\setlength{\headheight}{14.5pt}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}

\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}

\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{plainplain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhead[LO]{}%
  \fancyhead[RE]{}%
}
\fancypagestyle{myplain}{%
  \fancyhead[LO]{\bfseries\rightmark}%
  \fancyhead[RE]{\bfseries\leftmark}%
    \renewcommand{\headrulewidth}{0.5pt}%
}

\pagestyle{plainplain}

\begin{document}

\frontmatter

\tableofcontents

\listoffigures

\listoftables

\mainmatter % implies arabic pagenumbering
\pagestyle{myplain}

\onehalfspacing

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]


\cleardoublepage

\backmatter
\pagestyle{plainplain}
\pagenumbering{roman}
\setcounter{page}{5}

\nocite{*}

\bibliographystyle{plainnat}

\begin{filecontents}{\jobname.bib}
@book{mybook1,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}

\bibliography{\jobname}

\end{document}

One half line spacing is a typographical sin, in Italian I would use the word obbrobrio. Also resuming the Roman numbering in the back matter has no justification (other than being a prescription of some institution prepared by bureaucrats who know nothing about typography).

I have removed the oneside option, otherwise the RE or LO options would make no sense. Note that adding only 0.5pt to \headheight is insufficient (but fancyhdr tells you the required length).

If you don't want the page style plain to reemerge when chapter are started, and also avoid doing explicit declarations in the body of the document, it's easier patching the relevant commands:

\documentclass[12pt,a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}
\usepackage{lipsum, filecontents}

\usepackage{etoolbox}

% save the last frontmatter page number
\newcounter{savepage} % save the last frontmatter page number
% no plain style at chapter openings
\patchcmd{\chapter}{\thispagestyle{plain}}{}{}{}
% choose a page style for the frontmatter
\apptocmd{\frontmatter}{\pagestyle{frontback}}{}{}
% patch \mainmatter to save the page number and to choose a new page style
\patchcmd{\mainmatter}
  {\cleardoublepage}
  {\cleardoublepage
   \setcounter{savepage}{\value{page}}%
   \pagestyle{normal}%
  }{}{}
% patch \backmatter to choose a new page style and restore the page number
\apptocmd{\backmatter}
  {\pagestyle{frontback}%
   \pagenumbering{roman}%
   \setcounter{page}{\value{savepage}}%
  }{}{}

% set the headheight to a suitable value suggested by fancyhdr
\setlength{\headheight}{14.5pt}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}

\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}

\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{frontback}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhead[LO]{}%
  \fancyhead[RE]{}%
}

\fancypagestyle{normal}{%
  \fancyhead[LO]{\bfseries\rightmark}%
  \fancyhead[RE]{\bfseries\leftmark}%
  \renewcommand{\headrulewidth}{0.5pt}%
}

\begin{document}

\frontmatter

\tableofcontents

\listoffigures

\listoftables

\mainmatter % implies arabic pagenumbering

\onehalfspacing

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]


\cleardoublepage

\backmatter

\nocite{*}

\bibliographystyle{plainnat}

\begin{filecontents}{\jobname.bib}
@book{mybook1,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}

\bibliography{\jobname}

\end{document}
egreg
  • 1,121,712
  • Thank you very much for your time. Yes, I use this layout because of my institution's rules. Have you noticed that my chapter beginnings are now differently formatted in your version, although you solved my initial problem? The bibliography and every other "first page" now have the page number centered at the bottom. This is probably because of the missing redefinition of plain. Is there a way to fix this using your optimized solution? – Xiphias Feb 04 '14 at 18:36
  • @Tobias What do you want for those first pages? – egreg Feb 04 '14 at 18:43
  • @egreg thanks for making some remarks on the matter. I really didn't have the time. – Johannes_B Feb 04 '14 at 18:45
  • @egreg My institution's prescription says that the chapter beginnings' headings have to be the same as the "normal" pages. At the moment, the chapter beginnings are the only pages that differ from what you defined in the fancyhdr configuration. – Xiphias Feb 04 '14 at 18:49
  • 1
  • @Tobias And what about the first page of the bibliography, table of contents and so on? – egreg Feb 04 '14 at 19:00
  • I appreciate your help. I do not want to be pedantic, but I'd like to get this right. egreg said that I should not redefine plain, it would be bad coding. However, your link shows how to do exactly that. So is it legitimate in my case? – Xiphias Feb 04 '14 at 19:00
  • @egreg All pages of the frontmatter should have only I, II, III ... the main matter has to have 1, 2, 3, ... plus the section heading and the rule. The backmatter with the bibliography has to have iv, v, vi, vii ... on every page, including the first bibliography page. I know that this is typographically questionable but that's my prescription. Now this is exactly what you wanted me not to do --- Johannes_B suggests to redefine plain. Is it legitimate here? Please don't judge me for putting a roman page number on my table of contents, I am already hesitating to do that :-) – Xiphias Feb 04 '14 at 19:02
  • 1
    @Tobias I've added a new version that should suit your needs. You have my sympathy for being forced to use such a horrible layout. – egreg Feb 04 '14 at 20:47
  • @egreg Now this is fantastic. You solved my problem; on top of that, you also showed me truly elegant new approaches that I didn't know. I just downloaded the etoolbox documentation and will have a look at that. Great! – Xiphias Feb 04 '14 at 21:20