2

I just tried to keep track of my current chapter name, because I use the first letter for certain namings in my document, so I wrote this code to save it:

\documentclass[12pt,a4paper]{scrreprt}
\usepackage{etoolbox}
\usepackage{xpatch}

\newcommand{\currentchapter}{Not set yet}

\xpretocmd{\chapter}
{%
    \renewcommand{\currentchapter}{#1}%
}{}{}

\begin{document}
\currentchapter
\chapter{Hello}
\currentchapter
\chapter{Goodbye}
\currentchapter

\end{document}

Unfortunately this just prints:

Not set yet.
#1
#1

For the 3 occurrences of my command between the chapters. But my expected output would be:

Not set yet.
Hello
Goodbye

In other peoples code this usage of arguments seems to work, so I don't understand why it doesn't work here.

  • \chapter has no arguments, try to out \show\chapter to see it. The arguments are grabbed later. – Ulrike Fischer Apr 27 '18 at 20:30
  • So, how would I grab these arguments? – Moraxno Apr 27 '18 at 20:40
2

The \chapter command doesn't grab arguments, but has first to check for * or an optional argument.

In the standard classes the command to patch should be \@chapter, but in KoMa classes it is \scr@@startchapter. If you want to patch it in order to save the title in \currentchapter you can do

\makeatletter
\xpretocmd{\scr@@startchapter}{\def\currentchapter{#3}}{}{}
\makeatother

Full example:

\documentclass[12pt,a4paper]{scrreprt}
\usepackage{xpatch}

\newcommand{\currentchapter}{Not set yet}

\makeatletter
\xpretocmd{\scr@@startchapter}
  {\renewcommand{\currentchapter}{#3}}
  {}{}
\makeatother

\begin{document}
\currentchapter
\chapter{Hello}
\currentchapter
\chapter{Goodbye}
\currentchapter

\end{document}

Yes, patching commands is a black art.

  • 1
    @Moraxno If you also want this to work with \chapter* commands then you will need a similar modification of \scr@@startschapter. Btw, the normal way to thank people on TeX.SX is to up-vote and award green ticks! :) – Andrew Apr 27 '18 at 21:18
  • @Andrew Thanks for the additional information. Unfortunately, while your solution worked yesterday, today it broke. It seems as if \xpretocmd{\scr@@startchapter{\renewcommand{\currentchapter}{#3}}{}{} now interprets #3 not as the argument of the original command but as the String '#3' and I have no idea why this is. I basically changed nothing to my commands, only added some contents to the lists... – Moraxno Apr 28 '18 at 7:50
  • @Moraxno I said that a similar modification of \scr@@startschapter. If you look in scrreprt.cls you will see that you need to use #2, rather than #3, when you patch \scr@@startschapter. – Andrew Apr 28 '18 at 14:03
2

You can patch \chapterlinesformat:

\documentclass[12pt,a4paper]{scrreprt}
\usepackage{xpatch}
\xpretocmd\chapterlinesformat{\gdef\currentchapter{#3}}{}{\fail}
\newcommand{\currentchapter}{Not set yet}

\begin{document}
\currentchapter
\chapter{Hello}
\currentchapter
\chapter{Goodbye}
\currentchapter

\end{document}

enter image description here

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.