将 titlesec 代码转换为类似 KOMA 脚本的代码

将 titlesec 代码转换为类似 KOMA 脚本的代码

我知道将 titlesec 与 KOMA 脚本一起使用不是一个好主意,但过去这样做是可行的。现在它不起作用了,而且由于我依赖 KOMA 脚本,所以我想用更通用或至少与 KOMA 脚本兼容的代码替换剩余的 titlesec 代码:

%--> chapter with 'chapter' + number + rule + name + rule
% --------------------------------
\titleformat{\chapter}[display]               % {command}[shape]
  {\usekomafont{chapter}\Large \color{black}} % format
  {\LARGE\MakeUppercase{\chaptertitlename}%   % label
   \Huge~\thechapter \filright}%  
  {1pt}                                          % sep (from chapternumber)
  {%
   \titlerule \vspace{0.9pc} % 
   \filright 
   \color{sectioncolor}   
  }                                           % (before chaptertitle and after)
  [\color{black} \vspace{0.9pc} \filright {\titlerule}] %
% --------------------------------

章节标题示例

%--> part with 'PART' + number at frame and name inside frame
% --------------------------------
\titleformat{\part}[frame]
  {\usekomafont{part}\Large\color{black}\centering}    % format
  % label: PART I
  {\enspace \LARGE\MakeUppercase{\partname}%           
     \centering \Huge~\thepart \enspace }%  
  % sep (from partnumber)
  {1.5\baselineskip}
  % (before chaptertitle and after)
  {\color{sectioncolor}%
  \filcenter}          
% --------------------------------

示例部件标题

% spacing before and after sections
\titlespacing*{\section}{0pt}{*2.0}{*0.5}
\titlespacing*{\subsection}{0pt}{*1.5}{*0.5}
\titlespacing*{\subsubsection}{0pt}{*1.5}{*0.5}

我没有准备代码,因为对我来说这是 LateX 或 KOMA 内部代码,我不太了解。

该代码在我的模板中使用,因此该解决方案也将被许多其他人使用,并且无论如何都是公开的。

編輯:MWE

\documentclass{scrbook}

\usepackage{xcolor}
\usepackage{titlesec}

% --------------------------------
\titleformat{\chapter}[display]               % {command}[shape]
  {\usekomafont{chapter}\Large \color{black}} % format
  {\LARGE\MakeUppercase{\chaptertitlename}%   % label
   \Huge~\thechapter \filright}%  
  {1pt}                                          % sep (from chapternumber)
  {%
   \titlerule \vspace{0.9pc} % 
   \filright 
   \color{blue}
  }                                           % (before chaptertitle and after)
  [\color{black} \vspace{0.9pc} \filright {\titlerule}] %
% --------------------------------

%--> part with 'PART' + number at frame and name inside frame
% --------------------------------
\titleformat{\part}[frame]
  {\usekomafont{part}\Large\color{black}\centering}    % format
  % label: PART I
  {\enspace \LARGE\MakeUppercase{\partname}%           
     \centering \Huge~\thepart \enspace }%  
  % sep (from partnumber)
  {1.5\baselineskip}
  % (before chaptertitle and after)
  {\color{blue}%
  \filcenter}          

% --------------------------------

% spacing before and after sections
\titlespacing*{\section}{0pt}{*2.0}{*0.5}
\titlespacing*{\subsection}{0pt}{*1.5}{*0.5}
\titlespacing*{\subsubsection}{0pt}{*1.5}{*0.5}

\begin{document}

\part{Part Title}

\chapter{Introduction}

\subsection*{title}

\subsubsection*{title}

\end{document}

答案1

第二次更新 自 3.25 版本起,KOMA-Script\partlineswithprefixformat也提供:

\documentclass[chapterprefix,numbers=noenddot]{scrbook}[2018/03/30]% needs version 3.25 or newer
\usepackage{xcolor}

% part
\RedeclareSectionCommand[
  innerskip=0pt,
  font=\mdseries\Large,
  prefixfont=\LARGE,
]{part}

