我很高兴找到了一种方法,可以根据最宽的标签自动调整描述列表的标签宽度(从Gonzalo Medina 对此问题的回答)下面是 Gonzalo Medina 编写的代码:
\documentclass{article}
\usepackage{enumitem}
\usepackage{environ}
\newlength\widest
\makeatletter
\NewEnviron{ldescription}{%
\vbox{%
\global\setlength\widest{0pt}%
\def\item[##1]{%
\settowidth\@tempdima{\textbf{##1}}%
\ifdim\@tempdima>\widest\global\setlength\widest{\@tempdima}\fi%
}%
\setbox0=\hbox{\BODY}%
}
\begin{description}[
leftmargin=\dimexpr\widest+0.5em\relax,
labelindent=0pt,
labelwidth=\widest]
\BODY
\end{description}%
}
\makeatother
\begin{document}
\begin{ldescription}
\item[Short] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\item[A really really long label] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\end{ldescription}
\begin{ldescription}
\item[Short] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\item[A medium label] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\end{ldescription}
\end{document}
不过,我想知道是否有可能使此环境“可嵌套”。简单地尝试将其嵌套为“描述”或“枚举”不起作用:
\documentclass{article}
\usepackage{enumitem}
\usepackage{environ}
\newlength\widest
\makeatletter
\NewEnviron{ldescription}{%
\vbox{%
\global\setlength\widest{0pt}%
\def\item[##1]{%
\settowidth\@tempdima{\textbf{##1}}%
\ifdim\@tempdima>\widest\global\setlength\widest{\@tempdima}\fi%
}%
\setbox0=\hbox{\BODY}%
}
\begin{description}[
leftmargin=\dimexpr\widest+0.5em\relax,
labelindent=0pt,
labelwidth=\widest]
\BODY
\end{description}%
}
\makeatother
\begin{document}
\begin{ldescription}
\item[Short] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\item[A label with a nested list] I would like a nested list here.
\begin{ldescription}
\item[Short] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\item[A long label] text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\end{ldescription}
\end{ldescription}
\end{document}
特别:
(1)我希望每个列表/嵌套列表都有自己的标签宽度,等于该特定列表中最宽标签的宽度(不考虑其父列表中的任何标签或嵌套在其中的列表中的任何标签);并且
(2) 我希望能够更改每个列表/嵌套列表的标签的对齐方式(即能够在“align=left”和“align=right”之间选择)。
我不介意定义多个环境(只要我不必指定文档将具有多少个嵌套级别(即,只要该过程是自动化的));我的主要目标是自动完成此操作。
答案1
\documentclass{article}
\usepackage{enumitem}
\usepackage{environ}
\newlength\widest
\let\saveditem\item
\makeatletter
\NewEnviron{ldescription}{%
\dimen0=\widest
\let\item\saveditem
\vbox{%
\global\setlength\widest{0pt}%
\def\item[##1]{%
\settowidth\@tempdima{\textbf{##1}}%
\ifdim\@tempdima>\widest\global\setlength\widest{\@tempdima}\fi%
}%
\setbox0=\vbox{\BODY}%
}
\begin{description}[
leftmargin=\dimexpr\widest+0.5em\relax,
labelindent=0pt,
labelwidth=\widest]
\BODY
\end{description}%
\global\widest\dimen0
}
\makeatother
\begin{document}
\begin{ldescription}
\item[Short] text text text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text
\item[A label with a nested list] I would like a nested list here.
\begin{ldescription}
\item[Short] text text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text
\item[A long label] text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text text
\end{ldescription}
\end{ldescription}
\end{document}