2

Closely Related:
- Create Separate Footer and Header for Chapter Title and Content Pages?

1. Use Case - Decoupling Layout from Content:

Content Authors will provide TeX content via a web portal, which will likely include \chapter{Chapter Title} and most probably footnote, math, image, and other TeX syntax.

However - it is often the case that the default chapter title page and chapter content page styles could be wrongfully specified by these authors, or get corrupted through other imports (\input, \include, \usepackage, etc.)

Clarification: A Chapter Title Page is the first page of a chapter. Chapter Content Pages are the remaining content pages of the chapter. This has nothing to do with a Table of Contents, but should not effect the ToC.


2. Question:

With the Memoir, Book, or other document classes, is it possible to do what the related question is doing with the \xpatchcmd?

Is it possible to define and apply custom classes, only once, to affect all Chapter Title Pages and Chapter Content Pages, throughout an entire document, without all of the code repetition as seen in similar questions?

Goals:

  1. To be able to define headers and footers for Chapter Title Pages and Chapter Content Pages separately, (which the current answer is limited from doing).
  2. To eventually have separate custom styles for each Part in a book, (without worrying about default styles being corrupted.
  3. Less code repetition.
  4. Could override unwelcome customizations in imported documents.
  5. To blacklist \xpatchcmd and any other patch commands from being used.

3. Code Example:

\documentclass{memoir}

\usepackage{lipsum}

\makepagestyle{chapter}
    \makeheadrule{chapter}{\textwidth}{\normalrulethickness}
    \makefootrule{chapter}{\textwidth}{\normalrulethickness}{\footruleskip}
    \makeoddhead {chapter}{left header}{odd header}{right header}
    \makeoddfoot {chapter}{left footer}{odd footer}{right footer}
    % ***** Added in Even Page Headers and Footers to show on Chapter Content Pages
    \makeevenhead {chapter}{left header}{even header}{right header}
    \makeevenfoot {chapter}{left footer}{even footer}{right footer}


    \makepagestyle{alternate}
    \makeheadrule{alternate}{\textwidth}{\normalrulethickness}
    \makefootrule{alternate}{\textwidth}{\normalrulethickness}{\footruleskip}
    \makeoddhead {alternate}{left header}{alternate odd header}{right header}
    \makeoddfoot {alternate}{left footer}{alternate odd footer}{right footer}
    \makeevenhead {alternate}{left header}{alternate even header}{right header}
    \makeevenfoot {alternate}{left footer}{alternate even footer}{right footer}


\begin{document}

    % ***** What Memoir Code could go here, to override all
    % ***** chapter title and content pagestyles?
    % ***** How to specify all chapter Title and Content Pages
    % ***** ... without having to provide an actual chapter.
    % ***** ... \xpatch can do this, but does Memoir have a way?

    \chapter{Non-Styled Chapter}
    \chapterprecis{Ideally the only code would be the \\ include command.  Authors will only have TeX code specific to their own content - with no knowledge of current styles, etc.  There would be no "\\ pagestyle" commands in these \\ include -ed files.  Here, chapter content pages have no custom headers and footers.  }
    % \include("./Content/MyContent.text")
    \sloppy\lipsum

    \chapter{Test Chapter With required \\ pagestyle command}
        \chapterprecis{Required to use inner Styling to get Headers and Footers.}
        % **** Chapter Content Pages Need this \pagestyle command?
        \pagestyle{chapter}
        \sloppy\lipsum

    % ***** Every \chapter tag would reqire \chapterstyle, etc.
    % ***** Chapter Content will actually be pulled in via \include.
    \chapterstyle{bianchi}
    \chapter{Testing Bianchi Chapter Style}
        \chapterprecis{Built in Chapter Styles have no impact on headers and footers.%
        Have to resort to including \\ pagestyle in order to override possible%
        corruption of default \\ pagestyle\{chapter\} }

        \pagestyle{alternate}
        \sloppy\lipsum

    % ***** Bianchi Chapter Test 2, Without Style Tags
    \chapter{Non-Styled Chapter}
        \chapterprecis{Chapter Without Any Style Commands. Inherits globally altered changes.  This is what I am hoping to avoid.}
        \sloppy\lipsum

\end{document}
13
  • 1
    Your example doesn't show at all that you are trying to do different things for the first pages of different kinds of chapters. How are people supposed to guess that? Werner's crystal ball is evidently in excellent shape, but we don't all have crystals of similarly quality.
    – cfr
    Feb 2, 2018 at 23:15
  • xpatch is not replacing anything in the whole body. It changes the definition of a macro. The macro can be used multiple times.
    – Johannes_B
    Feb 4, 2018 at 7:14
  • it isn't very clear what problem you are trying to solve. It seems you have authors writing separate chapters to be included into a main document. They can only reference page styles that have been defined, so it is up to you to just provide the needed styles. (I deleted comments on the earlier version) Feb 4, 2018 at 20:51
  • I have no idea what you mean by "corruption by all imports" as I said I would only expect a chapter that is imported to use page styles defined in the document preamble, and if you control the document preamble then there should be no issue. Feb 4, 2018 at 21:05
  • do you just mean that you do not want to allow \chapterstyle{bianchi} if so \renewcommand\chapterstyle[1]{} would disable this command, but I can not really guess. Ultimately it is a social issue that you need to control by author guidelines not by technology, any latex input can redefine any part of latex. Feb 4, 2018 at 21:08

1 Answer 1

6

\xpatchcmd (from xpatch) has the same format/interface than its counterpart \patchcmd (from etoolbox):

\xpatchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}

