[Tex/LaTex] Create new section commands (with correct TOC entries) in KOMA-Script

koma-scriptsectioningsections-paragraphstable of contents

What is needed to make a new sectioning command which outputs a correct entry in the TOC?

For example, a command \foo which outputs something similar to \subsection in the TOC (and in the main document). After a few trial and error, I thought that just copying the definition of \subsection and defining the needed commands (the foo counter, \thefoo command, \addfootocentry, \foomarkformat, \foomark). But it seems that it's not enough.

Here is a minimal (non) working example.

enter image description here

\documentclass[DIV12]{scrartcl}

\makeatletter
\newcommand*{\foonumdepth}{\tw@}
\newcounter{foo}[section]
\renewcommand*{\thefoo}{\thesection.\@arabic\c@foo}
\newcommand*\foomarkformat{\thefoo\autodot\enskip}
\newcommand*{\addfootocentry}[2]{%
  \addtocentrydefault{foo}{#1}{#2}%
}
\newcommand\foomark[1]{\markright{#1}}
\newkomafont{foo}{\normalfont\normalsize}
\newcommand\foo{%
 \@startsection{foo}%
  {\foonumdepth}%
  {\z@}%
  {-\baselineskip}%
  {\baselineskip}%
  {\ifnum \scr@compatibility>\@nameuse{scr@v@2.96}\relax
   \setlength{\parfillskip}{\z@ plus 1fil}\fi
   \raggedsection\normalfont\sectfont\nobreak\size@subsection\usekomafont{foo}}%
}
%\def\foo#1{}
\makeatother

\begin{document}
\tableofcontents

\noindent\hrulefill

\section{One}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\subsection{SubOne}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\foo{FooOne}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\subsection{Two}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{document}

More and more

Added: I think this belongs to the same question, because it's, in a way, what I'm trying to achieve: how can I create a sectioning command \@startsection named section but on a different level (If I change \sectionlevel command… I still get the same output in the table of contents: it's like this command does not influence in anything).

And this is probably a different question… but is there a way to tell KOMA-Script that I want to include in the TOC only the levels until \subsection unnumbered (\setcounter{secnumdepth}{2}\setcounter{tocdepth}{2}) but I want, e.g., numbered \subsubsections (which are not in the TOC)?

EDIT

I'm not sure if this needs to be clarified… but I'm thinking that this might be an XY Problem. I came across this question because I'm writting a document where are certain points which I need to address:

  • I want to make a document which has four sectioning levels. Part, Foo (and Bar), Section and Subsection.
  • As you see, the second level has two different commands. Each Foo or Bar, has it's own Sections or Subsections (i.e., they restart the counter).
  • Part, and Foo and Bar, should not be numbered. And Section and Subsection should be numbered.
  • Moreover, Part should add a line to the TOC with the style of the default \section (I thought that just changing the \@startsection{…}{\@ne}… would suffice, but It seems not), and Foo/Bar should add a line to the TOC with the style of the default \subsection.

Of course, I can address this with what I consider “hackish”. And I came trough this question. E.g., is it possible to create a parallel sectioning command to \subsection which behaves correctly? How can I make it?

Best Answer

The question is a bit confusing. Parts that look like section, two parallel document structures. I cannot imagine a use case, that's why it is hard to implement.

Anyway, i think with a recent KOMA-version, you should be able quite easily to achieve your goal.

I just copied the default values from the KOMA-guide.

\documentclass{scrartcl}
\DeclareSectionCommand[
    afterskip=1.5ex plus .2ex,
    beforeskip=-3.25ex plus -1ex minus -.2ex,
    indent=0pt,
    level=2,
    font=\usekomafont{subsection},
    tocindent=1.5em,
    tocnumwidth=2.3em,
    counterwithin=section,
style=section]{foo}
\begin{document}
\tableofcontents
\section{Wombat}
\subsection{Walzing}
\foo{Wobbling}
\end{document}

manuelKomaDeclareSectioncommand