2

I want to use this style in my fancy header:

Number of Chapter - Title of Chapter

i.e: 1 - Chapter Name

I use this command, but I view only the title of chapter.

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\fancyhead[C]{\nouppercase{\leftmark}}

How can I add the number of chapter ?

  • 2
    Please don't post fragments only. – user31729 Dec 23 '15 at 9:45
  • 1
    If "#1" is "1 - Chapter Name", it should be OK. Could you please post a minimal working example (MWE)? – Stephen Dec 23 '15 at 9:52
  • @Stephen: It depends on the documentclass what #1 might be, so you're right: we need a MWE – user31729 Dec 23 '15 at 9:53
  • I don't understand why the \leftmark should appear in the center of the head? – user31729 Dec 23 '15 at 9:55
  • 2
    @ChristianHupfer: Well, you are right, my comment was too short. What I wanted to say was: We do not know when/where/how \chaptermark is used with which argument, thus without MWE it is not clear where (or whether) to add \thechapter{} - (or to change something else). – Stephen Dec 23 '15 at 9:58
3

A trial: I assume that the left mark should be empty (or it should use another style at least).

The \chaptermark{} must be redefined at begin of the \document body, otherwise the standard definition of \@chapapp.~#1 etc. is used.

Please note that the chapter start page is using another pagestyle, i.e. plain.

\documentclass[oneside]{book}
\usepackage{fancyhdr}
\usepackage{blindtext}

\makeatletter

\AtBeginDocument{%
\def\chaptermark#1{%
  \markboth {\MakeUppercase{%
      \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
      \bfseries\thechapter~--~ %
      \fi
      \fi
      #1}}{}}%
}

\fancyhead[C]{\nouppercase{\leftmark}}
\fancyhead[R]{}
\fancyhead[L]{}

\makeatother

\pagestyle{fancy}
\begin{document}
\chapter{First one}
\blindtext[5]
\section{A section}
\chapter{Next chapter}
\blindtext[5]
\end{document}

resulting output from compiling the given code

0

titleps is also a viable option for setting titles of sectional units in the header.

enter image description here

\documentclass[oneside]{book}

\usepackage{titleps,lipsum}
\makeatletter
\newpagestyle{main}{
  \sethead{}      % left
          {\ifnum\c@secnumdepth>\m@ne\if@mainmatter
             \bfseries \thechapter~--~\chaptertitle
          \fi\fi} % center
          {}      % right
}
\g@addto@macro{\frontmatter}{\setheadrule{0pt}}
\g@addto@macro{\mainmatter}{\clearpage\setheadrule{.4pt}}
\makeatother

\pagestyle{main}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\sloppy% Just for this example
\chapter{First chapter}\lipsum[1-10]
\section{First section}\lipsum[1-10]
\section{Second section}\lipsum[1-10]
\section{Third section}\lipsum[1-10]
\section{Last section}\lipsum[1-10]
\chapter{Second chapter}\lipsum[11-20]
\section{First section}\lipsum[11-20]
\section{Second section}\lipsum[11-20]
\section{Third section}\lipsum[11-20]
\section{Last section}\lipsum[11-20]
\chapter{Third chapter}\lipsum[21-30]
\section{First section}\lipsum[21-30]
\section{Second section}\lipsum[21-30]
\section{Third section}\lipsum[21-30]
\section{Last section}\lipsum[21-30]
\chapter{Last chapter}\lipsum[31-40]
\section{First section}\lipsum[31-40]
\section{Second section}\lipsum[31-40]
\section{Third section}\lipsum[31-40]
\section{Last section}\lipsum[31-40]

\end{document}

It's good to review the quick guide as it show-cases the difference in input between fancyhdr and titleps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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