\renewcommand*{\partformat}{%
  \raisebox{-.5\dp\strutbox}{%
    \makebox[0pt]{%
      \setlength\fboxsep{.5em}%
      \colorbox{white}{%
        \partname\nobreakspace{\Huge\thepart\autodot}%
}}}}

\renewcommand*\partlineswithprefixformat[3]{% needs KOMA-Script version 3.25 or newer
  \ifstr{#1}{part}{%
    \raggedpart
    \fbox{%
      \parbox[t][\dimexpr\height+3\normalbaselineskip][c]
        {\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
        {\centering \color{blue}#3}%
    }\nolinebreak%
    \ifnumbered{part}{\hspace*{-.5\textwidth}#2}{\linebreak}%
  }{%
    #2#3
  }%
}
\renewcommand*\raggedpart{\raggedright}

% chapter
\RedeclareSectionCommand[
  innerskip=1pt,
  font=\mdseries\Large,
  prefixfont=\LARGE,
]{chapter}

\renewcommand*{\chapterformat}{%
  \mbox{\MakeUppercase{%
    \chapappifchapterprefix{\nobreakspace}}{\Huge\thechapter\autodot}%
    \IfUsePrefixLine{}{\enskip}}%
}

\renewcommand\chapterlineswithprefixformat[3]{%
  \ifstr{#1}{chapter}{%
    #2\nobreak%
    \vspace*{\dimexpr-\ht\strutbox}%
    \rule[-\dp\strutbox]{\textwidth}{.4pt}\\*[.9pc]%
    {\color{blue}#3}%
    \vspace*{\dimexpr-\ht\strutbox-\dp\strutbox+.9pc}\nobreak%
    \rule[-\dp\strutbox]{\textwidth}{.4pt}%
    \par\nobreak%
  }{%
    #2#3%
  }%
}

% other section levels
\RedeclareSectionCommand[
  beforeskip=-2ex plus -.6ex minus -0.12ex,
  afterskip=.5ex plus .05ex
]{section}

\RedeclareSectionCommands[
  beforeskip=-1.5ex plus -.45ex minus -0.09ex,
  afterskip=.5ex plus .05ex
]{subsection,subsubsection}

\begin{document}
\addpart{Part without number}
\tableofcontents
\part{Part Title}
\chapter{Introduction}
\subsection*{title}
\subsubsection*{title}
\part{Second title}
\end{document}

更新

也可以用于style=chapter部分。然后\chapterlineswithprefixformat也可以用于设置部分标题的布局。

\documentclass[chapterprefix,numbers=noenddot]{scrbook}
\usepackage{xcolor}

% part and chapter
\RedeclareSectionCommand[
  style=chapter,
  beforeskip=-1sp,
  afterskip=1sp,
  innerskip=0pt,
  font=\mdseries\Large,
  prefixfont=\LARGE,
]{part}

\RedeclareSectionCommand[
  innerskip=1pt,
  font=\mdseries\Large,
  prefixfont=\LARGE,
]{chapter}

\renewcommand*{\partformat}{%
  \raisebox{-.5\dp\strutbox}{%
    \makebox[0pt]{%
      \setlength\fboxsep{.5em}%
      \colorbox{white}{%
        \partname\nobreakspace{\Huge\thepart\autodot}%
}}}}

\renewcommand*{\chapterformat}{%
  \mbox{\MakeUppercase{%
    \chapappifchapterprefix{\nobreakspace}}{\Huge\thechapter\autodot}%
    \IfUsePrefixLine{}{\enskip}}%
}

\renewcommand\chapterlineswithprefixformat[3]{%
  \ifstr{#1}{chapter}{%
    #2\nobreak%
    \vspace*{\dimexpr-\ht\strutbox}%
    \rule[-\dp\strutbox]{\textwidth}{.4pt}\\*[.9pc]%
    {\color{blue}#3}%
    \vspace*{\dimexpr-\ht\strutbox-\dp\strutbox+.9pc}\nobreak%
    \rule[-\dp\strutbox]{\textwidth}{.4pt}%
    \par\nobreak%
  }{%
    \ifstr{#1}{part}{%
      \null\vfil
      \fbox{%
        \parbox[t][\dimexpr\height+3\normalbaselineskip][c]
          {\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
          {\centering \color{blue}#3}%
      }\nolinebreak%
      \ifnumbered{part}{\hspace*{-.5\textwidth}#2}{}%
      \vfil\newpage\partheademptypage
    }{%
      #2#3%
    }%
  }%
}

% other section levels
\RedeclareSectionCommand[
  beforeskip=-2ex plus -.6ex minus -0.12ex,
  afterskip=.5ex plus .05ex
]{section}

\RedeclareSectionCommands[
  beforeskip=-1.5ex plus -.45ex minus -0.09ex,
  afterskip=.5ex plus .05ex
]{subsection,subsubsection}

\begin{document}
\addpart{Part without number}
\tableofcontents
\part{Part Title}
\chapter{Introduction}
\subsection*{title}
\subsubsection*{title}
\part{Second title}
\end{document}

原始答案

您的代码将适用于titlesec版本 2016/03/21 v2.10.2 和KOMA-Script 3.20 预发布版。 看因 titlesec 的使用而导致的失败(德语)。

但这里也有一个不带的建议titlesec

  • 您可以使用\RedeclareSectionCommand\RedeclareSectionCommands设置节标题之前和之后的垂直跳过。
  • 要更改章节标题的布局,请重新定义命令\chapterformat\chapterlineswithprefix。请注意,我使用选项chapterprefix
  • 不幸的是,目前还没有\partlineswithprefixformat。所以我使用 TikZ 在零件标题周围绘制框架。因此该示例需要运行两次。

代码:

\documentclass[chapterprefix,numbers=noenddot]{scrbook}
\usepackage{xpatch}
\usepackage{tikz}

% part
\RedeclareSectionCommand[
  font=\mdseries\Large\color{blue},
  prefixfont=\usekomafont{part}\LARGE\color{black},
  expandtopt,
  innerskip=1\baselineskip
]{part}
\renewcommand*{\partformat}{%
  \tikz[remember picture]\node[inner ysep=0pt,inner xsep=.5em](partnumber)
    {{\MakeUppercase{\partname}\nobreakspace{\Huge\thepart\autodot}}};%
}

\xapptocmd{\partheadstartvskip}{%
  \ifunnumbered{part}
    {\tikz[remember picture]\coordinate(partstart);\\*[1.5\baselineskip]}
    {}%
}{}{}
\xpretocmd{\partheadendvskip}{%
  \vspace*{.5\baselineskip}%
  \noindent\ifnumbered{part}
    {\tikz[remember picture,overlay]\draw(partnumber)-|(0,0)--+(\textwidth,0)|-(partnumber);}
    {\tikz[remember picture,overlay]\draw(partstart)-|(0,0)--+(\textwidth,0)|-(partstart);}%
}{}{}

% chapter
\RedeclareSectionCommand[
  innerskip=1pt,
  font=\mdseries\Large,
  prefixfont=\LARGE,
]{chapter}

\renewcommand*{\chapterformat}{%
  \mbox{\MakeUppercase{%
    \chapappifchapterprefix{\nobreakspace}}{\Huge\thechapter\autodot}%
    \IfUsePrefixLine{}{\enskip}}%
}

\renewcommand\chapterlineswithprefixformat[3]{%
  #2\nobreak%
  \vspace*{\dimexpr-\ht\strutbox}%
  \rule[-\dp\strutbox]{\textwidth}{.4pt}\\*[.9pc]%
  {\color{blue}#3}%
  \vspace*{\dimexpr-\ht\strutbox-\dp\strutbox+.9pc}\nobreak%
  \rule[-\dp\strutbox]{\textwidth}{.4pt}%
  \par\nobreak%
}

% other section levels

\RedeclareSectionCommand[
  beforeskip=-2ex plus -.6ex minus -0.12ex,
  afterskip=.5ex plus .05ex
]{section}

\RedeclareSectionCommands[
  beforeskip=-1.5ex plus -.45ex minus -0.09ex,
  afterskip=.5ex plus .05ex
]{subsection,subsubsection}

\begin{document}
\addpart{Part without number}
\tableofcontents
\part{Part Title}
\chapter{Introduction}
\subsection*{title}
\subsubsection*{title}
\part{Second title}
\end{document}

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容