Here's one approach that uses the titlesec
package for setting the section titles, and TikZ for drawing the bar. The section numbers are placed in a TikZ node that will grow automatically to accommodate the numbers:
\documentclass{book}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage{tikz}\usetikzlibrary{shapes.misc}
\newcommand\titlebar{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
\fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
\node [
fill=cyan!60!white,
anchor= base east,
rounded rectangle,
minimum height=3.5ex] at (3cm,0) {
\textbf{\arabic{chapter}.\thesection.}
};
}%
}
\titleformat{\section}{\large}{\titlebar}{0.1cm}{}
\renewcommand*{\thesection}{\arabic{section}}
\begin{document}
\chapter{First Chapter}
\section{Section name}
\lipsum[1]
\setcounter{chapter}{9999}
\setcounter{section}{105}
\section{Section name}
\lipsum[1-2]
\end{document}
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 \parbox
es 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}
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}
Best Answer