悬挂在章节标题内(在文档和目录中(如有必要))

悬挂在章节标题内(在文档和目录中(如有必要))

通常,长章节标题如下所示:

Chapter 1

This is a very long chapter heading; it is so long that is has a line break, but that’s
okay

现在,我想按以下方式进行更改(在文档内以及在目录中(如果需要)):

Chapter 2

Attention: This is also a very long chapter heading, but now, I would like it to continue
           after "Attention: ".

另一个例子:

Chapter 3

A: This is a very long chapter heading; but now, I would like to continue after "A: " so
   that it looks like this.

使用\\\phantom{Attention: }不起作用。

完整代码:

\documentclass[12pt,a4paper]{report}
\usepackage[UKenglish]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[activate={true,nocompatibility},final,tracking=false,kerning=true,spacing=true,factor=1100,stretch=10,shrink=10]{microtype}
\DisableLigatures[f]{encoding = T1}
\usepackage{etoolbox}
\microtypecontext{spacing=nonfrench}
\usepackage{geometry}
\usepackage{hanging}
\newlength{\hangwidth}
\newcommand{\myhang}[1]{\settowidth{\hangwidth}{#1}\hangpara{\hangwidth}{1}#1}
\geometry{
    left=2cm,
    right=2cm,
    top=2cm,
    bottom=2cm,
    bindingoffset=0mm
}
\begin{document}
    \begin{titlepage}
        \vspace*{2cm}
        \centering
        {\scshape\LARGE Title page\par}
    \end{titlepage}

    \cleardoublepage
    \pagenumbering{Roman}
    \tableofcontents

    \cleardoublepage
    \pagenumbering{arabic}

    \setcounter{chapter}{0}
    \chapter{This is a very long caption; I do not care about line breaks here}
    \chapter{Attention: Here, I definitely care about line breaks; even within the table of contents}
\end{document}

答案1

由于\hangindent必须在段落开始前使用,因此我改用了\parbox。棘手的是让页码与底部对齐而不是顶部。由于目录条目写入 AUX 文件,因此我将格式说明传递为\string\hangtoc

\documentclass[12pt,a4paper]{report}
\usepackage[UKenglish]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[activate={true,nocompatibility},final,tracking=false,kerning=true,spacing=true,factor=1100,stretch=10,shrink=10]{microtype}
\DisableLigatures[f]{encoding = T1}
\usepackage{etoolbox}
\microtypecontext{spacing=nonfrench}
\usepackage{geometry}

\geometry{
    left=2cm,
    right=2cm,
    top=2cm,
    bottom=2cm,
    bindingoffset=0mm
}

\newcommand{\hangtitle}[2]% #1=text for hang, #2=rest of title
 {\sbox0{#1}\usebox0\parbox[t]{\dimexpr \textwidth-\wd0}{\raggedright#2\strut}}

\newcommand{\hangtoc}[2]% #1=text for hang, #2=rest of title
 {\sbox0{#1}\usebox0%
  \parbox[t]{\dimexpr \textwidth-\wd0-\leftskip-\rightskip}{\raggedright#2\strut}%
  \vspace{-\baselineskip}\linebreak\hspace*{\fill}}

\newcommand{\mychapter}[2]% #1=text for hang, #2=rest of title
  {\chapter[\string\hangtoc{#1}{#2}]{\hangtitle{#1}{#2}}}

\begin{document}
    \begin{titlepage}
        \vspace*{2cm}
        \centering
        {\scshape\LARGE Title page\par}
    \end{titlepage}

    \cleardoublepage
    \pagenumbering{Roman}
    \tableofcontents

    \cleardoublepage
    \pagenumbering{arabic}

    \setcounter{chapter}{0}
    \chapter{This is a very long caption; I do not care about line breaks here}
    \mychapter{Attention: }{Here, I definitely care about line breaks; even within the table of contents}
\end{document}

相关内容