# Create new section commands (with correct TOC entries) in KOMA-Script

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.

\documentclass[DIV12]{scrartcl}

\makeatletter
\newcommand*{\foonumdepth}{\tw@}
\newcounter{foo}[section]
\renewcommand*{\thefoo}{\thesection.\@arabic\c@foo}
\newcommand*\foomarkformat{\thefoo\autodot\enskip}
}
\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?

• What is your foo section level supposed to do so differently compared to subsection? And the numbering of sections/subsections shall be turned off in TOC only, not in the rest of the document? – user31729 May 6 '14 at 14:48
• Technically nothing (may be spacing, and font size). But this is just part of a big image (I'm afraid that this might be an XY Problem, but I thought this question on it's own is acceptable). I would like to use in that document four section levels Part, Foo, Section, and Subsection. Only Part and Foo (which are not numbered) should add an entry to the TOC (and those entries should look like the actual \section and \subsection). And then I would redefine \section and \subsection to be numbered but in a deeper level so they don't add anything to the TOC. – Manuel May 6 '14 at 14:55

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}


This addresses only (via a hack) your second question about dropping the numbering of section and subsection. I am not really satisfied with this kind, but it works (somehow)

I redefined the section command (and subsection) to use the starred version, which does make an entry to toc and added the toc entry manually, dropping the section level numbering there.

\documentclass[DIV12]{scrartcl}
\usepackage{blindtext}%
\usepackage{etoolbox}%

\usepackage{tocloft}% For \cftpagenumber...

\let\LaTeXStandardSection\section%
\renewcommand{\section}[1]{%
\begingroup % Limit the renewcommand of \thesection
\renewcommand{\thesection}{}%
\refstepcounter{section}%
\LaTeXStandardSection*{\arabic{section} #1}
\endgroup
}%

\let\LaTeXStandardSubSection\subsection%
\renewcommand{\subsection}[1]{%
\par
\begingroup
\renewcommand{\thesubsection}{}%
\refstepcounter{subsection}%
\vspace{\baselineskip}
\LaTeXStandardSubSection*{\thesection.\arabic{subsection} #1}%
\endgroup%
}

%Enlarge the spacing a little bit
\pretocmd{\subsubsection}{\vspace{0.5\baselineskip}}

\newcounter{foo}[section]
\newcounter{subsectionsavecounter}

\newcommand{\foo}[1]{%
\setcounter{subsectionsavecounter}{\number\value{subsection}}
\setcounter{subsection}{\number\value{foo}}
%\refstepcounter{foo}% Increase before if patched \subsection is used
% Use the 'patched' \subsection command if no number entry to TOC should
% be done!
%\subsection{FOO #1}%
\LaTeXStandardSubSection{FOO #1}  %  Remove the FOO later one!
\refstepcounter{foo}% Increase afterwards if \LaTeXStandardSubSection is to be used.
\setcounter{subsection}{\number\value{subsectionsavecounter}}%
}

% If the page numbers of sections and subsections should not appear, remove the %
%\cftpagenumbersoff{section}%
%\cftpagenumbersoff{subsection}%

\begin{document}

\tableofcontents

\noindent\hrulefill

\section{One}
\blindtext
\subsection{SubOne}
\blindtext
\foo{One of some ones}

\subsection{Two}
\blindtext[2]
\subsubsection{First subsubsection ever}
\blindtext[3]

\newpage

\section{Another One}

\subsection{Yet another subsection from 2nd section}
\blindtext[1]
\subsubsection{First subsub of first sub of 2nd section}
\blindtext[2]
\foo{Yet another one of some ones}

\end{document}


I do not have a solution for your foo environment, instead of grabbing the section command as well and using an intermediate different counter for that.

EDIT I grabbed the subsection command and fooed it ;-)

Removing the section/subsection numbers looks awkward, in my point of view...

• You solve the question but a very hackish way is involved. I already thought about the starred plus manual \addxcontentsline but I wanted to know what is wrong with the “official” method to create sections. About the second ‘subquestion’, I thought of a more general way of doing it. May be patching the \@sect command defined by KOMA-Script to look for, e.g., secnummaxdepth counter in addition to the usual secnumdepth. – Manuel May 6 '14 at 19:44
• @Manuel: For sure, my solution is a crude hack. I once managed it to do additional sections the whole nine yards, but not with KOMA classes, but the standard book class. I did not want to dig that old code out of the trash bin. I could try, but I have a job in between, preventing to find solutions in the meantime. – user31729 May 6 '14 at 20:02
• I discovered it, only \l@foo is missing from the bunch of commands I declared. Moreover, the problem with numbering only certain levels is easy to solve redeclaring the \@sect command conveniently. If you want, you can answer the \l@foo part, and I accept your answer. The other part… well, I don't like it, but if you want to keep it in the answer… I don't have any problem :P – Manuel May 7 '14 at 18:45
• @Manuel: I searched for the code I mentioned in my previous comment and guess what, the key to solution was a \l@section command, in your case \l@foo. No, I am not satisfied with my 'solution', so I do not want it to be accepted. – user31729 May 8 '14 at 4:42
• That's what I propose, if you rewrite it (at least about the line in TOC, the part of \l@foo) I accept it. If not… well, I will post my own answer ;) – Manuel May 8 '14 at 8:34

I found the problem, I need to define the \l@foo command, which is the one in charge of adding the line of contents. In scrartcl.cls you can see hoy are defined each of the \l@… for each level. In my case, it's easier. Since I want it to add a line with the exact same style as \section (or whatever), it's just needed to do a

\let\l@foo\l@section


and \foo will output the exact line in TOC as \section.