\section 标题的悬挂缩进

\section 标题的悬挂缩进

有时,我确实有一些很长的章节标题,需要多行。我想使第二行和后续每一行标题\section都缩进(所谓的“悬挂缩进”)。我知道我可以使用包为段落生成悬挂缩进hanging

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

\begin{document}

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

\end{document}

但是,我不知道如何对\section标题执行相同的操作,因为hanging包仅适用于段落而不适用于标题。到目前为止,我的章节标题代码如下:

\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}

答案1

这是一个使用titlesec受@egreg 的评论启发的包的解决方案。

标题文本额外缩进 1em。对于第一行,此额外缩进由 取消\hspace{-1em}。节号如下:

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

不含章节编号:

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

代码:

\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}

答案2

章节标题由内部命令控制,例如\@startsection。因此,您必须修改这些命令。我重新定义\@startsection,以便它接收一个附加参数(第 4 个参数),该参数定义悬挂缩进量。

以下是代码:

\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}

如果要更改悬挂缩进量,请更改\@startsection相关部分命令定义中的第 4 个参数。例如,如果要将悬挂缩进量增加到\section5em,请替换

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

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

相关内容