[Tex/LaTex] How to use fancyhdr to style the BibTeX’s page headings

bibliographiesfancyhdrheader-footer

I haven't yet found a solution to this problem. Before using BibTeX, I styled my bibliographies with fancyhdr, which worked. I was able to write it directly into the bibliography environment:

\begin{thebibliography}{9}
    \setcounter{page}{5}
    \thispagestyle{fancy}

    \bibitem{marketing}
      Armstrong, Kotler, Saunders, Wong (2007):
      Grundlagen des Marketing,
      Pearson Education Deutschland GmbH,
      München 2007
\end{thebibliography}

I now switched to BibTeX. Checking the BibTeX command \bibliography{}, I saw that it "just" inputs text from the .bbl file. There the environment definition can be found. But this does not help me.

How can I define my bibliography's headers using fancyhdr? If not possible, how can I define them in another way?

If you provide a fancy macro that does what I am looking for, please be so kind to tell me where I can look up those features so that I write it by myself next time. I sometimes look up commands via \show\mycommand. Are there more efficient ways?

I already have the correct fancyhdr definition, but I do not know how to apply it to my BibTeX bibliography.

I'd like to apply this …

\pagenumbering{roman}
\setcounter{page}{5}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}

… to everything in:

\bibliography{mybibfile}

I'm using natbib. My fancyhdr definitions work well for every other part of my document.

Update: minimal working example
It is not so minimal, but it produces what I mean.

Please note that I use \romanhdr to switch to roman page numbers and a certain fancyhdrlayout in the beginning and it works very well. How can I get it in the bibliography as well? If you compile my file and watch the bibliography, everything is different.

LaTeXfile:

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

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

\usepackage{fancyhdr}
\pagestyle{fancy}
% with this we ensure that the chapter and section
% headings are in lowercase.
\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
\fancypagestyle{plain}{%
\fancyhead{} % get rid of headers on plain pages
\renewcommand{\headrulewidth}{0pt} % and the line
}

%%--------------------------------------------------------------------------
%% DOCUMENT-SPECIFIC
%%--------------------------------------------------------------------------
\usepackage{tabularx}
\setlength{\oddsidemargin}{1.5cm}
\setlength{\topmargin}{-0.5cm}
\setlength{\textheight}{22.5cm}
\usepackage{acronym}
\usepackage{tocloft}
\usepackage{tocbibind}

\newcommand{\chp}[1]{\chapter{#1}\thispagestyle{fancy}}
\newcommand{\romanhdr}
    {
    \thispagestyle{fancy}
    \renewcommand{\headrulewidth}{0pt}
    \fancyhead[LO]{}
    \fancyhead[RE]{}
    }
\newcommand{\fancyhdr}{
    \fancyhead[LO]{\bfseries\rightmark}
    \fancyhead[RE]{\bfseries\leftmark}
    \renewcommand{\headrulewidth}{0.5pt}
    }

\begin{document}

\frontmatter

\pagenumbering{Roman}
\tableofcontents
\romanhdr
\clearpage

\chapter*{Abkürzungsverzeichnis}
\romanhdr
\addcontentsline{toc}{chapter}{Abkürzungsverzeichnis}
\clearpage
\listoffigures
\romanhdr
\clearpage
\listoftables
\romanhdr
\clearpage

\fancyhdr

\mainmatter % implies arabic pagenumbering
\chp{Introduction}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\clearpage

\backmatter
\pagenumbering{roman}
\setcounter{page}{5}
\nocite{mybook1}
\nocite{mybook2}
\nocite{mybook3}
\nocite{mybook4}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook9}
\nocite{mybook10}
\bibliographystyle{plainnat}
\bibliography{bibliography}
\end{document}

BibTeX file:

@book{mybook1,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}

This is correct:

Correct layout

This is wrong:

Wrong layout

Best Answer

So I think that the following may do what you want but I'm not entirely clear what you want so I could be wrong about this. I take it you want the output to look roughly like this:

Varying page styles

If so, something like this should do it:

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

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

\usepackage{fancyhdr}
\pagestyle{fancy}
% with this we ensure that the chapter and section
% headings are in lowercase.
\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}
    }}


