Page LayoutChapter title in header

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
AndyBern
Posts: 2
Joined: Fri Nov 21, 2014 7:08 pm

Chapter title in header

Post by AndyBern »

My first post...

I'm in the final stages of writing a book. I used MS Word, but a week or so ago I did a full install of MiKTeX 2.9 so I can learn Latex and publish the book in a more professional-looking manner. I've been programming computers for decades in various languages, so Latex should be pretty easy to learn. However, I'm having problems with things that I think should be pretty easy to do. For example...

I'm using the fancyhdr package and trying to put the chapter title in the header. So far, I'm having no success. Everything I've read online tells me \leftmark should be the chapter title, but it doesn't return anything. (I've also tried \thechapter.) This is where I'm at at the moment...

Code: Select all

\documentclass[12pt,twoside]{book}
\usepackage{fancyhdr}
\setlength{\headheight}{15pt}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\fancyhead{}
\fancyfoot{}
\fancyhead[CO]{Book Title}
\fancyhead[CE]{\leftmark}
\fancyfoot[CO]{\thepage}
\fancyfoot[CE]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\AtBeginDocument{\fancyhfoffset{0pt}}

\begin{document}
\chapter*{First Chapter}

Lorem ipsum dolor sit amet, consectetur ...
Can anyone tell me what I'm doing wrong?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Chapter title in header

Post by Johannes_B »

Hi and welcome,

little hint, you can save the time of helpers by making the example compilable and showing the problem ;-)

Unnumbered chapters don't appear in the table of contents and \chaptermark is not set. You need to call it by yourself.

Code: Select all

\documentclass[12pt,twoside]{book}
\usepackage{fancyhdr}
\setlength{\headheight}{15pt}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\fancyhead{}
\fancyfoot{}
\fancyhead[CO]{Book Title}
\fancyhead[CE]{\leftmark}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\AtBeginDocument{\fancyhfoffset{0pt}}
\usepackage{blindtext}
\begin{document}
\chapter{First Chapter}
\blindtext[12]
\chapter*{unnumbered, look at the head}
\blindtext[12]
\chapter*{unnumbered, look at the head}
\chaptermark{what is here?}
\blindtext[12]


\end{document}
If you want an additional entry to the toc, you can use package unnumberedtotoc
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
AndyBern
Posts: 2
Joined: Fri Nov 21, 2014 7:08 pm

Re: Chapter title in header

Post by AndyBern »

Ok, thanks! That was it.
Post Reply