在枚举环境中居中对齐标签

在枚举环境中居中对齐标签

我正在尝试让枚举环境执行以下操作:

     (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!

根据 Peter Grill 的回答以及随后的讨论这里,我想出了以下代码:

\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}

这让我得到:

韋斯特

如果有人能帮助我修复这个代码我会很高兴!

编辑玩了一会儿,我发现增加\makebox宽度(?)可以让它不重叠地书写内容。但是,这会破坏对齐。有人能解释一下吗?

答案1

我会在这里使用描述列表,而不是滥用itemize。为了使其工作,您需要指定最长的标签。您需要使用enumitem计算左边距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}

代码输出

答案2

人们可以使用它environ,从而避免猜测最宽的标签。

\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}

在此处输入图片描述

采用不同的实现方式expl3可以避免对所有内容进行试探性排版。

\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}

相关内容