1

I use \include to insert parts into a large document. Some of the parts contain chapter declarations, others only sections. I would like to make the current chapter name available in the daughter sections, even when the chapter parts are unincluded.

My attempt was to modify the \chaptermark command (from the answer to how to get the current chapter name, section name, subsection name, etc?) to store the chapter name in \@Chaptername and also save this to the part's aux file.

However, my line in the aux file is written outside the \@setckpt such that all definitions of \@Chaptername are read in at the beginning of the document and the variable then holds the name of the last chapter (until an included chapter part overwrites it).

Here is a MNWE:

\documentclass{scrbook}

\begin{filecontents}{Ch1.tex}
\chapter{Chapter 1}
\end{filecontents}

\begin{filecontents}{Ch2.tex}
\chapter{Chapter 2}
\end{filecontents}

\includeonly{Ch2}       % <- Hide this to see the effect.

\makeatletter
\let\Chaptermark\chaptermark
\def\chaptermark#1{\gdef\@Chaptername{\thechapter\enskip#1}\Chaptermark{#1}%
    \write\@partaux{\string\gdef\string\@Chaptername{\thechapter\enskip#1}}}
\def\Chaptername{\ifdefined\@Chaptername\@Chaptername\fi}
\makeatother

\begin{document}

\include{Ch1}

The parent chapter is called: \Chaptername

\include{Ch2}

\end{document}

With the marked line commented out, one gets the desired result: enter image description here

With the marked line not commented out, one gets: enter image description here

I suspect a possible solution could be to write to the checkpoint section \@setckpt in the part's aux file. Is this possible?

Or is there maybe a more elegant / better solution?

FlorianL
  • 1,659

1 Answers1

2

The problem is that you you use the same macro \@Chaptername for every chapter. So the "last value" of \@Chaptername is the one that gets used. This is OK when every chapter is included, since \chaptermark redefines it, but if a chapter is not included this becomes a problem. For your MWE the value of \@Chaptername is 2 Chapter 2, which is not what you want.

To get around this you should define \@Chaptername1, \@Chaptername2, .... Unfortunately, (La)TeX is not fond of having numbers in macro names. You can get around this using \csname ...\endcsname but I prefer using \csefd{...} from the etoolbox or, in this case csgdef{...}. With this slight modification in place your code works.

Here is the modified code:

\documentclass{scrbook}
\usepackage{etoolbox}

\begin{filecontents}{Ch1.tex}
\chapter{Chapter 1}
\end{filecontents}

\begin{filecontents}{Ch2.tex}
\chapter{Chapter 2}
\end{filecontents}

\includeonly{Ch2}       % <- Hide this to see the effect.

\makeatletter
\let\Chaptermark\chaptermark
\def\chaptermark#1{%
  \csgdef{@Chaptername\arabic{chapter}}{\thechapter\enskip#1}\Chaptermark{#1}%
  \write\@partaux{\string\csgdef{@Chaptername\arabic{chapter}}{\thechapter\enskip#1}}%
}
\def\Chaptername{\ifcsdef{@Chaptername\arabic{chapter}}{\csuse{@Chaptername\arabic{chapter}}}{Chapter \arabic{chapter} unknown}}
\makeatother

\begin{document}

\include{Ch1}

The parent chapter is called: \Chaptername

\include{Ch2}

\end{document}