2

I would like to set the description's width automatically to the widest. The solution at Automatically set description list `labelwidth` based on widest label? works for me, except if I have the mathtools package loaded.

The following MWE, which is the same as Gonzalo's but loads the mathtools package, is below:

\documentclass{article}
\usepackage{enumitem}
\usepackage{environ}
\usepackage{mathtools}

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

\begin{document}

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

\end{document}

If I remove mathtools, it works. But I need mathtools.

Xu Wang
  • 1,239
  • It doesn't like applied maths: it claims everything is 0.0pt. – cfr Nov 24 '16 at 04:10
  • This not related to mathtools directly, but mathtools load the calc package which changes how \setlength behave. This is the source of your problems. – daleif Nov 24 '16 at 13:40

1 Answers1

2

I don't understand the cause of the problem, but for some reason when mathtools is loaded the change to \widest inside the \vbox{...} is only being made locally and it is not propagating globally (You can see this by adding some \typeout{...} commands in appropriate places). One fix to to use \newdimen rather than newlength to create \widest. With dimensions, rather than lengths, you get what you want.

Here is the modified working MWE:

\documentclass{article}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{environ}

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

\begin{document}

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

\end{document}
  • Isn't the problem just that mathtools loads calc in which case making \setlength global gets difficult. – daleif Nov 24 '16 at 09:55
  • @daleif Yes, I think you're right. The calc package is the problem and calc does change how these behave with \global. –  Nov 24 '16 at 13:29
  • @daleif Do you know why calc does that? – cfr Nov 24 '16 at 22:28
  • @cfr Ask the developers, I'm guessing it is the cost of the calculating feature. Might be mentioned in the calc manual – daleif Nov 24 '16 at 22:49