[Tex/LaTex] same space between number and text in chapters/sections

indentationsectioningtitlesec

I have been googleing for several hours now, my goal is to have the same indent for each chapter/section. Meaning there is the number but every chapter starts for example at 3cm from the left margin. Do you know what I mean? 🙂

I am using the titlesec package to customize fontsize, color and so on but I havent managed to get the spacing right. Therefore I tried to do it with two parboxes like that:

    \titleformat{\section}
      {\color{pt_Blau_04}\large\sffamily\bfseries}
      {}
      {0em}
      {\parbox{1cm}{\flushleft\thesection}{\parbox{6cm}{\flushleft #1}}}
      [\vspace*{5pt}]

If the chapter title is now doublespaced it looks really ugly and LaTeX just throws the parboxes around..

Do you guys know how to do it so every chapter/section/subsection starts at the same point?

Best Answer

Your idea is good, but you only have to use the appropriate lengths. If the box for the numbers has width x then the box for the titles has to have width equal to \textwidth-x. In the following example, for the numbers I used a \makebox of width \mylena (initially set to 1.5cm) and for the titles a \parbox with \raggedright contents and width \mylenb=\textwidth-\mylena. I used some definitions based on your snippet, but of course you can use your settings (notice that you'll need two separate, yet similar, definitions: one for numbered units and the other, for the unnumbered ones (through the numberless key)):

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

\newlength\mylena
\newlength\mylenb
\setlength\mylena{1.5cm}
\setlength\mylenb{\dimexpr\textwidth-\mylena\relax}

\newcommand\PlaceNumber[1]{%
  \makebox[\mylena][l]{#1}}

\colorlet{pt_Blau_04}{cyan!60!black}

\titleformat{\chapter}
      {\color{pt_Blau_04}\huge\sffamily\bfseries}
      {}
      {0em}
      {\PlaceNumber{\thechapter}\parbox[t]{\mylenb}{\raggedright#1}}
\titleformat{name=\chapter,numberless}
      {\color{pt_Blau_04}\huge\sffamily\bfseries}
      {}
      {0em}
      {\PlaceNumber{}\parbox[t]{\mylenb}{\raggedright#1}}
\titleformat{\section}
      {\color{pt_Blau_04}\large\sffamily\bfseries}
      {}
      {0em}
      {\PlaceNumber{\thesection}\parbox[t]{\mylenb}{\raggedright#1}}
      [\vspace*{5pt}]
\titleformat{name=\section,numberless}
      {\color{pt_Blau_04}\large\sffamily\bfseries}
      {}
      {0em}
      {\PlaceNumber{}\parbox[t]{\mylenb}{\raggedright#1}}
      [\vspace*{5pt}]
\titleformat{\subsection}
      {\color{pt_Blau_04}\large\sffamily\bfseries}
      {}
      {0em}
      {\PlaceNumber{\thesubsection}\parbox[t]{\mylenb}{\raggedright#1}}
\titleformat{name=\subsection,numberless}
      {\color{pt_Blau_04}\large\sffamily\bfseries}
      {}
      {0em}
      {\PlaceNumber{}\parbox[t]{\mylenb}{\raggedright#1}}

\begin{document}

\chapter{Test Chapter with some additional text to see the indentation}
\section{Test Numbered Section}
\section*{Test Unnumbered Section}
\subsection{Test Numbered  Subsection}
\subsection*{Test Unnumbered  Subsection}

\end{document}

enter image description here

Related Question