# Why conflict between mathtools and Gonzalo's solution for auto-adjusting description environment?

Gonzalo Medina provided a nifty solution to "Automatically set description list labelwidth based on widest label?." (The goal was to define a description environment where the item text is automatically indented to the width of the widest label.)

I could replicate its success on a standalone basis, but when I incorporated it into my standard set of packages, it no longer produced the desired results. By a process of elimination, I discovered that simply loading the mathtools package was sufficient to cause the problem.

I don't know where to begin to search for the conflict or a workaround, but I'm hoping someone with more experience, with mathtools in particular, would spot it quickly.

Below is the MWE, straight out of Gonzalo's solution, except that I've inserted a commented-out \usepackage{mathtools}. (I've experimented with the order of loading mathtools but nothing solved the problem.)

Without mathtools, the proper output looks like:

With mathtools, the improper output looks like:

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


After loading the length \widest gets "reseted". This is not really triggered by mathtools but by calc, which gets loaded by mathtools, as explained in this great answer. It is then easy to solve the problem: set the distance using = rather than \setlength.

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

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


• I revised this answer quite a bit because I learned a lot from egreg's nice answer. Imagine how much I could learn from egreg's question, if he had one. ;-) – user121799 Nov 21 '18 at 5:46
• Wow. Super answer; both very interesting. (I keep discovering, mostly negatively, that commands are redefining things I rely on without my knowledge!) And very helpful. This saves the enterprise just by changing a few characters. – Jim Ratliff Nov 21 '18 at 5:52
• @JimRatliff Thanks! I actually did not know any of this, so thanks a lot for this really nice question! (How did I get on track? I just added \typeouts at many positions of your/Gonzalo's code, which revealed that \widest got resetted. Then I did a google search with site:tex.stackexchange.com mathtools setlength, which lead me right to egregs answer. This kind of strategy, i.e. \typeouts and google searches with site:tex.stackexchange.com, can sometimes be useful.) – user121799 Nov 21 '18 at 5:59