1

I'm writing a custom class based on book.cls for our office's publication series, but I've hit a problem in how to set up a conditional.

Some of the publications are part->chapter->section while others are just chapter->section. Our publication format requires that the alternating page footers are short title (odd page) and either part or chapter (even page).

My question is: how can I determine of a document structure has parts, and if so change the footer accordingly?

3
  • 2
    after a part the \value{part} will be bigger than 0, if you want to know from the start that the document has parts either you will need to invent a new command that declares that, or at end of document write the value of the part counter to the aux file so detect on the next run whether parts are used Commented Dec 9, 2015 at 18:56
  • I would guess that if I test \value{part}>0 in the class definition (i.e. \ifnum \value{part}>0...\fi) for the header/footer to conditionally set/define a partmark instead of chaptermark?
    – JSCard
    Commented Dec 23, 2015 at 15:25
  • @DavidCarlisle, that worked great... nested an \ifnum \value{part} >\m@ne to test for part else set chaptermark. If you drop your comment to an answer, I'll mark it.
    – JSCard
    Commented Dec 29, 2015 at 17:53

1 Answer 1

2

Here's a modification of https://tex.stackexchange.com/a/241999/4427

\documentclass{book}

\usepackage{fancyhdr,etoolbox}
\usepackage{hyperref}

\usepackage{lipsum} % just for the example


\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[CE]{Short title}
\fancyfoot[CO]{\leftmark}
\fancyfoot[LE,RO]{\thepage}

\newif\ifpartused
\newcommand{\partmark}[1]{%
  \markboth{Part \thepart: #1}{}%
  \global\partusedtrue
}
\renewcommand{\chaptermark}[1]{%
  \ifpartused\else
    \markboth{Chapter \thechapter: #1}{}%
  \fi
}
\newcommand{\stopparts}{\global\partusedfalse}
\makeatletter
\@ifundefined{H@old@part}
  {\patchcmd{\@part}}
  {\patchcmd{\H@old@part}}
  {\markboth{}{}}
  {\partmark{#1}}
  {}{}
\makeatother

\begin{document}

\mainmatter

\chapter{Introduction}

\lipsum[1-30]

\part{First Part}

\chapter{Title A}

\lipsum[1-30]

\stopparts

\chapter{\bibname}

\lipsum[1-30]

\end{document}

If you comment out \part, you get chapters in the footer. Works with or without hyperref.

3
  • Thanks @egreg, but I'm doing this in a custom class file and trying to stay away from using fancyhdr if I can. It would require a lot of re-coding for some other things I'm doing in there.
    – JSCard
    Commented Dec 29, 2015 at 17:51
  • @Scard The information you gave was insufficient, then. Do you just use the standard \pagestyle{headings}? Or do you define a new one? It's impossible to know what you have in the class, isn't it?
    – egreg
    Commented Dec 29, 2015 at 17:54
  • @ egreg yes... I re-wrote the \pagestyle{headings} in the custom class to test the \value{part} and set the marks accordingly. Your solution would work great otherwise. Meant no slight to your answer, just not quite what I was aiming for. I needed to figure out how to test the conditional case for some other things in there as well.
    – JSCard
    Commented Dec 29, 2015 at 17:58

You must log in to answer this question.

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