我正在尝试学习如何使用enumitem
键来调整description
列表。在这种情况下,我希望列表项和描述文本都左对齐,但描述文本应全部从同一水平位置开始。
下面的内容非常接近,但似乎的设置\labelwidth
被忽略了,而且我需要添加一些其他内容才能获得正确的值leftmargin
。
该tabular
版本产生了我想要的输出,但更喜欢使用description
来自的列表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}
我已经尝试过如何解决更改描述列表中的悬挂缩进 和如何将列表中的长标签与左边距对齐? 但无法使它们适应我的需要。
答案1
\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}
如果枚举项版本 3 不可用,可以使用 LaTeX Companion 中提供的解决方案:
\newenvironment{mydesc}[1]
{\list{}{\renewcommand\makelabel[1]{##1\hfil}%
\settowidth\labelwidth{\makelabel{#1}}%
\setlength\leftmargin{\dimexpr\labelwidth+\labelsep\relax}}}
{\endlist}
答案2
对于 3.3 版本来说:
\begin{description}[
leftmargin=!, % let enumitem do the dirty job
labelwidth=\widthof{\bfseries The Largest Named Item}]
您必须放置标签的格式(这里,类似的东西widest
会很有用,所以我已将其添加到我的待办事项列表中)