How can I apply custom length of indent by giving a "precise word length" instead of estimate numbers under trial and error e.g. x pt
or x mm
?
1 Answer
You could use the \settowidth
macro to set the value of the \hangindent
length parameter to the width of a chosen string.
Note: If you use hanging indentation, and assuming the right-hand edges of the paragraph are justifed, it's necessaryto encase the head element -- "A short head", "A much longer head", etc -- in an \mbox
in order to keep LaTeX from stretching or shrinking any spaces that may occur in the lead text. Placing the head element in an \mbox
"freezes" the widths of the interword spaces.
\documentclass{article}
\usepackage{parskip} % no \parindent, \non-zero parskip
\newcommand\dummytext{This is a long sentence that will extend to the second line. I want the second line indented by the length of the head words.}
\begin{document}
\settowidth{\hangindent}{\textbf{A short Head: }}
\mbox{\textbf{A short Head: }}\dummytext
\settowidth{\hangindent}{\textbf{A much longer Head: }}
\mbox{\textbf{A much longer Head: }}\dummytext
\settowidth{\hangindent}{\textbf{Head: }}
\mbox{\textbf{Head: }}\dummytext
\end{document}
\settowidth
approach isn't quite sufficient: If the right-hand edges of the paragraph are justified, the actual head text has to be encased in an\mbox
in order to "freeze" the widths of any interword spaces.