2

I want to have unumbered chapters/sections appearing in the headers, so I used titlesec and defined a pagestyle that only displays the chapter/section title in the left/right marks. Now, I want that numbered chapters show as "Chapter X. Chapter title", for sections it would be "X.Y Section title". For unumbered it would be just "Chapter title" and "Section title".

My solution is to edit the \chaptermark and the \sectionmark like this :

\titleformat{\chapter}[display]
{\chaptermark{This is a test \thechapter. #1}\bfseries \Huge}
{\chaptertitlename \hspace{0ex} \thechapter}
{2ex}
{#1}

It works for chapters, unumbered chapters, unumbered sections but not for sections

MWE :

\documentclass{book}
\usepackage[pagestyles,explicit]{titlesec}
\newpagestyle{myps}[\scshape]{%
  \sethead[\footnotesize \oldstylenums{\thepage}][][\chaptertitle]% even
          {\sectiontitle}{}{\footnotesize \oldstylenums{\thepage}} % odd
}

\titleformat{name=\chapter , numberless}
 {\chaptermark{#1}\normalfont\bfseries\Huge \filcenter}
 {}
 {0pt}
 {#1}
 
\titleformat{\chapter}[display]
{\chaptermark{This is a test \thechapter. #1}\bfseries \Huge}
{\chaptertitlename \hspace{0ex} \thechapter}
{2ex}
{#1}

\titleformat{\section}
{\sectionmark{This is a test \thesection #1} \Large \bfseries \filcenter}
{\thesection}
{0.7em}
{#1}

\titleformat{name=\section , numberless}
{\sectionmark{#1} \Large \bfseries \filcenter}
{}
{0.7em}
{#1}

\begin{document}
\pagestyle{myps}
\chapter{Chapter title}
\section{Section title}
test
\newpage
test
\newpage
test
\end{document}

\chapter should display this enter image description here

\chapter*should display this enter image description here

\section should display this enter image description here

\section* should display this enter image description here

The issue :

\section displays this enter image description here

because the following code is not functionnal

\titleformat{\section}
{\sectionmark{This is a test \thesection #1} \Large \bfseries \filcenter}
{\thesection}
{0.7em}
{#1}

meanwhile all the others are functionnal.

6
  • titlesec sets marks too, and in the case of sectioning commands it actually sets two: one before and one after your manual \sectionmark and so your value is lost. Jun 13 at 17:32
  • @UlrikeFischer This is strange, do you know why it does this only with section and not chapter and others ?
    – Flowt
    Jun 13 at 17:40
  • chapters have a \newpage before, that makes their code easier. Jun 13 at 18:04
  • @UlrikeFischer but then why does it work with \section* ?
    – Flowt
    Jun 13 at 18:08
  • it doesn't issue an internal \sectionmark, so yours win. Jun 13 at 18:09

2 Answers 2

2

Your question doesn't really say how you call numberless chapters/sections. If you change the secnumdepth counter you can use the tools of titlesec. If you want it also for starred chapters and section you are out of luck: titlesec doesn't set the marks and there is no interface to reinsert them and patching titlesec is not trivial (even hyperref doesn't do it ...). You best bet is then to define new commands which issue the mark commands:

\documentclass{book}


\usepackage[pagestyles,explicit]{titlesec}
\newpagestyle{myps}[\scshape]{%
  \sethead[\footnotesize \oldstylenums{\thepage}][][\ifthechapter{Chapter~\thechapter~}{}\chaptertitle]% even
          {\ifthesection{Section~\thesection~}{}\sectiontitle}{}{\footnotesize \oldstylenums{\thepage}} % odd
}

\makeatletter
\titleformat{name=\chapter , numberless}
 {\normalfont\bfseries\Huge\filcenter}
 {}
 {0pt}
 {#1}
 
\titleformat{\chapter}[display]
{\bfseries \Huge}
{\chaptertitlename \hspace{0ex} \thechapter}
{2ex}
{#1}

\titleformat{\section}
{\Large \bfseries \filcenter}
{\thesection}
{0.7em}
{#1}

\titleformat{name=\section , numberless}
{\Large \bfseries\filcenter}
{}
{0.7em}
{#1}

\newcommand\starredchapter[1]{%
 \chapter*{#1}\chaptermark{#1}}

\newcommand\starredsection[1]{%
 \section*{#1\sectionmark{#1}}\sectionmark{#1}}
 
\begin{document}

\chapter{Chapter title}
\section{Section title}

test
\newpage
test
\newpage
test

\setcounter{secnumdepth}{-1}
\chapter{Nonumber Chapter title}
\section{Nonumber Section title}
test
\newpage
test
\newpage
test

\starredchapter{Starred Chapter title}
\starredsection{Starred Section title}

test
\newpage
test
\newpage
test
\end{document}
2
  • I edited to clarify how I want to call numberless chapters/sections
    – Flowt
    Jun 16 at 20:48
  • well as I wrote: it is difficult to get this to work with the starred versions, it would require internal patches. Use another command as shown in my answer. Jun 21 at 7:37
1

You could perhaps define a custom section command for numbered sections, which defines a \sectionmark just after the section:

\newcommand{\numberedsection}[1]{\section{#1}\sectionmark{This is a test \thesection\ #1}}

And change the style for the numbered section so:

\titleformat{\section}
{ \Large \bfseries \filcenter}
{\thesection}
{0.7em}
{#1}

For unnumbered sections, you use \section* normally.

You must log in to answer this question.

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