1

I am new here, and trying to modify the format of the chapters using the package titlesec.

I am trying to add a horizontal line between the chapter number and the chapter title, something similar to this: enter image description here

So far I managed to add the chapter number, chapter title, however I have no idea how to have that line with 2 different thickness.

I want to have Chapter number and the thicker part to the right, and chapter title to the left. The thickness of the title rule could be 2pt, and the thickner part 8pt (relation 1:4).

Here is my MWE:

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}        
\usepackage[utf8]{inputenc}     
\usepackage[a4paper,left=3cm,right=2.5cm,top=3cm,bottom=3cm, twoside]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{epigraph}
\usepackage{titletoc}
\usepackage{titlesec} 
\usepackage{blindtext}  
\colorlet{maincolor}{MidnightBlue}

% *****  Title format for Chapters *******
    \titleformat{\chapter}[display]
    {\normalfont\sffamily\Large\bfseries\color{maincolor}}
    {\filleft\Huge\color{maincolor}
        \begin{tikzpicture}[baseline={([yshift=-1.0ex]current bounding box.center)}]
            \node {\thechapter};
        \end{tikzpicture}
    }
    {1ex}{\titlerule[2pt]\vspace*{1.5ex}\sffamily\Huge\color{maincolor}}

% ***** Title format for the chapter without numbers *******
    \titleformat{name=\chapter,numberless}[display]
    {\normalfont\sffamily\Large\bfseries\color{maincolor}}
    {}{-2ex}{\vspace{-10mm}\sffamily\Huge\color{maincolor}}

% *****  Title format for sections *******
\titleformat{\section}[hang]    
{\normalfont\sffamily\large\bfseries\color{maincolor}} 
{\makebox[2em]{\normalfont\sffamily\large\bfseries\color{black}\thesection.}}
{1em}{}[]

% *****  Title format for subsections *******
\titleformat{\subsection}[hang]{\large\sffamily\color{maincolor}}
{\makebox[2em]{\large\sffamily\color{black}\thesubsection.}} 
{5pt}{}[]      
\begin{document}
\chapter{Introduction}
\epigraph{You can’t do better design with a computer, but you can speed up your work enormously.}{--Wim Crouwel}

\section{Aim of the Project}
\Blindtext

\subsection{Objectives}
\blindenumerate[5]

\section{Motivation} \label{Introoceanenergy}
\Blindtext

\section{Challenges of Wave Energy Systems}
\Blindtext

\section{Report Outline}
\blindtext
\blindenumerate[5]

\end{document}

Here is what I get: enter image description here

Also, in order to make the most of this question, how could I expand the phrase to have a width similar to the one showed in the previous snippet? (note the difference between the width from the phrase from Chapter 3, and the width of the phrase I get with my code.)

4
  • Welcome to TeX SX! Do you want the chapter number on the left or on he right? And the thicker part of the rule sould be on which side, if the chapter number is on the right?
    – Bernard
    Commented Aug 11, 2021 at 15:45
  • Hi Bernard, Chapter number and the thicker part to the right, chapter title to the left. And the thickness of the title rule could be 2pt, and the thickner part 8pt (relation 1:4). Thanks! Commented Aug 11, 2021 at 15:59
  • Do you really want a sledge-hammer like TiKZ to display the chapter number?
    – Bernard
    Commented Aug 11, 2021 at 16:14
  • It is not that I want; it is just that was the option that I could find. Is there any other way to make this solution better? Commented Aug 11, 2021 at 16:42

2 Answers 2

1

I don't think that \titlerule supports this, but you can use the primitive \hrule.

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}        
\usepackage[utf8]{inputenc}     
\usepackage[a4paper,left=3cm,right=2.5cm,top=3cm,bottom=3cm, twoside]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{epigraph}
\usepackage{titletoc}
\usepackage{titlesec} 
\usepackage{blindtext}  

\colorlet{maincolor}{MidnightBlue}

% *****  Title format for Chapters *******
\titleformat{\chapter}[display]
  {\normalfont\sffamily\Large\bfseries\color{maincolor}}
  {%
   \filleft\Huge\color{maincolor}%
   \begin{tikzpicture}[baseline={([yshift=-1.0ex]current bounding box.center)}]
     \node {\thechapter};
   \end{tikzpicture}%
  }
  {1ex}
  {%
   \hrule height 2pt \hrule height 4pt width 0.25\textwidth
   \vspace*{1.5ex}%
   \Huge\color{maincolor}%
  }

