Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
2  
Have a look at the titlesec package. –  egreg May 30 at 10:38

2 Answers 2

up vote 1 down vote accepted

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}
share|improve this answer

Section headings are controlled by internal commands such as \@startsection. Thus, you have to modify these commands. I redefined \@startsection so that it receives an additional argument (the 4th argument) which defines the amount of hanging indent.

Here is the code:

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

\makeatletter
% \@startsection{name}{level}{indent}{hangindent}{beforeskip}
%   {afterskip}{style}*[altheading]{heading}
% New argument: hangindent
\def\@startsection#1#2#3#4#5#6#7{%
  \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #5\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
  \@ifstar
    {\@ssect{#3}{#4}{#5}{#6}{#7}}%
    {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}{#7}}}}

\def\@sect#1#2#3#4#5#6#7[#8]#9{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #6\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #7{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \advance\hangindent by #4\relax
          \interlinepenalty \@M #9\@@par}%
    \endgroup
    \csname #1mark\endcsname{#8}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #8}%
  \else
    \def\@svsechd{%
      #7{\hskip #3\relax
      \@svsec #9}%
      \csname #1mark\endcsname{#8}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #8}}%
  \fi
  \@xsect{#6}}

\def\@ssect#1#2#3#4#5#6{%
  \@tempskipa #4\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #5{%
        \@hangfrom{\hskip #1}%
          \advance\hangindent by #2\relax
          \interlinepenalty \@M #6\@@par}%
    \endgroup
  \else
    \def\@svsechd{#5{\hskip #1\relax #6}}%
  \fi
  \@xsect{#4}}

\renewcommand\section{\@startsection {section}{1}{\z@}{1em}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}{1em}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\large\bfseries}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}{1em}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\normalsize\bfseries}}
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}{1em}%
                                    {3.25ex \@plus1ex \@minus.2ex}%
                                    {-1em}%
                                    {\normalfont\normalsize\bfseries}}
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}{1em}%
                                       {3.25ex \@plus1ex \@minus .2ex}%
                                       {-1em}%
                                      {\normalfont\normalsize\bfseries}}

\makeatother

\begin{document}

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

\lipsum

\end{document}

If you want to change the amount of hanging indent, change the 4th argument of \@startsection in the definition of the relevant section command. I.e. If you want to increase the hanging indent of \section to 5em, replace

\renewcommand\section{\@startsection {section}{1}{\z@}{1em}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}

with

\renewcommand\section{\@startsection {section}{1}{\z@}{5em}% <-- 5em
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}
share|improve this answer
    
Do you think there is any more straightforward way of doing this (other than re-defining \@startsection)? –  Rafal Jun 2 at 9:28
    
@Rafal I think suggestion from @egreg (using titlesec) is a better solution. –  krnk Jun 4 at 4:23
    
I agree that titlesecsolution is better (easier one) so I marked it as accepted answer, but your original answer was interesting too. –  Rafal Jun 4 at 19:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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