Monday, July 10, 2023

Creating New Divisions (New Types of Sections) In LaTeX

For those who just want the answer:

\newcounter{postsection}[chapter]
\renewcommand\thepostsection{\thechapter.\Alph{postsection}}
\newcommand\postsection{\@startsection{postsection}{1}{\z@}%
   {-3.5ex \@plus -1ex \@minus -.2ex}%
   {2.3ex \@plus.2ex}%
   {\normalfont\Large\raggedright}}
\newcommand*\postsectionmark{\sectionmark}
\newcommand*\l@postsection{\l@section}

 


For the book on which I'm currently working, I want to have special sections, labelled with letters rather than numbers, in the Postscripts. So, e.g., there will be sections 9.A and 9.B. This seemed like it would be easy to do. The book.cls file, on which my custom class file is based, defines sectioning commands like this:

\newcommand\section{\@startsection {section}{1}{\z@}%
    {-3.5ex \@plus -1ex \@minus -.2ex}%
    {2.3ex \@plus.2ex}%
    {\normalfont\Large\bfseries}}

The \@startsection macro is provided by LaTeX for precisely this purpose. It's documented here. So the obvious thing to do was just replicate that definition:

\newcounter{postsection}[chapter]
\renewcommand\thepostsection{\thechapter.\Alph{postsection}}
\newcommand\postsection{\@startsection{postsection}{1}{\z@}%
   {-3.5ex \@plus -1ex \@minus -.2ex}%
   {2.3ex \@plus.2ex}%
   {\normalfont\Large\raggedright}}

This defines a new counter, formats it, and then tells the new \postsection command to use it.

Unfortunately, this led to two problems. The first is that I got an error when generating the table of contents. LaTeX uses a series of commands like \l@section and \l@subsection to do that, and so it was trying to call \l@postsection to format these ones, and that wasn't defined. That's easy to fix:

\newcommand*\l@postsection{\l@section}

The second problem was weirder. The postsection headings were being printed like this:

9.A Against Russellianism
   Against Russellianism

I.e., the title of the section was being repeated. It took me a while to figure that one out. The problem turned out to be that LaTeX was, in effect, calling \postsectionmark to set the heading of the pages with the title of the section. And, again, that wasn't defined (though, given how it is called, it didn't throw an error but just printed the argument). So solving that problem, in the end, was also easy:

\newcommand*\postsectionmark{\sectionmark}

It's a bit unclear to me why \@startsection doesn't just define sensible defaults, but there you have it.