11

I am trying to learn how to use enumitem's keys to adjust a description list. In this case, I would like to have my list items and description text both left aligned, but the description text should all start at the same horizontal position.

What I have below comes pretty close, but it seems that the setting of \labelwidth is being ignored, and that there is some other thing I need to add to get the correct value of leftmargin.

The tabular version produces that output I want, but would prefer to use the description list from enumitem.

\documentclass{article}
\usepackage{enumitem}
\usepackage{calc}

\newcommand*{\LongDescription}{This is a  long sentence to check that when it gets wrapped it is indented properly on the next line, but doesn't seem to when used in the list.}%
\newcommand*{\ShortDescription}{A very short description.}%

\begin{document}
\newcommand*{\LargestItem}{The Largest Named Item}%
\newlength{\MaxWidth}%
\settowidth{\MaxWidth}{\LargestItem}%
\newlength{\MaxWidthPlusLabelSep}%
\setlength{\MaxWidthPlusLabelSep}{\labelsep+\MaxWidth}%

\begin{description} [leftmargin=\MaxWidthPlusLabelSep,labelwidth=\MaxWidth]
\item Small Name:
    \ShortDescription
\item The Largest Named Item: \LongDescription
\item Larger Name:
    \ShortDescription
\end{description}


\hrule\medskip

I want something like this, but should be able to do this with a list:

\bigskip
\begin{tabular}{l@{ }p{\linewidth-\MaxWidthPlusLabelSep}}
Small Name:
    &\ShortDescription\\\\
    The Largest Named Item:
    &\LongDescription\\\\
    Larger Name:
    &\ShortDescription
\end{tabular}
\end{document}

I have tried the solutions on how to Change hanging indent in description list and How to align long labels in a list to the left margin? but have not been able to adapt them to what I want.

David Carlisle
  • 757,742
Peter Grill
  • 223,288

2 Answers2

10

With version 3.3 it's:

\begin{description}[
  leftmargin=!,     % let enumitem do the dirty job
  labelwidth=\widthof{\bfseries The Largest Named Item}]

You have to put the format of the label (here, something similar to widest would be useful, so I've added it to my todo list)

Javier Bezos
  • 10,003
4
\newenvironment{mydesc}[1]
  {\settowidth{\dimen0}{#1}%
   \renewcommand{\descriptionlabel}[1]{##1\hfill}%
   \begin{description}[leftmargin=\dimexpr\dimen0+\labelsep\relax,labelwidth=\dimen0 ]}
  {\end{description}}

\begin{mydesc}{The Largest Named Item}
\item[Small Name]
    \ShortDescription
\item[The Largest Named Item] \LongDescription
\item [Larger Name]
    \ShortDescription
\end{mydesc}

If enumitem version 3 is not available, it's possible to use a solution provided in the LaTeX Companion:

\newenvironment{mydesc}[1]
  {\list{}{\renewcommand\makelabel[1]{##1\hfil}%
     \settowidth\labelwidth{\makelabel{#1}}%
     \setlength\leftmargin{\dimexpr\labelwidth+\labelsep\relax}}}
  {\endlist}
egreg
  • 1,121,712
  • This seems to work for the label width, but has two issues: The items seem to be shifted to the left into the margin, and the left margin seems to be way too large in that the long description is indented way too much. – Peter Grill Jul 20 '11 at 16:31
  • Doesn't happen to me. What version of enumitem are you using? – egreg Jul 20 '11 at 18:04
  • AFAIK, I am on TexLive 2010. Did a full update a few weeks ago so should be all up to date. The log file has Package: enumitem 2009/05/18 v2.2 – Peter Grill Jul 20 '11 at 18:42
  • Yes, with version 2.2 it happens as you are saying. :( See workaround. – egreg Jul 21 '11 at 14:15