4

I am writing a memoir document and I'd like to customize the visual appearance of the chapter title with Tikz1. This works so far, as shown by this MWE2:

\documentclass[a4paper,11pt]{memoir}

\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{calc}

\renewcommand{\chapterheadstart}{}
\renewcommand{\printchaptername}{}
\renewcommand{\chapternamenum}{}
\renewcommand{\printchapternum}{}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{\noindent\Huge\begin{tikzpicture}
  \clip (0,0) rectangle (\textwidth,\baselineskip);

  \node (bottomleft) at (0,0) {};
  \node (topright) at (\textwidth,\baselineskip) {};

  \draw[red] (bottomleft.center) -- (topright.center);

  \node at ($(bottomleft)!0.5!(topright)$) {\textbf{#1}};
  \node at (.1\textwidth, .5\baselineskip) {\thechapter};
\end{tikzpicture}}

\begin{document}

\chapter{First}

\lipsum[1-10]

\chapter*{Extra}

\lipsum[11-20]

\chapter{Last}

\lipsum[21-30]

\end{document}

This outputs the chapter name and chapter number at the beginning of each chapter in such a way that both pieces of information are embedded into the graphic.

Unfortunately (albeit unsurprisingly), the middle unnumbered chapter (\chapter*) reuses the number of the previous chapter. I would like to hide that number. Therefore, my question is:

How can I find out whether or not the current chapter is numbered? Is there any function that returns such a value, or a function that only prints its argument if the current chapter is numbered?

1: My actual graphic is quite a bit more complex and more appealing. The simple graphical chapter title in the MWE here are just for demonstration purposes.

2: Unlike my actual document, this MWE draws its contents from the lipsum package.

1
  • 1
    I had a similar question in mind while adding front and back matter to my thesis (though for the page header) and ended up changing the definition of the header based on where I was in the document. This was mainly for reasons of time. I think you'd need to patch the definition of \chapter and/or \chapter* to set a value to the chapter number of blank. Alternatively keep a count of the previous chapter number and compare that to the current reported value in your figure generating routine. Just a couple of thoughts in case no better way appears.
    – Chris H
    Oct 1, 2015 at 13:40

2 Answers 2

2

Here is more or less how memoir builds chapter headings

Numbered:

 \chapterheadstart % vert space
 \printchaptername
 \chapternamenum % space between chapter name and num
 \printchapternum
 \afterchapternum % mid space
 \printchaptertitle{Title}
 \afterchaptertitle % space below 

Unnumbered:

 \chapterheadstart % vert space
 \printchapternonum % empty by default
 \printchaptertitle{Title}
 \afterchaptertitle % space below 

While building a chapter style, it is perfectly fine to ditch some of these macros and move functionality into others. Then for example \printchapternonum can be used to run a switch such that \printerchaptertitle know it is being used in an unnumbered context.

So using Christians solution with a build in test inside \printchaptertitle, it is enough for use to add

\renewcommand\printchapternonum{\isstarredchaptertrue}

and the code will work for both numbered and unnumbered chapters.

For completness, here is Christians code slightly modified without the need for patching.

\documentclass[a4paper,11pt]{memoir}
\newif\ifisstarredchapter
% false by default
\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{calc}

\renewcommand{\chapterheadstart}{}
\renewcommand{\printchaptername}{}
\renewcommand{\chapternamenum}{}
\renewcommand{\printchapternum}{}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{\Huge\begin{tikzpicture}
  \node (bottomleft) at (0,0) {};
  \node (topright) at (\textwidth,\baselineskip) {};
  \draw[red] (bottomleft) -- (topright);
  \node at ($(bottomleft)!0.5!(topright)$) {\textbf{#1}};
  \ifisstarredchapter
  \else
  \node at (.1\textwidth, .5\baselineskip) {\thechapter};
  \fi
\end{tikzpicture}}

\renewcommand\printchapternonum{\isstarredchaptertrue}

\begin{document}

\chapter{First}

\lipsum[1-10]

\chapter*{Extra}

\lipsum[11-20]

\chapter{Last}

\lipsum[21-30]

\end{document}
3
  • This works! By the way, in addition to chapters, I'm meanwhile trying to achieve the same thing for parts, where the analogous solution does not seem to be available - there is no such thing as \printpartnonum, it seems (?) Oct 2, 2015 at 7:34
  • 1
    No, that is only available for chapters. In this case you will probable have to patch \@part and \@spart
    – daleif
    Oct 2, 2015 at 7:48
  • Thanks, that helped developed a solution. Oct 2, 2015 at 18:25
3

This solution hooks into the \@makechapterhead and \@makeschapterhead commands, being responsible for the unstarred and the starred chapter packages. It toggles a \ifstarredchapter conditional correspondingly which is then used in the tikzpicture environment in order to decide whether the chapter number should be printed or not.

\documentclass[a4paper,11pt]{memoir}


\usepackage{xpatch}

\newif\ifisstarredchapter

\isstarredchapterfalse

\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{calc}

\renewcommand{\chapterheadstart}{}
\renewcommand{\printchaptername}{}
\renewcommand{\chapternamenum}{}
\renewcommand{\printchapternum}{}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{\Huge\begin{tikzpicture}
  \node (bottomleft) at (0,0) {};
  \node (topright) at (\textwidth,\baselineskip) {};
  \draw[red] (bottomleft) -- (topright);
  \node at ($(bottomleft)!0.5!(topright)$) {\textbf{#1}};
  \ifisstarredchapter
  \else
  \node at (.1\textwidth, .5\baselineskip) {\thechapter};
  \fi
\end{tikzpicture}}


\makeatletter
\xpatchcmd{\@makechapterhead}{%
  \chapterheadstart%  \vspace*{50\p@}%
}{%
  \isstarredchapterfalse%  No, this is not a starred chapter
  \chapterheadstart%  \vspace*{50\p@}%
}{\typeout{success}}{}

\xpatchcmd{\@makeschapterhead}{%
  \chapterheadstart%
}{%  
  \isstarredchaptertrue% Yes, this is starred chapter. 
  \chapterheadstart%
}{\typeout{Success}}{}



\begin{document}

\chapter{First}

\lipsum[1-10]

\chapter*{Extra}

\lipsum[11-20]

\chapter{Last}

\lipsum[21-30]

\end{document}

enter image description here enter image description here

8
  • Is the patching really necessary? \renewcommand\printchapternonum{\isstarredchaptertrue\chapterheadstart} seems to do the same thing in this case.
    – daleif
    Oct 1, 2015 at 14:14
  • @daleif: I don't know. I am no memoir expert. Sometimes I refrain from \renewcommand
    – user31729
    Oct 1, 2015 at 14:16
  • This works in my example document, but in my actual document, it breaks compilation with the error C:\Program Files\MiKTeX 2.9\tex\latex\l3packages\xparse\xparse.sty:1543: LaTeX / error: "kernel/command-already-defined" / ! Control sequence \BooleanFalse already defined. Oct 1, 2015 at 15:58
  • @O.R.Mapper: Well, what should I do then? I don't know your real document
    – user31729
    Oct 1, 2015 at 16:00
  • @ChristianHupfer: Sure, I'm just pointing out that the solution is not yet compatible with all contexts (so other possible visitors know). As long as I don't know what's the difference between 200+ pages document and my MWE, I cannot update my MWE or ask a new question, but I'll keep looking into it. Oct 1, 2015 at 16:01

You must log in to answer this question.

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