Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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{}
\fancyhead[LO,RE]{\thesubsection \rightmark}
\fancyhead[RO,LE]{\thepage}
\renewcommand {\headrulewidth}{0pt}
\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}
share|improve this question
    
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
add comment

1 Answer

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{}
\fancyhead[LO,RE]{\rightmark}
\fancyhead[RO,LE]{\thepage}
\renewcommand {\headrulewidth}{0pt}
\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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.