4

I looked up for a solution to access the name of current sections (chapters, sections, etc.) from macros (including starred variations of the section command). The set of macros proposed here works like a charm in the base class book.

\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
\pretocmd{\@sect}
  {\expandafter\gdef\leveltitle{#8}}
  {}{}
\pretocmd{\@ssect}
  {\expandafter\gdef\leveltitle{#5}}
  {}{}
\pretocmd{\@chapter}
  {\expandafter\gdef\leveltitle{#2}}
  {}{}
\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}

book_example

However I need to implement my macros in Koma-Script, with scrbook and here things start to get unexpected. With the same code but with class scrbook I get the following:

enter image description here

The chapter name is not rendered. I looked up the code of scrbook and the title is indeed #2. Is there something wrong with the macros and how to fix this issue?

For my needs, it is not possible to use nameref, I need a hard-coded solution. Thanks for you help.

Bonus: Is it possible to choose between long titles and short titles (the ones defined with \section[short]{long}? Thanks again.

7

You can use nameref, all you need is an automatically set \label and a the name of the current label. From upcomming KOMA-Script 3.27 (currently only available from the KOMA-Script source repository or the KOMA-Script pre-release repository) the new do-hook feature can be used to add the automatic labels:

\documentclass[headings=optiontoheadandtoc]{scrbook}[2019/03/05]
\usepackage{nameref}
\newcounter{secautolabel}
\AddtoDoHook{heading/endgroup}{\setautolabel}
\newcommand*{\setautolabel}[1]{%
  \stepcounter{secautolabel}%
  \label{sec:autolabel:\thesecautolabel}%
  \expandafter\xdef\csname #1title\endcsname{%
    \noexpand\nameref{sec:autolabel:\thesecautolabel}%
  }%
}

\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[reference={The First subsection reference}]{First subsection}
\subsubsection{First subsubsection}

\test

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

\test
\end{document}

with the results:

numbered headings not numbered headings

So you do not need to patch or use (internal) commands.

With the current official release you can do something similar by patching \sectionlinesformat and \chapterlinesformat:

\documentclass[headings=optiontoheadandtoc]{scrbook}
\usepackage{nameref}
\newcounter{secautolabel}
\newcommand*{\setautolabel}[1]{%
  \stepcounter{secautolabel}%
  \label{sec:autolabel:\thesecautolabel}%
  \expandafter\xdef\csname #1title\endcsname{%
    \noexpand\nameref{sec:autolabel:\thesecautolabel}%
  }%
}
\usepackage{xpatch}
\xapptocmd{\sectionlinesformat}{\setautolabel{#1}}{}{\undefined}
\xapptocmd{\sectioncatchphraseformat}{\setautolabel{#1}}{}{\undefined}
\xapptocmd{\chapterlinesformat}{\setautolabel{#1}}{}{\undefined}
\xapptocmd{\chapterlineswithprefixformat}{\setautolabel{#1}}{}{\undefined}

\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[reference={The First subsection reference}]{First subsection}
\subsubsection{First subsubsection}

\test

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

\test
\end{document}

The result is the same as above.

BTW: In both examples I've shown, how to manipulate the result using option headings=optiontotocandhead and the extended optional argument of, e.g., \subsection.

  • Thank you, neat solutions! I went with the second one as I want to stay with current official release of KOMA-Script. One more clarification: is it possible to have the same result with \partsection command? I tried to find similar hooks for partwithout success. – 81737 Apr 16 at 11:01
  • 2
    @81737 Please have a look into the KOMA-Script manual. Immediately before the description of \chapterlineswithprefixformat and \chapterlinesformat you can find \partlineswithprefixformat. And the suggestion for KOMA-Script 3.27 using do-hooks should work for parts too. If not, it is not yet implemented. – Schweinebacke Apr 16 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.