Say I have some point in a sentence that I want the rest of the paragraph to hang after; result like this:

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

I made this work ish by calculating the width of a box containing the PART / prefix, like so:

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

I'm wondering if there is a more robust way of doing this, and whether there is a way to do it with centered text (i.e. if the text is not gonna break, then it should be centered).

Ideally something like a \hanghere command that would work like so:

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

or this syntax would also work

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

and produce the first mentioned result

Any ideas? Help is very much appreciated.

  • It would be easier with \hangat{PART} Long title... – egreg Dec 6 at 15:04
  • @egreg that syntax would definitely work! – Andreas Storvik Strauman Dec 6 at 15:10
  • If you want to center the thing when it fits a line, then both parts should be passed as arguments. – egreg Dec 6 at 16:06
up vote 2 down vote accepted

Measure the natural width and decide:

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

I use \mbox{ } in order to get rigid space.

enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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