[Tex/LaTex] Fancyhdr: Starred Sections

fancyhdr

Using fancyhdr package, this is what I do for each of my starred sections to print them at the header, e.g.:

\chapter*{{Foreword}} \markboth{{Foreword}}{Foreword}

and this what I have in the relative sections of my preamble:

\fancyhead[RE,RO]{\normalfont\scriptsize\thepage}
\fancyhead[LE]{\sffamily\small {My Book}
\fancyhead[LO]{\sffamily\small\\leftmark}
\fancyfoot{}

How can I automate it so that I do not have to do it manually for each starred section?

With my (very) limited knowledge, I tried,

\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}

From the fancyhr package manual, but it didn't work.

MWE: (Exported from LyX):

\documentclass[english]{book}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Minion Pro}
\setsansfont[Mapping=tex-text]{ITC Goudy Sans Std Book}
\usepackage[paperwidth=32pc,paperheight=48pc]{geometry}
\geometry{verbose,tmargin=9pc,bmargin=6pc,lmargin=5pc,rmargin=5pc}
\usepackage{fancyhdr}
\pagestyle{fancy}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{array}

\makeatletter

\usepackage{enumitem}
\usepackage{yfonts,color}
\usepackage{xcolor}
\usepackage{colortbl}
\arrayrulecolor{lightgray}
\usepackage{titlesec}

\makeatletter
\@addtoreset{footnote}{section}
\makeatother

\renewcommand{\thechapter}{\Roman{chapter}}
\titleformat{\chapter}[display]
{\normalfont\scshape\normalsize\filcenter}
{\normalsize\MakeUppercase{\thechapter} }
{0pt}
{\vspace{1pt}%
\normalsize}
\titlespacing{\chapter}{0pt}{20pt}{80pt}


\titleformat{\section}
{\itshape\normalsize\filright}
{\MakeUppercase{\thesection.}}{.5em}{}
\titlespacing*{\section}{0pt}{60pt}{60pt}

\fancyhead[RE,RO]{\normalfont\scriptsize\thepage}
\fancyhead[LE]{\sffamily\small {My Book}
\fancyhead[LO]{\sffamily\small\\leftmark}
\fancyfoot{}

\makeatletter
\renewcommand*{\cleardoublepage}{\clearpage\if@twoside \ifodd\c@page\else
\hbox{}%
\thispagestyle{empty}%
\newpage%
\if@twocolumn\hbox{}\newpage\fi\fi\fi}
\makeatother

Best Answer

You can add \markboth to the numberless version of \chapter. I reorganized the call to packages and the settings. Remove kantlipsum at the end that's only used for generating mock text in the example.

Note that emptypage avoids the redefinition of \cleardoublepage by hand.

This doesn't spare you from issuing \addcontentsline if you need the table of contents.

\documentclass[english]{book}

\usepackage[paperwidth=32pc,paperheight=48pc]{geometry}
\geometry{verbose,tmargin=9pc,bmargin=6pc,lmargin=5pc,rmargin=5pc}

\usepackage{fontspec}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{enumitem}
\usepackage{yfonts}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{titlesec}
\usepackage{chngcntr}
\usepackage{emptypage} % avoids redefining \cleardoublepage

\counterwithin*{footnote}{section}

\renewcommand{\thechapter}{\Roman{chapter}}

\titleformat{\chapter}[display]
  {\normalfont\scshape\normalsize\filcenter}
  {\normalsize\thechapter\ }
  {0pt}
  {\vspace{1pt}\normalsize}
\titleformat{name=\chapter,numberless}[display]
  {\normalfont\scshape\normalsize\filcenter}
  {}
  {0pt}
  {\vspace{1pt}\normalsize\addchaptertitle}
\titlespacing{\chapter}{0pt}{20pt}{80pt}

\newcommand{\addchaptertitle}[1]{%
  #1\markboth{#1}{#1}%
}

\titleformat{\section}
  {\itshape\normalsize\filright}
  {\thesection.}
  {.5em}
  {}
\titlespacing*{\section}{0pt}{60pt}{60pt}

\pagestyle{fancy}
\fancyhead[RE,RO]{\normalfont\scriptsize\thepage}
\fancyhead[LE]{\sffamily\small My Book}
\fancyhead[LO]{\sffamily\small\leftmark}
\fancyfoot{}

\arrayrulecolor{lightgray}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\setmainfont{Minion Pro}
\setsansfont{ITC Goudy Sans Std Book}

\usepackage{kantlipsum} % mock text

\begin{document}
\mainmatter

\chapter*{Introduction}

\kant[1-4]

\chapter{First}

\kant[5-8]

\end{document}

I'll show only page 3 which has the required header.

enter image description here

Related Question