\xpatchcmd searches for <search> in <cmd> and replaces it with <replace>. If this search-and-replace is successful, it executes <success>, otherwise it executes <failure>. In your code example (after removing the comments),

\xpatchcmd{\chapter}{\thispagestyle{chapter}}{}{}{}

searches for \thispagestyle{chapter} inside \chapter and replaces it with nothing. The <success> and <failure> branches are also left blank for brevity. Effectively, the above command removes \thispagestyle{chapter} from \chapter.

Secondly, you're confusing \chapterstyle with \pagestyle. They're different. A chapter style (defined using \makechapterstyle) is used with a call to \chapterstyle and defines the way the chapter heading/title is set, not the chapter page header/footer. The latter is called a page style and is created using \makepagestyle and used/set via \thispagestyle (for the current page only) or \pagestyle (for pages form the current on onward).

Here is a demonstration of the veelo chapter style, for example:

enter image description here

% ...
\setcounter{chapter}{3}% Just for this example
\chapterstyle{veelo}
\chapter{Demonstration of the veelo \\ chapter style}
\sloppy\lipsum
%...

The way the heading - Chapter 4 - and title - Demonstration of the veelo \\ chapter style - is set has been changed, but the page style is still chapter.

Here is a demonstration of applying the ChapterPage page style to the first pages of a \chapter under the madsen chapter style:

enter image description here

\documentclass{memoir}

\usepackage{lipsum,graphicx}

\makepagestyle{ChapterPage}
\makeheadrule{ChapterPage}{\textwidth}{\normalrulethickness}
\makefootrule{ChapterPage}{\textwidth}{\normalrulethickness}{\footruleskip}
\makeoddhead {ChapterPage}{left header}{centre header}{right header}
\makeoddfoot {ChapterPage}{left footer}{centre header}{right header}

\begin{document}

\tableofcontents

\clearpage
\copypagestyle{chapter}{ChapterPage}
\setcounter{chapter}{3}% Just for this example
\chapterstyle{madsen}
\chapter{Demonstration of the madsen \\ chapter style}
\sloppy\lipsum

\end{document}

By default the first page of a \chapter is given \thispagestyle{chapter} (see memoir documentation, section 7.2 Page styles) and the chapter page style is a copy - actually an alias - of plain (see section 7.3 Making headers and footers).

You first have to fully create the chapter page style, either as a completely new style (via \makepagestyle{chapter}) or as a copy of some other style (like \copypagestyle{chapter}).

Then you can change the style to suit your needs:

\makepagestyle{chapter}% Officially create the chapter page style
\makeheadrule{chapter}{<width>}{<thickness>}% Header rule specification
\makefootrule{chapter}{<width>}{<thickness>}{<skip>}% Footer rule specification
\makeevenhead{chapter}{<left>}{<centre>}{<right>}% Even header
\makeoddhead {chapter}{<left>}{<centre>}{<right>}% Odd header
\makeevenfoot{chapter}{<left>}{<centre>}{<right>}% Even footer
\makeoddfoot {chapter}{<left>}{<centre>}{<right>}% Odd footer

