9

I am trying to get enumerate environment do the following:

     (subgroup)   some description of a subgroup of a group G.
      (hello!)    This subgroup is a hello subgroup of G and satisfies an
                  awkward  condition that it does not occur often!
    (long enough) It also satisfies long enough condition so that it is not trivial!

Based on the answer of Peter Grill and the ensuing discussion here, I came up with the following code:

\documentclass{article}
\usepackage{enumitem}
\SetLabelAlign{Center}{\makebox[1em]{(#1)}}
\begin{document}
\begin{enumerate}[align=Center]
\item[subgroup] Some description of a subgroup of a group $G$.
\item[hello!] This subgroup is a hello subgroup of G and satisfies an awkward  condition that it does not occur often!
\item[long enough] It also satisfies long enough condition so that it is not trivial!
\end{enumerate}
\end{document}

This leaves me with:

mwe.png

I'd be glad if somebody can help me fix this code!

Edit Playing around a little bit, I figure that increasing the \makebox width(?), I could get it to write things without overlapping. But, this breaks alignment. Could someone please explain this?

2
  • Not sure this is what you're looking for, but increasing the width of your box to, say, 6em stops the overlapping.
    – Jonathan
    Commented Aug 1, 2013 at 19:44
  • @Jonathan Yes, I just figured that out and even edited to add it!
    – kan
    Commented Aug 1, 2013 at 19:45

2 Answers 2

8

I would use a description list here instead of misusing itemize. In order for this to work you need to specify the longest label you will have. You need to have enumitem calculate the left margin for you, using leftmargin=!.

\documentclass{article}
\usepackage{enumitem}
\usepackage{calc}
\newlength{\mylongest}
\setlength{\mylongest}{\widthof{The longest label I will need}}
\addtolength{\mylongest}{\labelsep}
\SetLabelAlign{CenterWithParen}{\makebox[\mylongest]{(#1)}}
\begin{document}
\begin{description}[style=unboxed,align=CenterWithParen,labelwidth=\mylongest,leftmargin=!]
\item[Foo]{This is a foo item}
\item[FooBar]{This is a foobar item}
\item[A longer description]{A longer label}
\end{description}
\end{document}

output of code

1
  • One can use environ and \NewEnviron in order to avoid guessing the widest label. There's no need to load calc, because \settowidth{\mylongest}{<text>} is defined in the kernel.
    – egreg
    Commented Aug 1, 2013 at 20:55
8

One can use environ and so avoid guessing at the widest label.

\documentclass{article}
\usepackage{enumitem}
\usepackage{environ}
\newlength{\cdescwidth}
\SetLabelAlign{CenterWithParen}{\makebox[\cdescwidth]{\normalfont(#1)}}

\NewEnviron{cdesc}{%
  \setbox0=\vbox{% measure the label widths
    \global\cdescwidth=0pt
    \def\item[##1]{%
      \settowidth{\dimen0}{(##1)}%
      \ifdim\dimen0>\cdescwidth
        \global\cdescwidth=\dimen0
      \fi}
    \BODY}
  % set the label width
  \global\advance\cdescwidth\labelsep
  \begin{description}[
    font=\normalfont,
    style=unboxed,
    align=CenterWithParen,
    labelwidth=\cdescwidth,
    leftmargin=!
  ]
  % typeset the contents
  \BODY
  \end{description}
}

\begin{document}
\begin{cdesc}
\item[Foo] This is a foo item
\item[FooBar] This is a foobar item
\item[A longer description] A longer label
\end{cdesc}
\end{document}

enter image description here

A different implementation with expl3 that avoids the tentative typesetting of the whole lot.

\documentclass{article}
\usepackage{enumitem}
\usepackage{xparse}

\ExplSyntaxOn

\SetLabelAlign{CenterWithParen}{\makebox[\l__kan_cdesc_dim]{\normalfont(#1)}}

\NewDocumentEnvironment{cdesc}{+b}
 {
  \__kan_cdesc_setup:n { #1 }
  \begin{description}[
    font=\normalfont,
    style=unboxed,
    align=CenterWithParen,
    labelwidth=\l__kan_cdesc_dim,
    leftmargin=!
  ]
  % typeset the contents
  #1
  \end{description}
 }{}

\dim_new:N \l__kan_cdesc_dim

\cs_new_protected:Nn \__kan_cdesc_setup:n
 {
  \seq_set_split:Nnn \l__kan_cdesc_seq { \item } { [] #1 }
  \dim_zero:N \l__kan_cdesc_dim
  \seq_map_function:NN \l__kan_cdesc_seq \__kan_cdesc_measure:n
  \dim_add:Nn \l__kan_cdesc_dim { \labelsep }
 }
\cs_new_protected:Nn \__kan_cdesc_measure:n
 {
  \__kan_cdesc_measure_aux:w #1 \q_stop
 }
\cs_new_protected:Npn \__kan_cdesc_measure_aux:w [#1] #2 \q_stop
 {
  \hbox_set:Nn \l_tmpa_box {\normalfont(#1)}
  \dim_set:Nn \l__kan_cdesc_dim
   {
    \dim_max:nn { \box_wd:N \l_tmpa_box } { \l__kan_cdesc_dim }
   }
 }

\ExplSyntaxOff


\begin{document}
\begin{cdesc}
\item[Foo] This is a foo item
\item[FooBar] This is a foobar item
\item[A longer description] A longer label
\end{cdesc}
\end{document}

You must log in to answer this question.

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