# fancyhdr: Title don't match title number

Some of my titles in the header don't match the number. This happens in page 3 in the example. What can be a solution or a work around?

\documentclass[twoside]{report}
\usepackage[ansinew]{inputenc}
\usepackage{fancyhdr}
\usepackage{lipsum}

\pagestyle{fancy}
\renewcommand{\subsectionmark}[1]{\markright{ #1}}
\renewcommand{\sectionmark}[1]{\markright{ #1}}
\renewcommand{\chaptermark}[1]{\markright{ #1}}

\rfoot{}
\cfoot{}
\renewcommand {\footrulewidth}{0pt}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\begin{document}

\chapter{Some chapter}

\section{Section I}
\lipsum[1]

\subsection{Subsection}
\lipsum[1-2]

\section{Section II}

\lipsum[1-2]

\subsection{Subsection One}
\lipsum[1-2]

\subsection{Subsection Two}
\lipsum[1-2]

\section{Section III}
\lipsum[1-2]

\subsection{Subsection for this section}
\lipsum[1-2]

\subsection{Another subsection}
\lipsum[1-2]

\end{document}

-
I guess that you have problem with the number based on your redefinition of the marks. –  Sigur Aug 27 '12 at 22:54
Exactly what information do you want to show in the headers for your document? –  Gonzalo Medina Aug 27 '12 at 23:25
It doesn't matter if it is the 1st or last chapter/section/subsection mentioned on a page. It only has to correspond to the chapter/section/subsection number. For instance on Page 3: It doesn't matter if in the header is "1.2.2 Subsection Two" or "1.3.0 Section III" –  DavChrFen Aug 28 '12 at 0:15

The section number must be added along with the title in \markright. Doing it in the header is too late.

The numbering is controlled by counter secnumdepth. Therefore the example also checks for the value of secnumdepth to avoid the case that a number is given in the header, but not in the text.

\documentclass[twoside]{report}
\usepackage[ansinew]{inputenc}
\usepackage{fancyhdr}
\usepackage{lipsum}

\pagestyle{fancy}
\renewcommand{\subsectionmark}[1]{%
\markright{%
\ifnum\value{secnumdepth}>1 %
\thesubsection~%
\fi
#1%
}%
}
\renewcommand{\sectionmark}[1]{%
\markright{%
\ifnum\value{secnumdepth}>0 %
\thesection~%
\fi
#1%
}%
}
\renewcommand{\chaptermark}[1]{%
\markright{%
\ifnum\value{secnumdepth}>-1 %
\thechapter~%
\fi
#1%
}%
}

\rfoot{}
\cfoot{}
\renewcommand {\footrulewidth}{0pt}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\begin{document}

\chapter{Some chapter}

\section{Section I}
\lipsum[1]

\subsection{Subsection}
\lipsum[1-2]

\section{Section II}

\lipsum[1-2]

\subsection{Subsection One}
\lipsum[1-2]

\subsection{Subsection Two}
\lipsum[1-2]

\section{Section III}
\lipsum[1-2]

\subsection{Subsection for this section}
\lipsum[1-2]

\subsection{Another subsection}
\lipsum[1-2]

\end{document}

-