0

I have created custom chapter headings:

\documentclass[final]{book}
\usepackage{lipsum}

% Custom macros 
\def \mychapter#1{%
\chapter*{\centering{\LARGE #1}\thispagestyle{empty}}%
\addcontentsline{toc}{chapter}{#1}%
}

\def \mychapterwithsubtitle#1#2{%
\chapter*{\baselineskip=20pt\centering{\LARGE #1}\\*\vspace*{0.3cm}\textnormal{\Large #2}\thispagestyle{empty}}%
\addcontentsline{toc}{chapter}{#1: #2}%
}

% Headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

% Document
\begin{document}
\mychapter{Some chapter}
\lipsum[1-10]
\mychapterwithsubtitle{Chapter Two}{Subtitle of this chapter}
\lipsum[11-30]
\mychapterwithsubtitle{Chapter Three}{Some other subtitle}
\lipsum[31-50]
\end{document}

Now, I would like to be able to put text of mychapter heading and name along with subtitles of mychapterwithsubtitles headings in the header (let's say left side of odd pages header).

I know that there are commands that could be used with fancyhdr package like \leftmark, \rightmark and \thechapter, but those only work for regular chapters and do not produce any result for starred versions of chapters which I tend to use.

Is there any way to put starred chapter name and number in header?

  • try adding marks as part of the definition: \markboth{#1}{#2} for \mychapterwithsubtitle. (this will position the marks as specified for the book class; if you want them somewhere else, then more work is required.) i don't know what you mean when you refer to "numbers"; by definition, starred chapters aren't numbered. ??? – barbara beeton Oct 24 '14 at 15:53
  • @barbarabeeton You are right regarding starred chapters; By 'numbers' I meant first part of chapter heading (#1). I have edited question to reflect that. Your solution indeed works the way you described it, though I hope someone comes up with solution which gives more control over position of those elements. – Rafal Oct 24 '14 at 16:06
  • Can't promise anything, but i'll have a look into this. I might use another package(s) tough. Not sure. – Johannes_B Oct 24 '14 at 20:01
  • @Johannes_B If you find any way to do that, I wil be beyond grateful. Thanks for trying. Situation described in question seems to be something that a lot of people might need, but there is no obvious way to do it that I am aware of. – Rafal Oct 24 '14 at 20:24
  • As i was reading the question again, i was a bit confused. The only thing to add is, as @barbarabeeton already mentioned, something like \markright{#1}. I made a DIY-chapter anyways. Please have a look at it. – Johannes_B Oct 25 '14 at 11:44
1

As Barbara mentioned in her comments, you can use mechnaisms like \markboth or \markright. I just added the mark to your definitions.

I also defined a command xchapter that takes an optional argument that will be typeset as the subtitle. The command in general patches the original \@makeschapterhead, makes the starting page empty and most important, clears the headers of any pre-existing material.

Since this patches the original book-chapter-definition, it is not compatible with KOMA-script.

\documentclass[final]{book}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{lipsum}


\def\mychapter#1{%
\chapter*{\centering{\LARGE #1}\thispagestyle{empty}}
\addcontentsline{toc}{chapter}{#1}%
\markright{#1}%
}

\def \mychapterwithsubtitle#1#2{%
\chapter*{\baselineskip=20pt\centering{\LARGE#1}\\*\vspace*{0.3cm}\textnormal{\Large#2}\thispagestyle{empty}}%
\markright{#1: #2}%
\addcontentsline{toc}{chapter}{#1: #2}%
}

\makeatletter
\DeclareDocumentCommand{\xchapter}{ m o }{
    \begingroup
    \ProvideDocumentCommand{\schapterheadendvskip}{}{\vskip 40\p@}
    \patchcmd{\chapter}{plain}{empty}{}{}
    \patchcmd{\@makeschapterhead}{\Huge}{\LARGE}{}{}
    \patchcmd{\@makeschapterhead}{\raggedright}{\centering}{}{}
    \patchcmd{\@makeschapterhead}{\vskip 40\p@}{\schapterheadendvskip}{}{}
    \clearpage\markboth{}{}
    \IfNoValueTF{#2}{
    \chapter*{#1}
    \addcontentsline{toc}{chapter}{#1}
    \markright{#1}
}{
    \renewcommand{\schapterheadendvskip}{\vskip
        15\p@{\normalfont\Large\centering#2\par\nobreak} \vskip 20\p@}
    \chapter*{#1}
    \addcontentsline{toc}{chapter}{#1:\space#2}
    \markright{#1: #2}
}
\endgroup
}
\makeatother

\usepackage{showframe}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LO]{\rightmark}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}


\setlipsumdefault{1-24}
\begin{document}
\tableofcontents
\mychapter{A chapter without a subtitle}
\lipsum
\mychapterwithsubtitle{Chapter with many many little
ducklings}{And a subtitle}
\lipsum
\mychapterwithsubtitle{no ducklings}{but some puppies}
\lipsum
\xchapter{There once was a little duckling}[and it was sooo cute]
\lipsum
\xchapter{The first law of thermodynamics}
\lipsum
\chapter*{Look at the header on the next odd page}
\lipsum
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.