1

I am using a macro made by user121799 originated from Gonzalo Medina (The main code is given in the first link). The macro takes the label text of the description environment defined in enumitem package, calculates the width of the text, and adjusts labelwidth according to the width estimation, where align=parleft is added to the original code):

\newlength\widest
\makeatletter
\NewEnviron{ldescription}{%
  \vbox{%
    \global\widest=0pt%
    \def\item[##1]{%
      \settowidth\@tempdima{\textbf{##1}}%
      \ifdim\@tempdima>\widest\relax
      \global\widest=\@tempdima\fi%
    }%
    \setbox0=\hbox{\BODY}%
  }
  \begin{description}[
    align=parleft,
    leftmargin=\dimexpr\widest+0.5em\relax,
    labelindent=0pt,
    labelwidth=\widest]
  \BODY
  \end{description}%
}
\makeatother

I am curious about the width estimation part:

\@tempdima{\textbf{##1}} 

If the ##1 contains a manual linebreak, the code does not work correctly; it simply ignores \\ macro, which can be seen by the following code:

\begin{ldescription}
\item[Short] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\item[A really really long label] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\end{ldescription}

\begin{ldescription} \item[Short] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text \item[A really really\ long label] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text \end{ldescription}

What is the simplest way to estimate the width of a paragraph with linebreaks?

Hermis14
  • 423
  • you do not need to estimate the width of a paragraph you know it in advance that it is \hsize (so initially \textwidth) linebreaking happens automatically to force all lines to that length. – David Carlisle Oct 15 '21 at 09:28

1 Answers1

1

If you want to measure the width of some text that possibly involves manual linebreaks, use a tabular not a paragraph (and definitely not an \hbox)

You can replace ##1 by \begin{tabular}[t]{@{}l@{}}##1\end{tabular} in both cases.

David Carlisle
  • 757,742