# How can I force labels in description environment to stay within the margins?

The labels in description exceed the margin by default. I tried using labelwidth=\widthof{Described Item} + 2.5\labelsep and it works well. However, I would like description to handle the alignments automatically and not exceed the margins, just like enumerate and itemize.

The desired output is shown here, but I want it to be automatic rather than by manual adjustments of labelwidth

\documentclass[]{report}

\usepackage{enumitem}

\usepackage{calc}
\usepackage[showframe=true]{geometry}

\begin{document}

This is an example of a description list, which shows how elegant the \texttt{description} environment. Note how the option entry \texttt{labelwidth=$\backslash$widthof$\lbrace$Described Item$\rbrace$ + 2.5$\backslash$labelsep} aligns the description item to the paragraph

\begin{description}[labelwidth=\widthof{Described Item} + 2.5\labelsep, labelsep=5mm, align=right]
\item[Described Item] The description
\item[Described] The description
\end{description}

See how the description labels go beyond the margin:

\begin{description}[itemindent=0mm, labelsep=3mm, align=right]
\item[Described Item] The description
\item[Described] The description
\end{description}

\end{document}


Environment mydescription runs the text twice. The first run (inside a savebox) computes the needed labelwidth. Warning, any global changes will leak out. These can be disabled on a case-by-case basis.

Note that the width is measured using boldface font.

\documentclass[]{report}
\usepackage{enumitem}

%\usepackage{calc}
\usepackage[showframe=true]{geometry}
\usepackage{environ}

\newlength{\descwidth}% reserve global name

\NewEnviron{mydescription}[1][]{% #1 = optional parmeters for description
\global\descwidth=0pt
\sbox1{\def\item[##1]{\sbox0{\textbf{##1}}%
\ifdim\descwidth<\wd0 \global\descwidth=\wd0\fi}%
\let\stepcounter=\relax% % disable global change macros
\let\refstepcounter=\relax
\BODY}% compute maximum width
\description[labelwidth=\descwidth, align=right, #1]
\BODY
\enddescription}

\begin{document}

\begin{mydescription}[labelsep=5mm]
\item[Described Item] The description\footnote{test}
\item[Described] The description
\end{mydescription}

\end{document}


This solution takes two runs and affects all mydescription environments. It slowly increases the label width, then saves the final value to the aux file. The next run reads this value at \begin{document}.

\documentclass[]{report}
\usepackage{enumitem}

%\usepackage{calc}
\usepackage[showframe=true]{geometry}

\newlength{\descwidth}
\setlength{\descwidth}{0pt}
\makeatletter
\AtEndDocument{\write\@auxout{\string\global\string\descwidth=\the\descwidth}}
\makeatother

\newenvironment{mydescription}[1][]% #1 = optional parmeters for description
{\description[labelwidth=\descwidth, align=right, #1]
\let\DefaultItem=\item
\def\item[##1]{\sbox0{\textbf{##1}}%
\ifdim\descwidth<\wd0 \global\descwidth=\wd0\fi
\DefaultItem[##1]}%
}{\enddescription}

\begin{document}

\begin{mydescription}[labelsep=5mm]
\item[Described Item] The description\footnote{test}
\item[Described] The description
\end{mydescription}

\end{document}

• very nice, except that if there is more than one description list on the same page, the indentation won't be the same unless the longest item in all the lists happens to be exactly the same length. (apologies for being a troublemaker.) – barbara beeton Aug 6 '18 at 16:55
• @barbarabeeton - One could write the widths to the aux file and set a global max width when read. I'll wait until the OP asks first. – John Kormylo Aug 6 '18 at 21:25