# Defining custom sectioning commands

In a document of mine (scrbook class) I have to describe several different scenarios as part of a subsection. I originally used \subsubsection for each which are not numbered with the default setting of secnumdepth. I could change that counter but having something 1.2.3.1 for a scenario would not look good. A problem is that I can't proper \reference a specific scenario like this because the \label would point to the parent \subsection. Using a enumerate environment isn't really an option here as well.

I would now define a sectioning-like macro \scenario which

• Is formatted like \subsubsection* of the used class (scrbook in my case).
• Starts with Scenario \thescenario:~, where \thescenario would be a single integer (1, 2, ...) independent of the parent section numbers.
• Can be \labeled and referenced correctly using hyperrefs \autoref.

My first approach was the following:

\documentclass{scrbook}
\usepackage{hyperref}
\newcounter{scenario}
\newcommand{\scenarioautorefname}{scenario}
\newcommand{\scenario}[1]{%
\refstepcounter{scenario}%
\subsubsection*{Scenario~\thescenario: #1}%
%\refstepcounter{scenario}%
}%

\begin{document}
\chapter{MWE}
\section{Grandparent}
\subsection{Parent}
\scenario{Foo}\label{sce:foo}
...
\scenario{Bar}\label{sce:bar}
...
In \autoref{sce:foo} ...
\end{document}


This gives me the format of \subsubsection*, but the issue is that it interferes with the \refstepcounter. If I put \refstepcounter{scenario} before it, the scenario number is correct but the \autoref will use subsubsection not scenario for the reference. If I put it at the end of the macro the reference name is correct but the number is off by one, i.e. one to low. Setting the number initially to 1 gives my the correct number in the \scenario line and the correct name in \autoref but there the number is off by one again, this time one to high.

How can I define such a sectioning-like macro? I personally wouldn't mind using Koma-Script macros to do so, but would welcome a class-independent solution more.

-
I added the hyperref package to make your code compilable. –  Gonzalo Medina May 3 '11 at 15:48
@Gonzalo: Thanks, I added it myself in my local copy but forgot to update the code in the post ;-) –  Martin Scharrer May 3 '11 at 15:51

One possible solution using titlesec (to define a new seccional unit for the "Scenarios"), and hyperref:

\documentclass{scrbook}
\usepackage{titlesec}
\usepackage{hyperref}

\titleclass{\scenario}{straight}[\chapter]
\newcounter{scenario}

\setcounter{secnumdepth}{3}

\titleformat{\scenario}
{\sffamily\normalsize\bfseries}{}{0em}{Scenario \thescenario:~}
\titlespacing*{\scenario}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\newcommand{\scenarioautorefname}{scenario}

\begin{document}

\tableofcontents
\chapter{MWE}
\section{Grandparent}
\subsection{Parent}
\scenario{Foo}\label{sce:foo}
\scenario{Bar}\label{sce:bar}
...
In \autoref{sce:foo} ...
In \autoref{sce:bar} ...

\end{document}


EDIT: As Martin Scharrer mentions in the comments, with these settings each scenario has also an associated PDF bookmark at the chapter level. To control the level of the bookmarks it's enough to define \toclevel@scenario to have the appropriate value (e.g., the depth of the desired sectional unit for the bookmark).

To set the scenario toc depth, it's necessary to define \l@scenario, which will control the actual typesetting of the entries in the ToC.

As an example, to force scenario to behave as a subsubsection in both the ToC (no entry, unless the value of tocdepth is set to 3 or greater) and in the PDF bookmarks (no bookmark), something like the following lines of code need to be added to the example code above:

\makeatletter
\def\toclevel@scenario{3}
\def\l@scenario{\@dottedtocline{3}{3.8em}{3.2em}}
\makeatother

-
Thanks, but I would like to keep the normal \subsubsection around. My goal is to define a new sectioning command which inherits its format. –  Martin Scharrer May 3 '11 at 16:26
@Gonzalo: It seems that \renewcommand\thesubsubsection{\arabic{subsubsection}} is redundant, because this redefinition is already done by the non-starred version of \counterwithout. –  lockstep May 3 '11 at 16:31
@Martin Scharrer: I added a new version of the code; this time I defined a new sectional unit, thus keeping the standard \subsubsection. @lockstep: you're right. Anyway, I changed my code to satisfy the requirement in Martin's comment. –  Gonzalo Medina May 3 '11 at 16:45
@Gonzalo: In the revised version of your example, \renewcommand{\thescenario}{\arabic{scenario}} seems to be redundant. –  lockstep May 3 '11 at 16:46
This should do it. However, it is a pity that the format and spacing can't be copying from \subsubsection directly, but must be stated explicitly. –  Martin Scharrer May 3 '11 at 16:48