TransWikia.com

I need help in getting chapter title (without number) to display in header

TeX - LaTeX Asked on August 8, 2021

I want my two-sided pages to have different headers: on the left (even) side, the title of the book, and on the right (odd) side, the chapter title, without number. (This is pretty normal for non-technical books.)

I adapted my code from Werner’s answer to Dave’s 2015 question "Add Chapter Title to Header – without “Chapter 1” without success. The left (even) header is fine, but all I get is a separation line on the top of the right (odd) pages with no chapter title.

See minimum (non-)working example below. Is the problem in line 6? Do I need to rearrange some lines? I’ve searched sundry places including the fancyhdr documentation without success. Thanks in advance,
Artmancc

documentclass[12pt]{book}
usepackage{fancyhdr,lipsum}
pagestyle{fancy}
fancyhf{} % clear all header and footer fields
fancyhead[CE]{T~I~T~L~E~~~~O~F~~~~T~H~I~S~~~~B~O~O~K} 
fancyhead[CO]{leftmark}  %%% Is this the problem?
fancyfoot[LE,RO]{thepage}

renewcommand{chaptermark}[1]{markboth{MakeUppercase{#1}}{}}


begin{document}

chapter*{First Chapter}
lipsum[1-9]
clearpage{pagestyle{empty}cleardoublepage}  %% no header on blank left page
chapter*{Last Chapter}
lipsum[10-18]

end{document}

2 Answers

It is easy with titleps which comes with titlesec:

documentclass[12pt]{book}
usepackage{lipsum}
usepackage{titleps}
newpagestyle{myfancy}{%
sethead[][T~I~T~L~E~~~~O~F~~~~T~H~I~S~~~~B~O~O~K][]{}{MakeUppercase{chaptertitle}}{}
setfoot[thepage][][]{}{}{thepage}
}
renewpagestyle{plain}{%
setfoot[thepage][][]{}{}{thepage}
}

begin{document}
pagestyle{myfancy}

chapter*{First Chapter}
chaptermark{First Chapter}
lipsum[1-9]
clearpage{pagestyle{empty}cleardoublepage} %% no header on blank left page
chapter*{Last Chapter}
chaptermark{Last Chapter}
lipsum[10-30]

end{document} 

enter image description here

Answered by Bernard on August 8, 2021

The starred version chapter* does not execute chaptermark. So you have to add this command for each starred chapter:

documentclass[12pt]{book}
setlength{headheight}{15pt}
usepackage{lipsum}% only for dummy text
usepackage{emptypage}% page style empty on blank pages

usepackage{fancyhdr}
pagestyle{fancy}
fancyhf{} % clear all header and footer fields
fancyhead[CE]{T~I~T~L~E~~~~O~F~~~~T~H~I~S~~~~B~O~O~K} 
fancyhead[CO]{leftmark}
fancyfoot[LE,RO]{thepage}

renewcommand{chaptermark}[1]{markboth{MakeUppercase{#1}}{}}

begin{document}

chapter*{First Chapter}chaptermark{First Chapter}
lipsum[1-9]
chapter*{Last Chapter}chaptermark{Last Chapter}
lipsum[10-18]

end{document}

The result for this example is the same as above.

enter image description here

But if there are no numbered chapters, sections etc in the document, it would be better to change counter secnumpdepth and use the unstarred version chapter:

documentclass[12pt]{book}
setlength{headheight}{15pt}
usepackage{lipsum}% only for dummy text
usepackage{emptypage}% page style empty on blank pages

usepackage{fancyhdr}
pagestyle{fancy}
fancyhf{} % clear all header and footer fields
fancyhead[CE]{T~I~T~L~E~~~~O~F~~~~T~H~I~S~~~~B~O~O~K} 
fancyhead[CO]{leftmark}
fancyfoot[LE,RO]{thepage}

setcounter{secnumdepth}{-1}% only parts will be numbered

begin{document}

chapter{First Chapter}
lipsum[1-9]
chapter{Last Chapter}
lipsum[10-18]

end{document}

Answered by esdd on August 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP