1

I would like to know why the following code prints the header "CONTENTS" after chapter 1 (Intro) starts, instead of writing the correct chapter name "Intro" in the header (as it does for chapter 2 "Test"). Do I need to patch "titlesec"?

\documentclass{book}
\usepackage{titlesec}

\newpagestyle{main}{%
  \headrule
  \sethead{\chaptername\ \thechapter}{}{\chaptertitle}
  \setfoot{}{}{\thepage}
}

\begin{document}
\tableofcontents
\chapter*{intro}
b \newpage b
\chapter{test}
a \newpage b
\end{document}
3
  • @JamesT : right, but it doesn't change the problem: "contents" still appears in the header of chapter 1...
    – Watson
    Commented Feb 15, 2023 at 13:24
  • The comment above was to add \pagestyle{main}
    – Watson
    Commented Feb 16, 2023 at 7:39
  • 1
    This is the default behavior in LaTeX. You need a combination of \chaptermark and, very likely, \ifthesection. Commented Feb 17, 2023 at 15:51

2 Answers 2

2

By default, LaTeX doesn’t update headers and footers with starred sectioning commands. You have do it yourself with \chaptermark (avoid \markboth and \markright). In addition, you may want to remove the chapter numbers, which is accomplished with \ifthechapter. Don’t forget the package option pagestyles.

\documentclass{book}
\usepackage[pagestyles]{titlesec}

\newpagestyle{main}{%
  \headrule
  \sethead{\ifthechapter{\chaptername\ \thechapter}{}}{}{\chaptertitle}
  \setfoot{}{}{\thepage}
}

\begin{document}
\pagestyle{main}
\tableofcontents
\chapter*{intro}
\chaptermark{intro}
b \newpage b
\chapter{test}
a \newpage b
\end{document}
1

The problem is, that \chapter* does not reset or change the running head. KOMA-Script classes provide command \addchap resp. \addsec for this purpose:

\documentclass[headings=standardclasses,headsepline,cleardoublepage=headings]{scrbook}

% Following lines are only to make a similar heading like your `\newpagestyle` definition but using KOMA-Script
\usepackage{scrlayer-scrpage}
\automark{chapter}

\clearscrheadfoot
\renewcommand*{\chaptermark}[1]{%
  \markdouble{\MakeMarkcase{\Ifnumbered{chapter}{\chaptermarkformat}{}\hfill#1}}%
}
\ohead{\headmark}
\ofoot*{\pagemark}

\begin{document}
\tableofcontents
\addchap{intro}% Chapter with ToC entry and running head
b \newpage b
\chapter{test}
a \newpage b
\end{document}

enter image description here

With the standard classes, you would need an additional \markboth:

\documentclass{book}
\usepackage{titlesec}

\newpagestyle{main}{%
  \headrule
  \sethead{\chaptername\ \thechapter}{}{\chaptertitle}
  \setfoot{}{}{\thepage}
}

\begin{document}
\tableofcontents
\chapter*{intro}% does neither add a ToC entry nor change the running head
\markboth{into}{into}
b \newpage b
\chapter{test}
a \newpage b
\end{document}

enter image description here

But this does not work as expected using a titleps (titlesec) pagestyle. So here is an example without titlesec but using scrlayer-scrpage:

\documentclass{book}

% Following lines are only to make a similar heading like your `\newpagestyle` definition but using KOMA-Script
\usepackage[headsepline]{scrlayer-scrpage}
\automark{chapter}

\clearscrheadfoot
\renewcommand*{\chaptermark}[1]{%
  \markdouble{\MakeMarkcase{\chaptermarkformat\hfill#1}}%
}
\ohead{\headmark}
\ofoot*{\pagemark}

\begin{document}
\tableofcontents
\chapter*{intro}% does neither add a ToC entry nor change the running head
\markboth{into}{into}
b \newpage b
\chapter{test}
a \newpage b
\end{document}

enter image description here

3
  • Thanks! Your second solution does not work well if we add \pagestyle{main} (which I forgot...)
    – Watson
    Commented Feb 15, 2023 at 13:34
  • 1
    @Watson This is a titleps (loaded by titlesec) problem, but I do not know titleps good enough, to solve it. I always use scrlayer-scrpage and know a little bit about fancyhdr but never use titleps. See my third example, how scrlayer-scrpage could be used.
    – cabohah
    Commented Feb 15, 2023 at 13:51
  • Yes. Ideally I would like a solution which "fixes" this issue of titlesec. I could add this issue to github.com/jbezos/titlesec/issues
    – Watson
    Commented Feb 15, 2023 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .