在 KOMA 脚本 scrartcl 中交换章节标题和章节编号

在 KOMA 脚本 scrartcl 中交换章节标题和章节编号

由于不建议titlesec在 KOMA 脚本中使用,我正在寻找一些替代方法来交换章节标题和章节编号,

Title 1

代替

1 Title

最小示例:

\documentclass{scrartcl}

\begin{document}

\section{Title}
\section{Title}
\section{Title}

\end{document}

生产

在此处输入图片描述

我发现的所有解决方案都使用了titlesec包 - 它scrartcl确实可以使用,但会产生警告:

Package titlesec Warning: Non standard sectioning command \section
(titlesec) detected. Using default spacing and no format.
Package titlesec Warning: Non standard sectioning command \subsection
(titlesec) detected. Using default spacing and no format.
Package titlesec Warning: Non standard sectioning command \subsubsection
(titlesec) detected. Using default spacing and no format.
Package titlesec Warning: Non standard sectioning command \paragraph
(titlesec) detected. Using default spacing and no format.
Package titlesec Warning: Non standard sectioning command \subparagraph
(titlesec) detected. Using default spacing and no format.
)
Class scrartcl Info: Unknown `titesec' release.
(scrartcl) Cross your fingers, that is compatible on input line 9.

我怎样才能在不发出警告的情况下交换号码和标题scrartcl

答案1

您必须重新定义\sectionlinesformat\sectionformat

\documentclass{scrartcl}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \Ifstr{#1}{section}
    {\@hangfrom{\hskip #2}{#4#3}}
    {\@hangfrom{\hskip #2#3}{#4}}% original definition for subsection, subsubsection
}
\makeatother
\renewcommand\sectionformat{\enskip\thesection\autodot}

\begin{document}
\section{Title}
\section{Title}
\section{Title}
\end{document}

在此处输入图片描述


关于以下评论:如果您想使用页面样式,headings那么您可以添加

\makeatletter
\renewcommand*{\sectionmark}[1]{%
  \if@twoside\expandafter\markboth\else\expandafter\markright\fi
  {\MakeMarkcase{#1\Ifnumbered{section}{\sectionmarkformat}{}}}{}}%
\makeatother
\renewcommand*\sectionmarkformat{\enskip\thesection\autodot}

在页眉中交换章节编号和章节标题。

如果有目录,请添加

\renewcommand*{\addsectiontocentry}[2]{%
  \IfArgIsEmpty{#1}
    {\addtocentrydefault{section}{#1}{#2}}
    {\addtocentrydefault{section}{}{#2\enskip#1}}%
}

例子:

\documentclass{scrartcl}
\usepackage{lipsum}% only for dummy text
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
%\providecommand*\Ifnumbered{\ifnumbered}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\pagestyle{headings}

\renewcommand*\sectionformat{\enskip\thesection\autodot}
\renewcommand*\sectionmarkformat{\enskip\thesection\autodot}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \Ifstr{#1}{section}
    {\@hangfrom{\hskip #2}{#4#3}}
    {\@hangfrom{\hskip #2#3}{#4}}% original definition for subsection, subsubsection
}
\renewcommand*{\sectionmark}[1]{%
  \if@twoside\expandafter\markboth\else\expandafter\markright\fi
  {\MakeMarkcase{#1\Ifnumbered{section}{\sectionmarkformat}{}}}{}}%
\makeatother
\renewcommand*{\addsectiontocentry}[2]{%
  \IfArgIsEmpty{#1}
    {\addtocentrydefault{section}{#1}{#2}}
    {\addtocentrydefault{section}{}{#2\enskip#1}}%
}


\begin{document}
\tableofcontents
\section{Title}
\section{Title}
\section{Title}
\lipsum

\end{document}

在此处输入图片描述

相关内容