7

I have seen description-list-with-right-alignment-of-labels that explains, how to align description labels on the right. However the presented solution sets the lengths globally whereas I look for a local solution, meaning to specify the width for each environment separately. Is there a way telling each description environment what the broadest item is:

\documentclass{article}
\usepackage{enumitem}
\SetLabelAlign{parright}{\parbox[t]{\labelwidth}{\raggedleft#1}}

\setlist[description]{style=multiline,topsep=10pt,leftmargin=5cm,font=\textbf,align=parright}

\begin{document}

\begin{description}
\item[aaa] some text
\item[bbbbbb] some more text
\item[ccccccccc] and some text
\end{description}


\begin{description}
\item[aaaaaaa] some text
\item[bbbbbbbbbbbbbbb] some more text
\item[ccccccccccccccccccccccc] and some text
\end{description}

\end{document}
3
  • 1
    I don't have time to explain the exact details, so here's a short note. You can define an environment that uses the list environment to typeset the description list. Let the environment take an option that takes the item with the maximum width. Use it to set the \labelwidthat the start of the environment (using the style for the labels). You also have to redefine \makeitem, which should use the label, and set it in the right style, right justified in an \hbox to \labelwidth (or equivalent). It's not too much work, but keep track of how the list environment positions the labels.
    – user10274
    Mar 2, 2012 at 8:39
  • 2
    You are aware that enumitem allows you to set parameters both globally (as in your MWE) and locally (as in \begin{description}[style=...])?
    – DevSolar
    Mar 2, 2012 at 8:40
  • Apologies: \makeitem should be \makelabel.
    – user10274
    Mar 2, 2012 at 9:01

1 Answer 1

4

You can use the \widthof{} from the calc package to compute the correct width for the labelwidth on a per description list to obtain:

enter image description here

The parameter passed to the \widthof{} should be the widest element in the description list and also note that since the label is set in bold font, we need to ensure that the measurements are done with \bfseries applied:

\documentclass{article}
\usepackage{calc}
\usepackage{enumitem}
\SetLabelAlign{parright}{\parbox[t]{\labelwidth}{\raggedleft#1}}

\begin{document}
\begin{description}[labelwidth=\widthof{\bfseries ccccccccc},align=parright]
    \item[aaa]       some text
    \item[bbbbbb]    some more text
    \item[ccccccccc] and some text
\end{description}

\begin{description}[labelwidth=\widthof{\bfseries ccccccccccccccccccccccc},align=parright]
    \item[aaaaaaa]                 some text
    \item[bbbbbbbbbbbbbbb]         some more text
    \item[ccccccccccccccccccccccc] and some text
\end{description}
\end{document}
4
  • 1
    Furthermore, if the label is always set to the widest one, then boxing it in a parbox is unnecesary - just use align=right. (And I added to the todo list a widest for description.) Mar 3, 2012 at 9:20
  • \begin{description}[labelwidth=4em,align=parright] gives this error for me: "Package enumitem: Unknown value 'parright' for key 'align'". I don't know if this was allowable in 2012 when the answer was written; however, it is not valid now. See the documentation at texnia.com/archive/enumitem.pdf which states: "Three values are possible: left, the default right and parleft"
    – David J.
    Sep 28, 2019 at 23:55
  • @DavidJ.: Did you include the line \SetLabelAlign{parright}{\parbox[t]{\labelwidth}{\raggedleft#1}}? Sep 29, 2019 at 2:00
  • @PeterGrill whoops, I overlooked that. Thank you.
    – David J.
    Sep 30, 2019 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .