将段落的其余部分放在特定点(或标记)之后

将段落的其余部分放在特定点(或标记)之后

假设我在一个句子中有一个要点,我想让该段落的其余部分挂在其后;结果如下:

PART / Long title that won't fit on one line that
       we want hanged after the slash on the next line

我通过计算包含前缀的框的宽度来完成这项工作PART /,如下所示:

\documentclass{article}
% Prevent extra indent
\setlength\parindent{0pt}
% Box to contain the stuff that the text will hang after
\newbox\partprefixBox
% The stuff that the text will hang after
\def\partprefix{\Large PART /\nobreakspace}
% Set part-header-prefix into the box
\setbox\partprefixBox=\hbox{\partprefix}
\begin{document}
    \bgroup
     % Set the hangindent size (using the box width)
    \hangindent=\wd\partprefixBox%
    \hangafter=1%
    \partprefix Long title that won't fit on one line that we want hanged after the slash on the next line\par
    \egroup
\end{document}

我想知道是否有更强大的方法来做到这一点,以及是否有办法使用居中文本来实现这一点(即,如果文本不会中断,那么它应该居中)。

理想情况下,类似\hanghere这样的命令可以工作:

PART / \hanghere Long title that won't fit on one line that we want hanged after the slash on the next line

或者这个语法也可以

\hanghere{PART / }Long title that won't fit on one line that we want hanged after the slash on the next line

并产生第一个提到的结果

有什么想法吗?非常感谢您的帮助。

答案1

测量自然宽度并决定:

\documentclass{article}

\newlength{\partlength}

\newcommand{\perhapshang}[2]{%
  \par\noindent
  \sbox0{#1 / #2}%
  \ifdim\wd0<\linewidth
    \makebox[\linewidth]{\usebox0}%
  \else
    \settowidth{\partlength}{#1\mbox{ }/\mbox{ }}%
    \hangafter 1
    \hangindent\partlength
    #1\mbox{ }/\mbox{ }#2
  \fi
  \par
}

\begin{document}

\perhapshang{PART}{Long title that won't fit on one line
  that we want hanged after the slash on the next line}

\perhapshang{PART}{This is short}

\end{document}

我使用是\mbox{ }为了获得刚性空间。

在此处输入图片描述

相关内容