How can I number paragraphs (and sections) in the margin?

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}

For KOMA-Script classes you do not need an additional package. At KOMA-Script classes the numbers of sectioning titles are printed by \partformat, \chapterformat, \sectionformat, \subsectionformat\subparagraphformat. So I first suggestion could be to redefine these commands:

\documentclass{scrreprt}
\usepackage{blindtext}

\setcounter{secnumdepth}{\paragraphnumdepth}% To show the result for all
                                            % levels down to paragraph

\newcommand*{\numberinmargin}[1]{%
  \makebox[0pt][r]{#1\autodot\hskip\marginparsep}}

\renewcommand*{\chapterformat}{\numberinmargin{\thechapter}}
\renewcommand*{\sectionformat}{\numberinmargin{\thesection}}
\renewcommand*{\subsectionformat}{\numberinmargin{\thesubsection}}
\renewcommand*{\subsubsectionformat}{\numberinmargin{\thesubsubsection}}
\renewcommand*{\paragraphformat}{\numberinmargin{\theparagraph}}

\begin{document}
\chapter{A Chapter Title}
\blindtext
\section{A Section Title}
\blindtext
\subsection{A Subsection Title}
\blindtext
\subsubsection{A Subsubsection Title}
\blindtext
\paragraph{A Paragraph Title}
\blindtext
\end{document}

right aligned numbers

You can left align the numbers by using another definition of \numberinmargin, e.g.:

\newcommand*{\numberinmargin}[1]{%
  \makebox[0pt][r]{%
    \makebox[\marginparwidth][l]{#1\autodot}%
    \hskip\marginparsep
  }%
}

would result in:

left aligned chapter … paragraph

But if you use the same code for \subparagraph:

\setcounter{secnumdepth}{\subparagraphnumdepth}
\renewcommand*{\subparagraphformat}{\numberinmargin{\thesubparagraph}}

in the document preamble and

\subparagraph{A Subparagraph Title}
\blindtext

in the body you will find, that it does not work:

failure

Why does it fail? The default of \subparagraph is to indent the heading. So the \makebox[0pt][r]{…}, that prints the content (= the number) right from the current position, now does not print from the left edge of the text area but from the indent position.

We could work around the issue by removing the indent:

\RedeclareSectionCommand[indent=0pt]{subparagraph}

subparagraph without indent

But if we want to respect the indent, that could also be configured for other section levels, we have make a step down the stairs to a lower level interface of KOMA-Script and redefine \sectionlinesformat and \sectioncatchphraseformat.

\sectionlinesformat is used to format the sectioning titles of section levels, that are printed displayed. By default these are \section, \subsection and \subsubsection. The default definition (given in the KOMA-Script manual) is:

\newcommand{\sectionlinesformat}[4]{%
  \@hangfrom{\hskip #2#3}{#4}%
}

The first argument it the name of the sectioning level, i. e., section, subsection, subsubsection. The second argument is the indent. The third argument is the number given by \…format and the fourth argument is the title text. \@hangfrom is used to format a hanging title for multi-line titles. Assume we want the number in the margin but the indent in the text area, we can move #2' to the front still using the redefined\…format` commands:

\renewcommand{\sectionlinesformat}[4]{%
  #3\@hangfrom{\hskip #2}{#4}%
}

\sectioncatchpraseformat is used for section headings that at printed in-text as a catch phrase. Usually this is used for \paragraph and \subparagraph. The default is similar to \sectionlinesformat but without \@hangfrom:

\newcommand{\sectioncatchphraseformat}[4]{%
  \hskip #2#3#4%
}

The redefinition would be similar, too:

\renewcommand{\sectioncatchphraseformat}[4]{%
  #3\hskip #2#4%
}

Let's use a right aligned version of \numbersinmargin again:

\documentclass{scrreprt}
\usepackage{blindtext}

\setcounter{secnumdepth}{\subparagraphnumdepth}% To show the result for all
                                               % levels down to subparagraph

\newcommand*{\numberinmargin}[1]{%
  \makebox[0pt][r]{%
    \makebox[\marginparwidth][r]{#1\autodot}%
    \hskip\marginparsep
  }%
}

\renewcommand*{\chapterformat}{\numberinmargin{\thechapter}}

\renewcommand*{\sectionformat}{\numberinmargin{\thesection}}
\renewcommand*{\subsectionformat}{\numberinmargin{\thesubsection}}
\renewcommand*{\subsubsectionformat}{\numberinmargin{\thesubsubsection}}
\renewcommand*{\paragraphformat}{\numberinmargin{\theparagraph}}
\renewcommand*{\subparagraphformat}{\numberinmargin{\thesubparagraph}}
\makeatletter% because of @ in \@hangfrom
\renewcommand{\sectionlinesformat}[4]{%
  #3\@hangfrom{\hskip #2}{#4}%
}
\makeatother% switch back from \makeatletter
\renewcommand{\sectioncatchphraseformat}[4]{%
  #3\hskip #2#4%
}

\begin{document}
\chapter{A Chapter Title}
\blindtext
\section{A Section Title}
\blindtext
\subsection{A Subsection Title}
\blindtext
\subsubsection{A Subsubsection Title}
\blindtext
\paragraph{A Paragraph Title}
\blindtext
\subparagraph{A Subparagraph Title}
\blindtext
\end{document}

indent of subparagraph

As you can see, the \subparagraph title is indented now, without interfering the position of the number.

If you want to handle also chapter title with prefix line (see option chapterprefix in the KOMA-Script manuals) or part titles you need additional definitions. For example for prefix lines you could move the number to the margin like above but the term Chapter stay in the text area:

\renewcommand*{\chapterformat}{%
  \numberinmargin{\thechapter}\chapappifchapterprefix{}%
}

If you now add option chapterprefix to \documentclass you will get:

chapter with prefix line

Let me say: I do not like this result and would suggest not to use chapterprefix.

For part titles you could do similar, but I also do not like the result, so I do not show it.