0

I'm using LaTeX for academic papers, and for several different papers I'd like to use several different section types that number independently of one another. I cannot split them into parts, however, as I'd like them to mingle where they are relevant. E.g. in one paper I'd like to have text sections (down to the level of subsubsections), exercise sections (down to the level of subsections) and code snippet sections (with no sublevels).

Is there an efficient way to do this?

E.g.

1: Text section

1: Exercise section

2: Text section

2: Exercise section

1: Code section

3: Text section

2: Code section

4: Text section

3: Exercise section

3: Code section

etc.

Also, if it's possible, with some automatic formating of the titles. E.g. exercise sections in blue and code snippets in green.

3
  • 5
    Sure it is possible. But as a reader I would loath such a numbering. Section numbers are supposed to help me to find a section and I wouldn't expect code 3 behind text 4. Feb 27, 2020 at 7:58
  • For articles there are independent counts of sections, tables and figures. This doesn't do much harm because are very different beasts. Independent counts for sections, sections and sections is another scenario (that in which all beasts bitting you equally). Note that in book-like classes by default even the tables and figures counters are linked to chapter counters to help to the reader.
    – Fran
    Feb 27, 2020 at 12:27
  • You might consider treating the "non-text" sections as variants of theorems, which can easily be numbered separately. Obviously you want the body of such elements to be ordinary roman, not italic, so choose a package that provides such a choice. (amsthm is such a package.) Feb 27, 2020 at 14:31

1 Answer 1

1

Easier to show than explain. This creates a new section type and counter.

Note that \section definitions are found in and depend on the document class.

\documentclass{article}
\newcounter{code}
\makeatletter
\newcommand\code{\@startsection {code}{1}{\z@}% must use same name as counter
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}
\let\l@code=\l@section% for TOC
\let\codemark=\@gobble% no \codemark in header
\makeatother

\begin{document}
\tableofcontents

\section{first}
\section{second}
\code{first}
\code{second}
\section{third}
\code{third}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .