3

I am working on a document in which the numbers of headers are displayed in a left margin, e.g.

1     Main header
      some text within the given section.

1.1   A Subheader
      Some text within the subheader.

I found on the web somewhere, the following code, which achieves this

\newlength\titleindent
\setlength\titleindent{1.35cm}

\titleformat{\chapter}[block]
  {\normalfont\huge\bfseries\center}{}{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}{\theparagraph\hfill}}}{0em}{}

\titleformat{\subparagraph}[runin]
  {\normalfont\normalsize}{\llap{\parbox{\titleindent}{\theparagraph\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}

What I need is for there to be some way, that I can add such numbers to text sections too. Imagine something like this

\section{Main header}
some text within the given section.

\subsection{A Subheader}
\subsubsection{} Some text within the subheader

would produce

1      Main header
       some text within the given section.

1.1    A Subheader
1.1.1  Some text within the subheader.

That is what I want. This should work no matter what level I do it at. In this case it was a subsubsection, but it might as well have been a paragraph

What I actually get with the current preamble is

1      Main header
       some text within the given section.

1.1    A Subheader
1.1.1  
       Some text within the subheader.

That is, the subsubsection with the empty title still gets its own line, and the paragraph is shown below.

The tex markup need not be exactly like this, with empty headers. The important part is that

  • Numbers are shown in a left margin
  • Hiearchy is preserved so the TOC looks correct (and respects levels)
  • \ref{...} Will insert the correct number

EDIT: The issue bears some similarities with Numbering of subsections and paragraphs but I cannot connect the dots myself

3

Use the explicit option for titlesec; now you can grab the sectional title unit using #1 and you can perform a conditional test: if the title is empty, add some negative vertical spacing; otherwise, typeset the title. The idea, illustrated for sections, subsections and subsubsections (you can easily modify the remaining levels using my code as reference):

\documentclass{book}
\usepackage[explicit]{titlesec}

\setcounter{secnumdepth}{5}

\newlength\titleindent
\setlength\titleindent{1.35cm}

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

\titleformat{\section}
  {\normalfont\Large\bfseries}
  {\llap{\parbox[t]{\titleindent}{\thesection\hfill}}}{0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr2.3ex+0.5\baselineskip\relax}}
    \else#1
    \fi
  }

\titleformat{\subsection}
  {\normalfont\large}{\llap{\parbox[t]{\titleindent}{\thesubsection\hfill}}}
  {0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
    \else\bfseries#1
    \fi
  }

\titleformat{\subsubsection}
  {\normalfont\normalsize}{\llap{\parbox[t]{\titleindent}{\thesubsubsection}}}
  {0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
    \else\bfseries#1
    \fi
  }

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

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

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

\begin{document}

\chapter{A test chapter}
Some test text. And cross-references to~\ref{sec:test}, \ref{ssec:test} and~\ref{sssec:test}
\section{A test section}
Some test text.
\section{}
\label{sec:test}
Some test text.
\subsection{A test subsection}
Some test text.
\subsection{}
\label{ssec:test}
Some test text.
\subsubsection{A test subsubsection}
Some test text.
\subsubsection{}
\label{sssec:test}
Some test text.

\end{document}

enter image description here

As a side note, your \parboxes for the numbers should have t alignment to get the proér vertical positioning (I used this in my exmaple code for the modified sectional units).

2
  • perfect. Just what I needed. One thing though. The TOC now contains untitled sections. If I wanted it to only include those with a title, how would I approach that? – Eldamir Mar 10 '15 at 7:10
  • I created my followup question as a standalone question instead: tex.stackexchange.com/questions/232341/… – Eldamir Mar 10 '15 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.