% ***** Title format for the chapter without numbers *******
\titleformat{name=\chapter,numberless}[display]
  {\normalfont\sffamily\Large\bfseries\color{maincolor}}
  {}
  {0pt}
  {\Huge\color{maincolor}}

% *****  Title format for sections *******
\titleformat{\section}[hang]    
  {\normalfont\sffamily\large\bfseries\color{maincolor}} 
  {\makebox[2em][l]{\color{black}\thesection.}}
  {0pt}
  {}

% *****  Title format for subsections *******
\titleformat{\subsection}[hang]
  {\large\sffamily\color{maincolor}}
  {\makebox[3em][l]{\color{black}\thesubsection.}} 
  {0pt}
  {}

\begin{document}

\chapter*{Preface}

\chapter{Introduction}

\section{Aim of the Project}

\subsection{Objectives}

\end{document}

I made some changes to the code for sections and subsections: the [l] option is necessary or the numbers would be indented.

enter image description here

If you want the thicker rule on the right, change the code into

\titleformat{\chapter}[display]
  {\normalfont\sffamily\Large\bfseries\color{maincolor}}
  {%
   \filleft\Huge\color{maincolor}%
   \begin{tikzpicture}[baseline={([yshift=-1.0ex]current bounding box.center)}]
     \node {\thechapter};
   \end{tikzpicture}%
  }
  {1ex}
  {%
   \hrule height 2pt
   \moveright0.75\textwidth\vbox{\hrule height 4pt width 0.25\textwidth}
   \vspace*{1.5ex}%
   \Huge\color{maincolor}%
  }

enter image description here

1

Here is a solution which doesn't require Tikz:

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc} no more needed
\usepackage[a4paper,left=3cm,right=2.5cm,top=3cm,bottom=3cm, twoside]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{epigraph}
\usepackage{titletoc}
\usepackage{titlesec}
\usepackage{blindtext}
\colorlet{maincolor}{MidnightBlue}

% ***** Title format for Chapters *******
    \titleformat{\chapter}[display]
    {\normalfont\sffamily\Large\bfseries\color{maincolor}}
    {\filleft\Huge\thechapter\enspace }
    {1ex}{\titlerule[2pt]{\hfill\raisebox{1ex}[0pt]{\rule{60mm}{8pt}}}\vskip1ex\Huge}

% ***** Title format for the chapter without numbers *******
    \titleformat{name=\chapter,numberless}[display]
    {\normalfont\sffamily\Large\bfseries\color{maincolor}}
    {}{-2ex}{\vspace{-10mm}\sffamily\Huge\color{maincolor}}

% ***** Title format for sections *******
\titleformat{\section}[hang]
{\normalfont\sffamily\large\bfseries\color{maincolor}}
{\makebox[2em]{\normalfont\sffamily\large\bfseries\color{black}\thesection.}}
{1em}{}[]

% ***** Title format for subsections *******
\titleformat{\subsection}[hang]{\large\sffamily\color{maincolor}}
{\makebox[2em]{\large\sffamily\color{black}\thesubsection.}}
{5pt}{}[]
\begin{document}
\chapter{Introduction}
\epigraph{I refuse to join any club that would have me as a member.}{-- Groucho Marx}

\section{Aim of the Project}
\Blindtext

\subsection{Objectives}
\blindenumerate[5]

\section{Motivation} \label{Introoceanenergy}
\Blindtext

\section{Challenges of Wave Energy Systems}
\Blindtext

\section{Report Outline}
\blindtext
\blindenumerate[5]

\end{document} 

enter image description here

2
  • Thanks for your answer! It looks nicer and cleaner without the tikzpicture environment. Quick question, Why is that \usepackage[utf8]{inputenc} not more needed? Commented Aug 12, 2021 at 12:01
  • @Choguerrero: It's been 3 years that it is the encoding expected by the LaTeX kernel.
    – Bernard
    Commented Aug 12, 2021 at 12:17

You must log in to answer this question.

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