scrbook 重新定义没有 titlesec 包的部分

scrbook 重新定义没有 titlesec 包的部分

我正在编辑一个包(不是我自己写的),该titlesec包与文档类结合使用scrbook。这会产生错误,如本文所述:TeXLive2019 最新更新后,titlesec 包与 scrbook 类之间发生冲突。我希望修复这个错误,并消除错误消息。我意识到必须使用 KOMA 类中的命令,但我不知道具体怎么做。这是一个最小的例子:

包裹:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage} 
\RequirePackage[T1]{fontenc}
\RequirePackage[explicit]{titlesec}
\titleformat{\section}
  {\normalfont}{\thesection}{0pt}{\phantomsection\fontsize{18pt}{20pt} \sffamily\center\MakeUppercase{#1}}
\titleformat{\subsection}
  {\normalfont}{\thesubsection}{0pt}{\fontsize{14pt}{16pt} \sffamily\center\MakeUppercase{#1}}
\titleformat{\subsubsection}
  {\normalfont}{\thesubsubsection}{0pt}{\scshape{\textbf{\normalsize #1}}}
% {left}{top}{below}{right}
\titlespacing*{\section}{0pt}{0pt minus 0.5em}{0pt}
\titlespacing*{\subsection}{0pt}{-2em plus 0.3em minus 0.3em}{-0.4em plus 0.2em minus 0.2em}
\titlespacing*{\subsubsection}{0pt}{-0.5em plus 0.2em}{-0.8em plus 0.5em minus 0em} 
\setcounter{secnumdepth}{0} 

该文件:

\documentclass[fontsize=13pt,a4paper,openany,parskip=half,DIV=calc]{scrbook}
\usepackage{mypackage}
\begin{document}
\section{Section}
    Some text.
\subsection{Subsection}
    More text.
\subsubsection{Subsubsection}
    Text.
\end{document}

因此,挑战在于重新定义命令sectionsubsectionsubsubsection按照此类作者的意图在不使用titlesec包的情况下进行定义。

答案1

我看不出有什么办法可以将方法应用于\MakeUppercase章节标题komascript。但这是可以做到的。

\RequirePackage{fix-cm}
\documentclass[fontsize=13pt,a4paper,openany,parskip=half,DIV=calc]{scrbook}

\usepackage{lipsum} % for nonsense text

\RedeclareSectionCommand[
  style=section,
  level=1,
  runin=false,
  font=\fontsize{18}{20}\sffamily\centering,
  beforeskip=10pt,
  afterskip=5pt,
]{section}
\RedeclareSectionCommand[
  style=section,
  level=2,
  runin=false,
  font=\fontsize{14}{16}\sffamily\centering,
  beforeskip=6pt,
  afterskip=3pt,
]{subsection}
\RedeclareSectionCommand[
  style=section,
  level=3,
  runin=false,
  font=\normalfont\normalsize\scshape,
  beforeskip=6pt,
  afterskip=3pt,
]{subsubsection}
\setcounter{secnumdepth}{0}

\NewCommandCopy{\scrsection}{\section}
\NewCommandCopy{\scrsubsection}{\subsection}

\NewDocumentCommand{\uppercasesection}{msom}{%
  \IfBooleanTF{#2}{%
    #1*{\MakeUppercase{#4}}
  }{%
    \IfNoValueTF{#3}{%
      #1{\MakeUppercase{#4}}%
    }{%
      #1[#3]{\MakeUppercase{#4}}%
    }%
  }%
}
\RenewDocumentCommand{\section}{}{\uppercasesection\scrsection}
\RenewDocumentCommand{\subsection}{}{\uppercasesection\scrsubsection}

\begin{document}

\lipsum[1][1-3]

\section{Section}

\lipsum[1][1-3]

\subsection{Subsection}

\lipsum[1][1-3]

\subsubsection{Subsubsection}

\lipsum[1][1-3]

\end{document}

根据您的喜好调整间距和字体参数。

在此处输入图片描述

答案2

以下是另一个重新定义\sectionlinesformat应用的建议\MakeUppercase

\begin{filecontents}[overwrite]{mypackage.sty}
\ProvidesPackage{mypackage}
\@ifundefined{KOMAClassName}
  {% no KOMA-Script class is used
    \RequirePackage[explicit]{titlesec}
    \titleformat{\section}
      {\normalfont}{\thesection}{0pt}{\fontsize{18pt}{20pt} \sffamily\centering\MakeUppercase{#1}}
    \titleformat{\subsection}
      {\normalfont}{\thesubsection}{0pt}{\fontsize{14pt}{16pt} \sffamily\centering\MakeUppercase{#1}}
    \titleformat{\subsubsection}
      {\normalfont}{\thesubsubsection}{0pt}{\scshape{\normalsize #1}}
    %{indent}{before}{after}
    \titlespacing*{\section}{0pt}{10pt}{5pt}
    \titlespacing*{\subsection}{0pt}{6pt}{3pt}
    \titlespacing*{\subsubsection}{0pt}{6pt}{3pt}
  }
  {% a KOMA-Script class is used
    \RedeclareSectionCommand[
      runin=false,
      font=\fontsize{18}{20}\mdseries,
      beforeskip=10pt,
      afterskip=5pt,
    ]{section}
    \RedeclareSectionCommand[
      runin=false,
      font=\fontsize{14}{16}\mdseries,
      beforeskip=6pt,
      afterskip=3pt,
    ]{subsection}
    \RedeclareSectionCommand[
      runin=false,
      font=\normalfont\normalsize\scshape,
      beforeskip=6pt,
      afterskip=3pt,
    ]{subsubsection}
    \renewcommand*{\sectionlinesformat}[4]{%
      \Ifstr{#1}{section}
        {\centering\MakeUppercase{#3#4}}
        {%
          \Ifstr{#1}{subsection}
            {\centering\MakeUppercase{#3#4}}
            {\@hangfrom{\hskip #2#3}{#4}}% original definition for other section levels
        }%
      }%
  }
\setcounter{secnumdepth}{0}
\end{filecontents}


\RequirePackage{fix-cm}
\documentclass[fontsize=13pt,a4paper,openany,parskip=half,DIV=calc]{scrbook}
\usepackage{mypackage}

\usepackage{lipsum} % for nonsense text

\begin{document}
\lipsum[1][1-3]
\section{Section}
\lipsum[1][1-3]
\subsection{Subsection}
\lipsum[1][1-3]
\subsubsection{Subsubsection}
\lipsum[1][1-3]
\end{document}

我已经改变了标题前后的间距(就像@egreg 的回答一样),因为问题中的间距很奇怪。

在此处输入图片描述

答案3

我尝试了这样的事情:

\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
             {-3.5ex \@plus -1ex \@minus -.2ex}%
             {2.3ex \@plus.2ex}%
             {\center\phantomsection\fontsize{18pt}{20pt} \sffamily\MakeUppercase}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
             {-3.25ex\@plus -1ex \@minus -.2ex}%
             {1.5ex \@plus .2ex}%
             {\fontsize{14pt}{16pt} \sffamily\center\MakeUppercase}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
             {-3.25ex\@plus -1ex \@minus -.2ex}%
             {1.5ex \@plus .2ex}%
             {\scshape\normalsize\bfseries}}
\makeatother
                                  

似乎已经奏效了。现在我只需要一些关于如何(正确地)在\titlespacing命令中包含空格的提示。

相关内容