# Letter prefix for “sections”

Between chapter and section level I sometimes need another level, which is indicated by a letter prefix. The table of contents might look like this:

1
1.1
1.2
2
A.2.1
A.2.2
B.2.1
B.2.2
3
3.1

Now I made myself a little command to set a prefix:

\makeatletter
\newcommand{\setsectionprefix}[1]{
\renewcommand*\thesection{#1\@arabic\c@chapter.\@arabic\c@section}
\setcounter{section}{0}
}
\makeatother

The problem is with hyperref, since it generates label, which do not match \thesection. Clicking on the B.2.1 entry in the toc jumps to B.2.1, because the label is just 2.1 and hyperref takes the first one. How can I fix this?

-
As the new sectioning level is located between chapter and section, shouldn't sections be labelled as, e.g., "2.A.1" instead of "A.2.1"? Also, the (apparent) possibility of a section still directly following a chapter ("3.1") seems to be confusing. Could you describe the aim of the new intermediate level? – lockstep Aug 25 '11 at 11:06
The aim is to fulfill stupid style guidelines. Of course it does not make sense to put the letter before the numbers, but unfortunatelly the guidelines cannot be changed. – beza1e1 Aug 25 '11 at 11:10

As I understand the "stupid style guidelines", the aim is not to create a "real" intermediate sectioning level between chapter and section (there's no "A.2" and "B.2" in your example, and some sections are labelled only with chapter plus secion number, e.g., "1.1"), but to occasionally add prefixes to sections and to be able to discern sections by prefixes ("A.2.1" vs. "B.2.1"). So here's my take:

\documentclass{report}

\usepackage{etoolbox}

\newcounter{sectionprefix}
\newcounter{fakesection}
\pretocmd{\chapter}{%
\setcounter{sectionprefix}{0}%
\setcounter{fakesection}{0}%
\renewcommand{\thesection}{\thechapter.\arabic{section}}%
}{}{}
\pretocmd{\section}{\stepcounter{fakesection}}{}{}
\newcommand*{\stepsectionprefix}{%
\stepcounter{sectionprefix}%
\setcounter{fakesection}{0}%
\renewcommand{\thesection}{\Alph{sectionprefix}.\thechapter.\arabic{fakesection}}%
}
\makeatletter
\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{2.8em}}
\makeatother

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter{first}

\section{first-first}

\section{first-second}

\chapter{second}

\stepsectionprefix

\section{alpha-second-first}

\section{alpha-second-second}

\stepsectionprefix

\section{bravo-second-first}

\section{bravo-second-second}

\chapter{third}

\section{third-first}

\section{third-second}

\end{document}
-
I like this, because the prefix letter needs not be specified, but is just counted. The \l@section redefinition seems unecessary? – beza1e1 Aug 26 '11 at 9:25
@beza1e1: The redefinition achieves proper specing of section labels and names in the ToC. – lockstep Aug 26 '11 at 12:13

The answer to Defining custom sectioning commands gives you (mutatis mutandis) all the elements to define your new sectional unit with the help of the titlesec package. Here's an adaptation:

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

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

\titleformat{\Asection}
{\sffamily\Large\bfseries}{\theAsection}{0.5em}{}
\titlespacing*{\Asection}
{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}

\renewcommand\theAsection{\Alph{Asection}.\thesection}
\newcommand{\Asectionautorefname}{New section}

\makeatletter
\def\toclevel@Asection{1}
\def\l@Asection{\@dottedtocline{1}{1.5em}{2.8em}}
\makeatother

\begin{document}

\tableofcontents
\chapter{Test chapter}
\section{Test section}
\Asection{Test new level one}\label{sec:one}
\Asection{Test new level two}\label{sec:two}

In \autoref{sec:one} ...
In \autoref{sec:two} ...

\end{document}
-
Would be quite a lot of copy-paste coding, since i have the prefixes A-M and Z. – beza1e1 Aug 26 '11 at 9:20
@beza1e1: Why a lot of copy-paste coding? The \Asection command (as it is defined in my example code) has an associated counter that steps automatically and automatically produces prefixes from A to Z. – Gonzalo Medina Aug 26 '11 at 17:16

I cannot see such a problem with this example:

\documentclass{scrbook}

\let\theSection\thesection
\newcommand\setsectionprefix[1]{%
\def\thesection{\csname#1\endcsname{section}.\thechapter}}

\newcommand\restoreprefix{\let\thesection\theSection}

\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}
\usepackage{hyperref}

\begin{document}
\tableofcontents

\chapter{foo}
\section{foo}\subsection{bar}\newpage
\subsubsection{baz}\newpage
\section{foo}\subsection{bar}\subsubsection{baz}\newpage
\chapter{foo}
\setsectionprefix{Alph}
\section{foo}\subsection{bar}\newpage
\subsubsection{baz}\newpage
\section{foo}\subsection{bar}\subsubsection{baz}\newpage

\restoreprefix
\chapter{foo}
\section{foo}\subsection{bar}\newpage
\subsubsection{baz}\newpage
\section{foo}\subsection{bar}\subsubsection{baz}

\end{document}

-
Ah, you use another approach. Instead of prefixing, you just switch from numeric to alphabetic numbering and change the order. I like this, because it is much cleaner. However, it would have to be extended to switch back to normal order for numeric prefixes in chapter 3. – beza1e1 Aug 26 '11 at 9:17
@bezale1: see edit, there is now a \restoreprefix – Herbert Aug 26 '11 at 11:09

OK, I found a solution myself after looking through the hyperref code. Hyperref uses \theHsection for the label. So i can extend my macro like this:

\makeatletter
\newcommand{\setsectionprefix}[1]{
\renewcommand*\thesection{#1\@arabic\c@chapter.\@arabic\c@section}
\renewcommand*\theHsection{#1\@arabic\c@chapter.\@arabic\c@section}
\setcounter{section}{0}
}
\makeatother
-