8

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: enter image description here

With mathtools, the improper output looks like: enter image description here

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

1 Answer 1

9

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}

enter image description here

3
  • 1
    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, 2018 at 5:46
  • 1
    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. Nov 21, 2018 at 5:52
  • 1
    @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, 2018 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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