1

I need to run a command after each chapter, so I need to patch chapters. If I do:

\documentclass{scrreport} %% Works with memoir but not with scrreport

\NewDocumentCommand\MyAddLabel{}{\label{mylabel}}
\usepackage{etoolbox}

\makeatletter
%% Add the above command to \chapter if the command exists
\ifdef{\@chapter}{
  \ifpatchable*{\@chapter}{
    \apptocmd{\@chapter}{\MyAddLabel}{}{}%
  }{\PackageWarning{mypackage}{Chapters are not patchable.}{}}
}{}
\makeatother

\begin{document}
Test
\chapter{Testchap}
 asdf
\end{document}

Then it works great for the memoir class, but not for the scrreport class.

What's the proper way to also patch the memoir class in the scrreport class?

EDIT

The Komascript class even states:

Any package, that changes internal macros like \cs{@chapter}, \cs{@schapter}, \cs{@makechapterhead}, \cs{@makeschapterhead} is incompatible with \KOMAScript{} classes. Neither \KOMAScript{} nor the \KOMAScript{} author is responsible for this. Package authors are welcome to ask for interfaces they need, e.g., a good idea would be to define a own headings style.

EDIT2

I tried to use \AddtoDoHook{heading/endgroup}{\label{mydummylabel}} but it adds an ugly chapter or section text right after the section:

\documentclass{scrreport} %% Works with memoir but not with scrreport
\usepackage{lipsum}

\AddtoDoHook{heading/endgroup}{\label{mydummylabel}} % IRL, the label is programmatically chosen

\begin{document}
\lipsum[1]

\chapter{Here is my chapter}

\lipsum[1]

\section{Here is my section}

% \begin{thmE}
% Here is my important theorem  
% \end{thmE}

\end{document}

enter image description here

EDIT3

Oh, I understand, either we use heading/endgroup/chapter to target chapters, otherwise it calls the content like a macro:

\documentclass{scrreport} %% Works with memoir but not with scrreport
\usepackage{lipsum}
%%  Only for chapters
\AddtoDoHook{heading/endgroup/chapter}{Only for chapt.}

%% For everything
\def\mydummystuff#1{Coucou}
\AddtoDoHook{heading/endgroup}{\mydummystuff} % IRL, the label is programmatically chosen

\begin{document}

\lipsum[1]

\chapter{Here is my title}

\lipsum[1]

\section{Here is my section}

% \begin{thmE}
% Here is my important theorem  
% \end{thmE}

\end{document}
5
  • 4
    chapter commands are defined by the classes, and every class can do what it wants. So there is no "reliable" way that works with arbitrary classes, you will have to check every class you want to support for suitable hooks (KOMA has hooks, see the documentation). Aug 24, 2022 at 10:25
  • 2
    Side remark: But if you are really trying to add \label commands automatically: This is rather senseless. The main point of labels is that the user know which label they added, for this they muss see the command. Aug 24, 2022 at 10:32
  • @UlrikeFischer Thanks. So regarding the hooks, I tried to use them (see my edit) but I don't know why it adds an ugly chapter text on the line after the title. Regarding the label, I use them in a library to provide a way to refer to the current section, so the labels are chosen programmatically and not accessible directly to the end user.
    – tobiasBora
    Aug 24, 2022 at 11:17
  • Oh I think I understood the issue.
    – tobiasBora
    Aug 24, 2022 at 11:23
  • Not claiming what you're doing is 100% the wrong approach or the question is unclear just because of that, but there might be a better approach to achieve what you actually want to get e.g. Make an hyperref link to current chapter/section - TeX - LaTeX Stack Exchange
    – user202729
    Aug 24, 2022 at 17:41

1 Answer 1

0

I solved my problem using hooks and a test to check if the class is KOMA:

\AddtoDoHook{heading/endgroup/chapter}{\pratendAddLabel}

With the test it looks like:

  \@ifundefined{KOMAClassName}{%
    % For articles/...
    \ifdef{\@chapter}{
      \ifpatchable*{\@chapter}{
        \apptocmd{\@chapter}{\pratendAddLabel}{}{}%
      }{\PackageWarning{proof-at-the-end}{Chapters are not patchable.}{}}
    }{}
    %% patch sections/subsections/paragraph/...
    %% https://tex.stackexchange.com/questions/631713/patch-section-command-fails/631717?noredirect=1#comment1575337_631717
    \ifdef{\@sect}{
      % \apptocmd{\section}{\pratendAddLabel}{}{}%
      \ifpatchable*{\@sect}{
        \apptocmd{\@sect}{\pratendAddLabel}{}{}%
      }{\PackageWarning{proof-at-the-end}{Sections are not patchable.}{}}
    }{}%
  }{%
    %% For KOMA classes/... or it won't work with scrreport
    %% \AddtoDoHook{heading/endgroup/chapter} is too specific 
    \AddtoDoHook{heading/endgroup/chapter}{\pratendAddLabel}%
    \AddtoDoHook{heading/endgroup/section}{\pratendAddLabel}%
  }%

Note that I just noticed that this code does not capture the section of a memoir… will need to read more doc!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .