[Tex/LaTex] Hanging indents for \section headings

indentationsectioningsections-paragraphs

Sometimes, I do have long section headings that take more than one line. I would like to make second and each subsequent line of \section heading indented (so called 'hanging indent'). I know I can produce hanging indent for paragraphs with hanging package:

\documentclass[final]{book}
\usepackage{lipsum}
\usepackage{hanging}

\begin{document}

\hangpara{3em}{1} \lipsum[11]

\end{document}

However, I have no idea how to do the same for \section heading as hanging package works only for paragraphs and not headings. My code for section heading so far:

\documentclass[final]{book}
\usepackage{lipsum}

\begin{document}

\section*{\large{Very long section heading that takes two or more lines when it is typeset on page}}

\end{document}

Best Answer

Here is a solution using the titlesec package inspired by @egreg's comment.

The title text is indented by extra 1em. For the first line, this extra indentation is cancelled by \hspace{-1em}. With section number:

      <-- -1em
1.1   The first line of title
         the second line...
   -----> 2em

Without section number:

<-- -1em
The first line of title
   the second line...
--> 1em

Code:

\documentclass[final]{book}
\usepackage{lipsum}
\usepackage{titlesec}

\titleformat{name=\section}
  {\normalfont\Large\bfseries}{\thesection}{2em}{\hspace{-1em}}{}
\titleformat{name=\section,numberless}
  {\normalfont\Large\bfseries}{}{1em}{\hspace{-1em}}{}
\titleformat{name=\subsection}
  {\normalfont\large\bfseries}{\thesubsection}{2em}{\hspace{-1em}}{}
\titleformat{name=\subsection,numberless}
  {\normalfont\large\bfseries}{}{1em}{\hspace{-1em}}{}
\titleformat{name=\subsubsection}
  {\normalfont\bfseries}{\thesubsubsection}{2em}{\hspace{-1em}}{}
\titleformat{name=\subsubsection,numberless}
  {\normalfont\bfseries}{}{1em}{\hspace{-1em}}{}

\begin{document}

\section*{Very long section heading that takes two or more lines when it is typeset on page}
\subsection*{Very long section heading that takes two or more lines when it is typeset on page}
\subsubsection*{Very long section heading that takes two or more lines when it is typeset on page}
\lipsum

\end{document}
Related Question