0

I am editing a package (which I did not write myself) where the titlesec package was used in combination with the scrbook document class. This produces an error, as explained in this post: Conflict between titlesec package and scrbook class after most recent update of TeXLive2019. I wish to fix this error, and get rid of the error message. I realize one must use the commands in the KOMA-class, but I don't know exactly how. Here is a minimal exaple:

Package:

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

The document:

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

So the challenge is redefining the commands section, subsection and subsubsection as intended by the author of this class without using the titlesec package.

Vebjorn
  • 1,778

3 Answers3

2

I see no way to apply \MakeUppercase to section titles with komascript methods. But it can be done.

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

Adjust the spacing and font parameters to your taste.

enter image description here

egreg
  • 1,121,712
  • This solution produced 14 errors, maybe because it is a package and the package does not provide \RedeclareSectionCommand ? – Vebjorn Apr 22 '22 at 16:39
  • 1
    @Vebjorn My guess is that you have an older version of LaTeX. Try adding \usepackage{xparse}. – egreg Apr 22 '22 at 16:42
  • I don't think I use an older version. I use Overleaf – Vebjorn Apr 22 '22 at 16:43
  • 1
    @Vebjorn I get no error. – egreg Apr 22 '22 at 16:47
  • Well, you have not made a package. Could also be other existing things in the package I was correcting that made it not work. – Vebjorn Apr 24 '22 at 00:46
  • @Vebjorn Since a package is just \input, it can't make a difference. You've still to tell what errors you get. – egreg Apr 24 '22 at 07:58
  • It says \RedeclareSectionCommand is not defined. – Vebjorn Apr 24 '22 at 20:28
  • 1
    @Vebjorn I guess you have an outdated version of the KoMa-script bundle, then. – egreg Apr 24 '22 at 20:32
  • @Vebjorn The example works for me on Overleaf, if I select TeXLive 2021 in the menu of the project on Overleaf. If you load the package after \documentclass{scrbook} the command \RedeclareSectionCommand must be known, because it is provided by the class. \RedeclareSectionCommand was introduced in scrbook a long time ago. – esdd Apr 25 '22 at 11:22
1

Here is another suggestion redefining \sectionlinesformat to apply \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}

I have changed the spacings before and after the headings (like in the answer of @egreg), because the spacings in the question are curious.

enter image description here

esdd
  • 85,675
0

I tried something like this:

\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

which seems to have worked. Now I just need tips on how to (correctly) include the spacings from the \titlespacing commands.

Vebjorn
  • 1,778
  • I just tried using 0.7ex on the afterskip, which seemes to have produced something similar. – Vebjorn Apr 22 '22 at 15:30
  • Using a KOMA-Script class do not redefine \section etc. using \@startsection. With an uptodate KOMA-Script version you will get warnings and several features of KOMA-Script will be deactivated. – esdd Apr 25 '22 at 11:30