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.

O. R. Mapper
  • 1,029
  • 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 01 '15 at 13:40

2 Answers2

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

  • Is the patching really necessary? \renewcommand\printchapternonum{\isstarredchaptertrue\chapterheadstart} seems to do the same thing in this case. – daleif Oct 01 '15 at 14:14
  • @daleif: I don't know. I am no memoir expert. Sometimes I refrain from \renewcommand –  Oct 01 '15 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. – O. R. Mapper Oct 01 '15 at 15:58
  • @O.R.Mapper: Well, what should I do then? I don't know your real document –  Oct 01 '15 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. – O. R. Mapper Oct 01 '15 at 16:01
  • @O.R.Mapper: Then try daleif's proposition above. I'll delete my answer then –  Oct 01 '15 at 16:37
  • I already did, and it works. If @daleif wrote an answer, I'd accept it, as his or her solution seems to be more robust right now. However, that does not mean that your solution does not have any advantages in different situations that I cannot think of now; upvoted for that and I suggest you also leave it alife. – O. R. Mapper Oct 01 '15 at 21:47
  • I've added an answer based on Christians, but without patching. Also explained what is going on (for those not knowing about how memoir builds it chapters) – daleif Oct 02 '15 at 07:14
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}
daleif
  • 54,450