%%--------------------------------------------------------------------------
%% DOCUMENT-SPECIFIC
%%--------------------------------------------------------------------------
\usepackage{tabularx}
\setlength{\oddsidemargin}{1.5cm}
\setlength{\topmargin}{-0.5cm}
\setlength{\textheight}{22.5cm}
\usepackage{acronym}
\usepackage{tocloft}
\usepackage{tocbibind}

\usepackage{filecontents,lipsum}
\begin{filecontents}{bibliography.bib}
@book{mybook1,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}


\begin{document}
\plainplain

\frontmatter

\pagenumbering{Roman}
\tableofcontents
\clearpage

\chapter*{Abkürzungsverzeichnis}
\addcontentsline{toc}{chapter}{Abkürzungsverzeichnis}
\clearpage
\listoffigures
\clearpage
\listoftables
\clearpage

\myplain

\mainmatter % implies arabic pagenumbering
\chapter{Introduction}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\lipsum[1-7]

\section{New Section}

\lipsum[8-14]

\clearpage
\plainplain

\backmatter
\pagenumbering{roman}
\setcounter{page}{5}
\nocite{mybook1}
\nocite{mybook2}
\nocite{mybook3}
\nocite{mybook4}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook9}
\nocite{mybook10}
\bibliographystyle{plainnat}
\bibliography{bibliography}
\end{document}

This works by redefining the pagestyle plain rather than defining additional styles which you then have to manually apply. Basically, it provides two commands to switch between two versions of plain. \plainplain covers the front and back matter, while \myplain covers the fancier main matter (where you want the head rule even on the first page of chapters). This also avoids the need for a separate \chp command. \chapter will do what you want automatically. That means the bibliography automatically gets the styling in place at the time it is typeset since it uses \chapter. An alternative would be to tell TeX to use \chp as the sectioning for the bibliography. But I think this method is cleaner as it avoids the need to keep reapplying page styling throughout the document.

Note that it would be better to use geometry rather than adjusting margins etc. directly, that you need to make headheight at least 15pt, and that you should either drop the E options from fancyhdr's settings or use twoside. If you don't correct the headheight you will get inconsistent spacing at the top of the page.

UPDATE: To get the entire bibliography to use the same style as the first page, you could use this code:

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

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

\usepackage{fancyhdr}
\pagestyle{fancy}
% with this we ensure that the chapter and section
% headings are in lowercase.
\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}
    }}


%%--------------------------------------------------------------------------
%% DOCUMENT-SPECIFIC
%%--------------------------------------------------------------------------
\usepackage{tabularx}
\setlength{\oddsidemargin}{1.5cm}
\setlength{\topmargin}{-0.5cm}
\setlength{\textheight}{22.5cm}
\usepackage{acronym}
\usepackage{tocloft}
\usepackage{tocbibind}

\usepackage{filecontents,lipsum}
\begin{filecontents}{bibliography.bib}
@book{mybook1,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title     that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs. This is my book title that I stretch so that I get two pages of bibliography in order to show that the layout of the header differs},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}


\begin{document}
\plainplain

\frontmatter

\pagenumbering{Roman}
\tableofcontents
\clearpage

\chapter*{Abkürzungsverzeichnis}
\addcontentsline{toc}{chapter}{Abkürzungsverzeichnis}
\clearpage
\listoffigures
\clearpage
\listoftables
\clearpage

\myplain

\mainmatter % implies arabic pagenumbering
\chapter{Introduction}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\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{mybook4}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook9}
\nocite{mybook10}
\bibliographystyle{plainnat}
\bibliography{bibliography}
\end{document}

which produces:

Plainer bibliography

Related Question