Here is a complete minimal example highlighting the procedure:

enter image description here

\documentclass{memoir}

\usepackage{lipsum}

% This creates (and overrides) the chapter page style - set on the 
% first page of a \chapter
\makepagestyle{chapter}
\makeheadrule{chapter}{\textwidth}{\normalrulethickness}
\makefootrule{chapter}{\textwidth}{\normalrulethickness}{\footruleskip}
\makeoddhead {chapter}{left header}{chapter odd header}{right header}
\makeoddfoot {chapter}{left footer}{chapter odd footer}{right footer}
\makeevenhead{chapter}{left header}{chapter even header}{right header}
\makeevenfoot{chapter}{left footer}{chapter even footer}{right footer}

% This is everything else - set on "chapter content" pages
\makepagestyle{alternate}
\makeheadrule{alternate}{\textwidth}{\normalrulethickness}
\makefootrule{alternate}{\textwidth}{\normalrulethickness}{\footruleskip}
\makeoddhead {alternate}{left header}{alternate odd header}{right header}
\makeoddfoot {alternate}{left footer}{alternate odd footer}{right footer}
\makeevenhead{alternate}{left header}{alternate even header}{right header}
\makeevenfoot{alternate}{left footer}{alternate even footer}{right footer}

\pagestyle{alternate}

\begin{document}

\sloppy% Just for this example

\chapter{Non-Styled Chapter}
\chapterprecis{Ideally the only code would be the \texttt{\string\include} command.
  Authors will only have TeX code specific to their own content - with no knowledge of current styles, 
  \textit{etc.} There would be no ``\texttt{\string\pagestyle}'' commands in these \texttt{\string\include}-ed files.
  Here, chapter content pages have no custom headers and footers.}
\lipsum

\chapter{Test Chapter With required \texttt{\string\pagestyle} command}
\chapterprecis{Required to use inner Styling to get Headers and Footers.}
\lipsum

\chapterstyle{bianchi}
\chapter{Testing Bianchi Chapter Style}
\chapterprecis{Built in Chapter Styles have no impact on headers and footers.%
  Have to resort to including \texttt{\string\pagestyle} in order to override possible%
  corruption of default \texttt{\string\pagestyle\string{chapter\string}}}
\lipsum

\chapter{Non-Styled Chapter}
\chapterprecis{Chapter Without Any Style Commands. Inherits globally altered changes.
  This is what I am hoping to avoid.}
\lipsum

\end{document}
9
  • @elikakohen: I added some more detail towards the end to show how you can have the \tableofcontents with a different style than the other \chapters.
    – Werner
    Feb 2, 2018 at 5:58
  • 1
    @elikakohen: You are confusing chapter styles with page styles, as far as I can tell. I've elaborated on this in the first part of my answer, as well as address your question about \xpatchcmd.
    – Werner
    Feb 3, 2018 at 21:06
  • 1
    @elikakohen: The way you're using it - with \makeoddhead, \makeoddfoot, ... as part of \makechapterstyle - does make it seem like the header/footer forms part of the chapter style. However, your use is incorrect. Sure it works, but that is only because \chapterstyle{ChapterHead} calls whatever is stored inside the ChapterHead chapters style (actually \chs@ChapterHead). That doesn't mean you're using it correctly.
    – Werner
    Feb 3, 2018 at 21:26
  • @Werner - I removed all references to Chapter Styles, to avoid this distraction. Again, I only included it as a way to solve this problem, (which seems very non-standard). I added your code example to mine - to show the issues with having to add style macros to every \chapter. Feb 4, 2018 at 20:50
  • @elikakohen: I have to admit, your edits seem to make the problem more complex than is needed. If you want "to override all chapter title and content pagestyles", then simply issue \chapterstyle at the point from which you want them to apply (typically in the preamble or \AtBeginDocument) and redefine the chapter page style in the same way (in the preamble via \makepagestyle{chapter} ...).
    – Werner
    Feb 6, 2018 at 1:31

You must log in to answer this question.

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