Discussion:
Headers in Table of Contents using fancyhdr
(too old to reply)
Tariq
2008-05-02 20:56:07 UTC
Permalink
Hi TeXperts,

I have been deeply frustrated with not being able to get the headers
in the "Table of Contents" in my book to conform with the headers in
the rest of the book. Briefly, I am using \documentclass{book} and I
want to have headers that look like those in the TeXBook (i.e., in
italic, except that mine are set in bold italic and I have section
title alternating with chapter title). I have used fancyhdr package
and followed the advice in its documentation to get the results I
wanted. I am suing the following code
%
%
\pagestyle{fancy} \fancyhf{}
\renewcommand\headrule{\hrule height 0pt}
\pagestyle{fancy}
\fancyheadoffset[LE,RO]{40pt}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\fancypagestyle{plain}{%
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
}
%%
%% To get the headers in italic for chapter and section, I have the
following code
\renewcommand{\chaptermark}[1]{%
\markboth{{\spaceskip = .22em plus 0.02em minus 0.02em \itshape
\chaptername
\ \thechapter\ \ \ $\bullet$ \ \ #1}}{}}
%
\renewcommand{\sectionmark}[1]{%
\markright{{\spaceskip = .22em plus 0.02em minus 0.02em \itshape
Section
\ \thesection\ \ \ $\bullet$ \ \ #1}}{}}
%%


To get the same look for the headers in the frontmatter (such as
Preface, Acknowledgments etc), I have something like the following

\chapter*{Preface\markboth{\itshape P\MakeLowercase{reface}}{}}


This all seems to work quite well for everything **EXCEPT** for the
Table of Contents. I have tried the following:

\tableofcontents\markboth{\itshape T\MakeLowercase{able of} C
\MakeLowercase{ontents}}{}

On the surface, it looks like the code for the Preface above, and it
seems to conform to the rest of the document **BUT** fails when the
Table of Contents is longer than 2 pages!!! The headers in the Table
of Contents are all set in uppercase (not in italics). Is there a way
to correct this? This sort of things makes my book appear nonuniform
and nonprofessional. I am sure there is some workaround but I have
nearly given up on how to do it. I would greatly appreciate any advice
and suggestions. BTW, I am sure this problems plagues my List of
Figures and List of Tables also, but I have not been able to test it
because the list of figures and tables does not go beyond one or two
pages yet.

Regards,

Tariq
Ulrike Fischer
2008-05-03 09:46:58 UTC
Permalink
Post by Tariq
Hi TeXperts,
I have been deeply frustrated with not being able to get the headers
in the "Table of Contents" in my book to conform with the headers in
the rest of the book.
Don't sent a lot of code snippets. Make a complete, minimal example that
demonstrates your problem. You can force a \newpage in the table of
contents by using \addtocontents{toc}{\protect\newpage} in your document
between two \chapter or \section.
--
Ulrike Fischer
Tariq
2008-05-03 16:58:25 UTC
Permalink
Post by Ulrike Fischer
Post by Tariq
Hi TeXperts,
I have been deeply frustrated with not being able to get the headers
in the "Table of Contents" in my book to conform with the headers in
the rest of the book.
Don't sent a lot of code snippets. Make a complete, minimal example that
demonstrates your problem. You can force a \newpage in the table of
contents by using \addtocontents{toc}{\protect\newpage} in your document
between two \chapter or \section.
--
Ulrike Fischer
Hi Ulrike

I'll attempt to make a minimal example and have it posted shortly.
Regards,
Tariq
Tariq
2008-05-03 18:16:54 UTC
Permalink
Post by Ulrike Fischer
Make a complete, minimal example that
demonstrates your problem. You can force a \newpage in the table of
contents by using \addtocontents{toc}{\protect\newpage} in your document
between two \chapter or \section.
Hi Ulrike

Here is a sort of minimal example that clearly demonstrates my
problem. For your kind consideration, please note the following issues
after you compile this example
(1) If you keep the Table of Contents to within two pages, then the
header is in italics, like the rest of the document PROVIDED I have

\markboth{\itshape T\MakeLowercase{able of} C\MakeLowercase{ontents}}
{}

code attached to \tableofcontents line. Without this code, the headers
are in uppercase.

(2) When the Table of Contents is longer than two pages, even the
above code does not help and the headers for Table of Contents are all
in uppercase. In addition, they appear only on even pages and not on
the odd pages. Also note that in my example, the last page (which is
even) has the header Table of Contents in italalic but has no
contents! Seems to me that the appearance of contents on a page change
the format of the headers. I would like to have the header Table of
Contents on both odd and even pages in italic.

I have a feeling that similar situation would happen with List of
Figures and List of Tables; so a general solution would be needed.

I would appreciate any advice and suggestion. Regards,

Tariq

%% Example code follows
%%
\documentclass[twoside,openright]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{fancyhdr}
\fancypagestyle{plain}{\fancyhf}%
\pagestyle{fancy} \fancyhf{} %
\renewcommand\headrule{\hrule height 0pt}
\pagestyle{fancy}
\fancyheadoffset[LE,RO]{40pt}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\fancypagestyle{plain}{%
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
}

%Following macros are from fancyhdr package %%%%
\renewcommand{\chaptermark}[1]{%
\markboth{{\spaceskip = .22em plus 0.02em minus 0.02em \itshape
\chaptername
\ \thechapter\ \ \ $\bullet$ \ \ #1}}{}} %
%
\renewcommand{\sectionmark}[1]{%
\markright{{\spaceskip = .22em plus 0.02em minus 0.02em \itshape
Section
\ \thesection\ \ \ $\bullet$ \ \ #1}}{}}
\setcounter{tocdepth}{4}
\author{\scshape Tariq Perwez, Ph.D.}
\title{MATHEMATICS FOR\\MOLECULAR BIOLOGY}
\date{1874}
%%%
\begin{document}
\frontmatter
\maketitle

\tableofcontents%
\markboth{\itshape T\MakeLowercase{able of} C\MakeLowercase{ontents}}
{}

\mainmatter
%%%%%%%%
\chapter{Numbers and Measurements}
\section{Introduction}
\newpage
\section{Dealing with significant digits}
\subsection{Rounding off significant digits in calculations}
\section{Exponents and scientific notation}
\newpage
\subsection{Expressing numbers in scientific notation}
\section{Metric Prefixes}
\section{Conversion factors and canceling terms}
\section{Dimensions and Units}
\subsection{Converting Between Different Units}
%
\addtocontents{toc}{\protect\newpage}
%
\chapter{Dilution of Solutions, Mixtures, and Media}
\section{Introduction}
\section{Calculating Dilution}
\newpage
\section{Concentrations}
\section{Preparing Percent Solutions}
\subsection{Diluting Percent Solutions}
\section{Moles and Molecular Weight}
\subsection{Molarity}
\newpage
\subsection{Converting Molarity to Percent}
\subsection{Converting Percent to Molarity}
\subsection{Normality}
\section{pH of Solutions}
\subsection{Henderson-Hasselbalch Equation}
%
\addtocontents{toc}{\protect\newpage}
%
\chapter{Probability in Prokaryotic Genetics}
\section{Introduction}
\section{Probability: A Definition}
\newpage
\subsection{Dependent Versus Independent Events}
\section{Application of Binomial Expansion}
\subsection{Permutation and Combination}
\newpage
\subsection{Binomial Expansion}
\subsection{Poisson Distribution}
\section{Problems}
%
\end{document}
%%%-------------------------------------------------------------
Ulrike Fischer
2008-05-04 11:57:41 UTC
Permalink
Post by Tariq
Post by Ulrike Fischer
Make a complete, minimal example that
demonstrates your problem. You can force a \newpage in the table of
contents by using \addtocontents{toc}{\protect\newpage} in your document
between two \chapter or \section.
Here is a sort of minimal example that clearly demonstrates my
problem.
Yes "sort of". Why are you defining \chaptermark and \sectionmark twice?
And why are you defining \fancypagestyle{plain} twice?
Post by Tariq
\tableofcontents%
\markboth{\itshape T\MakeLowercase{able of} C\MakeLowercase{ontents}}
{}
You are issuing a \mark-command *after* the table of contents, so at
best it can affect the last page of the table of contents.

Either use a better class, which hasn't like book the header of the toc
hard coded.

Or copy the definition of \tableofcontents from book.cls to your
preamble (between \makeatletter ...\makeatother) and change it.

Or insert the marking command at the start of the table of contents with
\addtocontents:

\tableofcontents
\addtocontents{toc}{\protect\markboth{\protect\itshape Table of
contents}{}}
--
Ulrike Fischer
Tariq
2008-05-04 17:11:01 UTC
Permalink
Post by Ulrike Fischer
Post by Tariq
Post by Ulrike Fischer
Make a complete, minimal example that
demonstrates your problem. You can force a \newpage in the table of
contents by using \addtocontents{toc}{\protect\newpage} in your document
between two \chapter or \section.
Here is a sort of minimal example that clearly demonstrates my
problem.
Yes "sort of". Why are you defining \chaptermark and \sectionmark twice?
And why are you defining \fancypagestyle{plain} twice?
Post by Tariq
\tableofcontents%
\markboth{\itshape T\MakeLowercase{able of} C\MakeLowercase{ontents}}
{}
You are issuing a \mark-command *after* the table of contents, so at
best it can affect the last page of the table of contents.
Either use a better class, which hasn't like book the header of the toc
hard coded.
Or copy the definition of \tableofcontents from book.cls to your
preamble (between \makeatletter ...\makeatother) and change it.
Or insert the marking command at the start of the table of contents with
\tableofcontents
\addtocontents{toc}{\protect\markboth{\protect\itshape Table of
contents}{}}
--
Ulrike Fischer
Hi Ulrike

Thanks a bundle for your help. Very importantly, I now understand
better the logic behind the code. All of your suggestions are
valuable. I just feel a bit afraid to switch the class for my project
right now (but will do it for my next project). I have played around
with copying the code from the book.cls. However, your last suggestion
is probably the most straightforward and I am using that for now. I
really appreciate your help. Regards,

Tariq

Loading...