修补报告和文章中章节的可靠方法

修补报告和文章中章节的可靠方法

我需要在每一章之后运行一个命令,所以我需要修补章节。如果我这样做:

\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}

那么它对班级来说很有效memoir,但对scrreport班级来说却无效。

memoir在课堂上修补课堂的正确方法是什么scrreport

编辑

Komascript 类甚至声明:

任何更改内部宏(如 \cs{@chapter}、\cs{@schapter}、\cs{@makechapterhead}、\cs{@makeschapterhead})的包都与 \KOMAScript{} 类不兼容。\KOMAScript{} 和 \KOMAScript{} 作者均不对此负责。欢迎包作者索取他们需要的接口,例如,定义自己的标题样式是个好主意。

编辑2

我尝试使用\AddtoDoHook{heading/endgroup}{\label{mydummylabel}}但它在该部分之后添加了一个丑陋的文本chaptersection

\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}

在此处输入图片描述

编辑3

哦,我明白了,要么我们用来heading/endgroup/chapter定位章节,否则它会像宏一样调用内容:

\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}

答案1

我使用钩子和测试来检查该类是否是 KOMA,从而解决了我的问题:

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

经过测试,它看起来像:

  \@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}%
  }%

请注意,我刚刚注意到该代码并未捕捉到section回忆录的内容……需要阅读更多文档!

相关内容