Center Aligning Labels in a enumerate environment
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}
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}
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}