31

how to display current section title in header insted of chapter title?

I'm using book and fancychap

MWE

\documentclass [11pt,oneside,final]{book}
\usepackage[nohints]{minitoc}
\usepackage[round]{natbib}
\usepackage{longtable}
\usepackage[font={footnotesize,it}]{caption}
\usepackage{bookman} 
\usepackage[T1]{fontenc} 
\usepackage{setspace}
\doublespacing
%\onehalfspacing
\usepackage{multirow}
\usepackage[left=35mm,top=30mm,right=30mm,bottom=30mm]{geometry}
\pagenumbering{Roman}
\usepackage[small,compact]{titlesec}
\usepackage[Lenny]{fncychap}
\usepackage{arabtex}
\usepackage{cp1256}
\setcode{cp1256}
\usepackage{float}
\usepackage[final]{graphicx}
\usepackage{epstopdf}
\usepackage{array}

\setarab
\raggedbottom
\begin{document}
\chapter{introduction} \thispagestyle{empty}

\section{one}
bla bla bla bla bla

\section{two}
bla bla bla bla 

\end{document}
khaled
  • 1,435

3 Answers3

42

You can use fancyhdr.

\documentclass{book}

\usepackage{fancyhdr}
\usepackage{lipsum}% just to generate text for the example

\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\rightmark}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}

\begin{document}
\chapter{Chapter title}
\section{section title}

\lipsum[1-20]

\end{document}
  • \pagestyle{fancy} sets the page style to the style delivered and editable with fancyhdr.
  • \fancyhf{} sets all head and foot elements empty.
  • \fancyhead[L]{\rightmark} sets the left head element to \rightmark, which contains the current section (\leftmark is the current chapter).
  • \fancyhead[R]{\thepage} sets the right head element to the page number.
  • \renewcommand{\headrulewidth}{0pt} lets the head rule disappear.

Possible selectors for the optional argument of \fancyhead/\fancyfoot are L (left), C (center) or R (right) for the position of the element and E (even) or O (odd) to distinguish even and odd pages. If you omit E/O the element is set for all pages.

Tobi
  • 56,353
  • i have applied this solution. the problem it put the section title on the right side of the page with underline and i need it in the left without underline . Also it move the page number from top right of page to the end of the page and i need it to stay at top right. – khaled Jan 01 '14 at 21:48
  • @user1200219. You didn’t say that ;-) I change my example, but you could have find the solution yourself by reading the manual ;-) … – Tobi Jan 01 '14 at 23:05
  • it is fine now. but the page number need to be in in most right if it can because it is in some how near to middle? also, is it possible to include subsection too not just section ? – khaled Jan 02 '14 at 16:31
  • The page number is exactly flushed right with the text – sorry I don’t understand where you want it to be … In scrpage2 you can use \automark{subsection} but I don’t know something similar for fancyhdr. – Tobi Jan 02 '14 at 16:45
  • how to fix this warning Package Fancyhdr Warning: \headheight is too small (12.0pt): Make it at least 13.59999pt. – khaled Jan 03 '14 at 17:23
  • Just do what the warning suggest: increase the head height, e.g. with geometry. – Tobi Jan 03 '14 at 17:26
  • This did not give me section titles, only section numbers. – sh37211 Feb 22 '21 at 23:53
16

Updated Answer

Please use scrlayer-scrpage instead …

\documentclass{book}

\usepackage{scrlayer-scrpage} \usepackage{lipsum}% just to generate text for the example

\pagestyle{scrheadings} \clearpairofpagestyles \ohead{\rightmark} \cfoot[\pagemark]{\pagemark}

\begin{document} \chapter{Chapter title} \section{section title}

\lipsum[1-20]

\end{document}

Except for \clearscrheadfoot{} which is replaced by \clearpairofpagestyles nothing has changed for the user.

You may also like to read further in my german blog: TeX-Beispiel des Monats „Layout II: Kopf- und Fußzeilen“.


Original Answer

You can use scrpage2 from KOMA-Script bundle.

\documentclass{book}

\usepackage{scrpage2} \usepackage{lipsum}% just to generate text for the example

\pagestyle{scrheadings} \clearscrheadfoot{} \ohead{\rightmark} \cfoot[\pagemark]{\pagemark}

\begin{document} \chapter{Chapter title} \section{section title}

\lipsum[1-20]

\end{document}

  • \pagestyle{scrheadings} sets the page style to the style delivered and editable with scrpage2.
  • \clearscrheadfoot{} sets all head and foot elements empty.
  • \ohead{\rightmark} sets the outer head element (o) to \rightmark, which contains the current section (\leftmark is the current chapter).
  • \cfoot[\pagemark]{\pagemark} sets the page number to the center foot element (c). The optimal argument defines the plain style (used on chapter starting pages).

This image shows all possible elements.

scrpage2 head/foot elements
source: scrguien.pdf, p. 198

Tobi
  • 56,353
  • Notes: 1) In one-sided documents (as in scrartcl by default) there are only right/odd pages, so only commands seen on the right side of the image are applied. 2) From KOMA-Script, version 3.12, on there’s a successor package named scrlayer-scrpage, that is as far as I see downwards compatible to scrpage2. – Speravir Jan 02 '14 at 00:17
  • @Speravir: Thanks for the notes :-) I’m aware of the new package but didn’t had the time yet to inspect it :-) – Tobi Jan 02 '14 at 00:47
  • 1
    Trying this solution had absolutely no effect on my article. Perhaps it's only for books? – sh37211 Feb 22 '21 at 23:44
  • The difference between books and articles is, that the latter are single-paged by default and books in double pages, which means that an article makes no difference between even and odd pages. For an article all pages are considers right/odd. – Tobi Feb 23 '21 at 13:50
1

I would use labels at the sections and reference them with \nameref{}. At every new section I think you must redefine your header with the new one...

Alex44
  • 573
  • 6
  • 18
  • 1
    I’d say it’s preferable to use the automatics LaTeX provides ;-) – Tobi Dec 30 '13 at 14:14
  • The other way I don't know till now. But your the solution is very nice and much more nice than my :) – Alex44 Dec 30 '13 at 14:35
  • The \nameref mechanism is useful to but more for references and not of automating head/foot elements … :-) – Tobi Dec 30 '13 at 14:42