9

I came across this answer which claims that it works even with the starred version of \chapter et al. The following example shows that it does not, because \...mark macros are not called. Is there a way to "fix" this and be able to set the respective value of the \...name macros even if there is a *-type call?

enter image description here

\documentclass{book}

\let\Chaptermark\chaptermark
\let\Sectionmark\sectionmark
\let\Subsectionmark\subsectionmark
\let\Subsubsectionmark\subsubsectionmark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}
\def\sectionmark#1{\def\Sectionname{#1}\Sectionmark{#1}}
\def\subsectionmark#1{\def\Subsectionname{#1}\Subsectionmark{#1}}
\def\subsubsectionmark#1{\def\Subsubsectionname{#1}\Subsubsectionmark{#1}}

\begin{document}
\chapter{First chapter}
Title: ``\Chaptername''.
\section{First section}
Title: ``\Sectionname''.
\subsection{First subsection}
Title: ``\Subsectionname''.
\subsubsection{First subsubsection}
Title: ``\Subsubsectionname''.
\chapter*{Second chapter}
Title: ``\Chaptername''.
\section*{Second section}
Title: ``\Sectionname''.
\subsection*{Second subsection}
Title: ``\Subsectionname''.
\subsubsection*{Second subsubsection}
Title: ``\Subsubsectionname''.
\end{document}

Lets assume that I have a long file with many \section (etc.) calls. But there is a macro which I want to change to contain the title of the actual section (etc.).

masu
  • 6,571
  • See package nameref – Marco Daniel Nov 08 '13 at 14:05
  • 2
    @MarcoDaniel doesn't the use of nameref mean that I have to \label each and every section, chapter, etc? If I want to automatize the labeling the problem seems to remain the same with a little more thinkering with labels. – masu Nov 08 '13 at 14:10
  • No, it won't do what it's claimed, because \...mark is called only for the non *-version. – egreg Nov 08 '13 at 14:59
  • However, it's not very clear what you want to achieve: a \chapter* command doesn't set any label. So your \chapter*{Second chapter}\label{ch:second} will simply make \ref{ch:second} refer to \subsubsection{First subsubsection} (or the last refstepped counter at the same group level, in general). – egreg Nov 08 '13 at 15:05
  • @egreg Thanks, I've fixed my question. I was asking about a way to set the \...name macros even there is a * call. – masu Nov 08 '13 at 16:11
  • 1
    I still don't understand what the use of this would be. – egreg Nov 08 '13 at 16:40
  • @egreg: To use this with section titles in the todolist. Maybe it would be better to unset them if a \chapter* macro call occurs (but this would only mean to set them to \relax instead of the title. – masu Nov 08 '13 at 17:11
  • How many \chapter* do you have? I guess one or two: alternating numbered and unnumbered chapters (or sections or subsections) is quite dubious practice. So I continue not to understand what this would be for. – egreg Nov 08 '13 at 17:17

1 Answers1

11

The given macros will not work with the *-versions, because the \...mark commands are issued only when the * is not present. For instance, \chapter{Title} issues \chaptermark{Title}, but \chapter*{Title} doesn't.

Here's a set of patches that will provide the commands

\chaptertitle
\sectiontitle
\subsectiontitle
\subsubsectiontitle

which will always expand to the most recent title of the corresponding sectional unit.

\documentclass{book}

\usepackage{etoolbox}
% Patch the sectioning commands to provide a hook to be used later
\preto{\chapter}{\def\leveltitle{\chaptertitle}}
\preto{\section}{\def\leveltitle{\sectiontitle}}
\preto{\subsection}{\def\leveltitle{\subsectiontitle}}
\preto{\subsubsection}{\def\leveltitle{\subsubsectiontitle}}

\makeatletter
% \@sect is called with normal sectioning commands
% Argument #8 to \@sect is the title
% Thus \section{Title} will do \gdef\sectiontitle{Title}
\pretocmd{\@sect}
  {\expandafter\gdef\leveltitle{#8}}
  {}{}
% \@ssect is called with *-sectioning commands
% Argument #5 to \@ssect is the title
\pretocmd{\@ssect}
  {\expandafter\gdef\leveltitle{#5}}
  {}{}
% \@chapter is called by \chapter (without *)
% Argument #2 to \@chapter is the title
\pretocmd{\@chapter}
  {\expandafter\gdef\leveltitle{#2}}
  {}{}
% \@schapter is called with \chapter*
% Argument #1 to \@schapter is the title
\pretocmd{\@schapter}
  {\expandafter\gdef\leveltitle{#1}}
  {}{}
\makeatother

\newcommand\test{%
  \noindent
  The chapter title is \chaptertitle\\
  The section title is \sectiontitle\\
  The subsection title is \subsectiontitle\\
  The subsubsection title is \subsubsectiontitle
}

\begin{document}
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}

\test

\chapter*{Second chapter}
\section*{Second section}
\subsection*{Second subsection}
\subsubsection*{Second subsubsection}

\test
\end{document}

Here \test is just to show that at the point the command is issued the control sequences hold the right value.

enter image description here

enter image description here

The patches must go before the loading of hyperref (if you use it).

David Carlisle
  • 757,742
egreg
  • 1,121,712