[Tex/LaTex] How to number paragraphs (and sections) in the margin

boxesmarginsnumberingparagraphssectioning

I am (still) trying replicate the layout of this document.

Sections and paragraphs are all numbered, in the margin (the chapter titles do not treat this as a margin, but I can ignore that). They are left-aligned.

So far, Google and playing have uncovered these:

1) parano.sty
This allows me to auto-number paragraphs, which is nice. It has no flexibility for depth (i.e. sub-paragraphs) and I haven't tested moving its numbers.

2) \paragraph{}
This is the closest solution so far. Using \setcounter{secnumdepth}{5} I can add numbers to my paragraphs. This code then puts all of the numbers in the margin:

\makeatletter
\def\@seccntformat#1{\protect\makebox[0pt][r]{\csname the#1\endcsname\quad}}
\makeatother

I can make these left-aligned by increasing [0pt] and changing r to l, but I haven't been able to get the hanging indent right (or to move the box into the left margin)

Further, \paragraphs without a title leave some space (presumably to separate text from the title?), further stopping the neat alignment I am aiming for.

This solution (with the same limitations) also allows me to use sub-paragraphs, which I will eventually need.

Best Answer

After giving a look at the linked document, I could detect six sectional units: chapters, sections, numbered subsections with title, numbered subsections without title, numbered subsubsections with title, and numbered subsubsections without title. So the problem is how to define these six sectional units.

The code below shows a possible solution using the titlesec package; the underlaying idea is to use \llap and \parboxes to typeset the numbers hanging onto the margin. The length \titleindent allows you to easily control the hanging indent.

\paragraph has been changed so that it will allow numbered subsections without title and \subparagraph was also redefined to allow numbered subsubsections.

The following table shows the "dictionary" between each secional unit of the document and the LaTeX command that will be used to typeset it:

  • Chapters -> \chapter

  • Sections -> \section

  • Numbered subsections with title -> \subsection

  • Numbered subsections without title -> \paragraph

  • Numbered subsubsections with title -> \subsubsection

  • Numbered subsubsections without title -> \subparagraph

And here's the code (feel free to make the necessary adjustments to satisfy your requirements):

\documentclass{book}
\usepackage{titlesec}
\usepackage{etoolbox}
\usepackage{lipsum}

\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}

% this length controls tha hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{2cm}

\pretocmd{\paragraph}{\stepcounter{subsection}}{}{}
\pretocmd{\subparagraph}{\stepcounter{subsubsection}}{}{}

\titleformat{\chapter}[block]
  {\normalfont\huge\bfseries}{}{0pt}{\hspace*{-\titleindent}}

\titleformat{\section}
  {\normalfont\Large\bfseries}{\llap{\parbox{\titleindent}{\thesection\hfill}}}{0em}{}

\titleformat{\subsection}
  {\normalfont\large}{\llap{\parbox{\titleindent}{\thesubsection\hfill}}}{0em}{\bfseries}

\titleformat{\subsubsection}
  {\normalfont\normalsize}{\llap{\parbox{\titleindent}{\thesubsubsection}}}{0em}{\bfseries}

\titleformat{\paragraph}[runin]
  {\normalfont\large}{\llap{\parbox{\titleindent}{\thesubsection\hfill}}}{0em}{}

\titleformat{\subparagraph}[runin]
  {\normalfont\normalsize}{\llap{\parbox{\titleindent}{\thesubsubsection\hfill}}}{0em}{}

\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}

\begin{document}

\chapter{Test chapter}
\section{Test Section}
\lipsum[2]
\subsection{Test Subsection}
\lipsum[2]
\paragraph{}% acts like a numbered subsection without title
\lipsum[2]
\subsubsection{Test Subsubsection}
\lipsum[2]
\subparagraph{}% acts like a numbered subsubsection without title
\lipsum[2]

\end{document}

enter image description here

The previous approach assumes that you won't use \paragraph and \subparagraph with their standard formatting; another (perhaps more sensible) option is to define two fresh new sectional units; this, of course, requires a little additional work, but now you will have the standard sectional units plus the two new ones.

The following code illustrates this approach defining \subsectionwt (for numbered subsections without titles) and \subsubsectionwt (for numbered subsubsections without titles); it also makes the necessary provisions for the ToC.

The new "dictionary" is now:

  • Chapters -> \chapter

  • Sections -> \section

  • Numbered subsections with title -> \subsection

  • Numbered subsections without title -> \subsectionwt

  • Numbered subsubsections with title -> \subsubsection

  • Numbered subsubsections without title -> \subsubsectionwt

  • Paragraphs -> \paragraph

  • Subparagraphs -> \subparagraph

The example code:

\documentclass{book}
\usepackage{titlesec,titletoc}
\usepackage{etoolbox}
\usepackage{lipsum}

\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}

% this length controls tha hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{2cm}

% counters for the new sectional units
\newcounter{subsectionwt}
\newcounter{subsubsectionwt}

% definition of the new sectional units with the representation of the counters
\titleclass{\subsectionwt}{straight}[\subsubsection]
\renewcommand{\thesubsectionwt}{\thesection.\arabic{subsectionwt}}

\titleclass{\subsubsectionwt}{straight}[\subsectionwt]
\renewcommand{\thesubsubsectionwt}{\thesubsection.\arabic{subsubsectionwt}}

% \subsection must increase the subsectionwt counter
% and \subsectionwt must increase the subsection counter
% Analogous treatment for \subsubsection and \subsubsectionwt
\pretocmd{\subsectionwt}{\stepcounter{subsection}}{}{}
\pretocmd{\subsection}{\stepcounter{subsectionwt}}{}{}
\pretocmd{\subsubsectionwt}{\stepcounter{subsubsection}}{}{}
\pretocmd{\subsubsection}{\stepcounter{subsubsectionwt}}{}{}

% format for the sectional units
\titleformat{\chapter}[block]
  {\normalfont\huge\bfseries}{}{0pt}{\hspace*{-\titleindent}}

\titleformat{\section}
  {\normalfont\Large\bfseries}{\llap{\parbox{\titleindent}{\thesection\hfill}}}{0em}{}

\titleformat{\subsection}
  {\normalfont\large}{\llap{\parbox{\titleindent}{\thesubsection\hfill}}}{0em}{\bfseries}

\titleformat{\subsectionwt}[runin]
  {\normalfont\large}{\llap{\parbox{\titleindent}{\thesubsectionwt\hfill}}}{0em}{\bfseries}

\titleformat{\subsubsection}
  {\normalfont\normalsize}{\llap{\parbox{\titleindent}{\thesubsubsection}}}{0em}{\bfseries}

\titleformat{\subsubsectionwt}[runin]
  {\normalfont\normalsize}{\llap{\parbox{\titleindent}{\thesubsubsectionwt}}}{0em}{\bfseries}

\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsectionwt}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsectionwt}{0pt}{3.25ex plus 1ex minus .2ex}{0em}

% numbered entries without title shouldn't go to the ToC
\titlecontents{subsectionwt}[]{}{}{}{}
\titlecontents{subsubsectionwt}[]{}{}{}{}

\begin{document}

\tableofcontents

\chapter{Test chapter}
\section{Test Section}
\lipsum[2]
\subsection{Test Subsection}
\lipsum[2]
\subsectionwt{}% for numbered subsections without title
\lipsum[2]
\subsubsection{Test Subsubsection}
\lipsum[2]
\subsubsectionwt{}% for numbered subsubsections without title
\lipsum[2]

\end{document}