[Tex/LaTex] convert titlesec code to something KOMA-script like

koma-script

I know that it is not a good idea to use titlesec together with KOMA script, but it worked in the past. Now it does not work and since I rely on KOMA script I would like to replace the remaining titlesec code with something more generic or at least KOMA script compatible:

%--> 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}] %
% --------------------------------

example chapter heading

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

example part heading

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

I have no code prepared simply because this is to me LateX or KOMA internal code I do not know well enough.

The code is used in my template, thus the solution is going to be used by many other people as well and public anyway.

EDIT:
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}

Best Answer

Second Update Since version 3.25 KOMA-Script provides \partlineswithprefixformat, too:

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

Update

It is also possible to use style=chapter for parts. Then \chapterlineswithprefixformat can also be used to set the layout for the part titles.

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

Original answer

Your code will work with titlesec version 2016/03/21 v2.10.2 and the prerelease of KOMA-Script 3.20. See Fehler bei Verwendung von titlesec (German).

But here is also a suggestion without titlesec.

  • You can use \RedeclareSectionCommand and \RedeclareSectionCommands to set the vertical skips before and after section headings.
  • To change the layout of the chapter titles redefine the commands \chapterformat and \chapterlineswithprefix. Note that I use option chapterprefix.
  • Unfortunaly there is no \partlineswithprefixformat yet. So I use TikZ to draw the frame around the part titles. Therefore the example needs two runs.

Code:

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

enter image description here enter image description here enter image description here enter image description here