![“可嵌套”描述环境会自动查找每一级最宽的标签宽度?](https://linux22.com/image/348132/%E2%80%9C%E5%8F%AF%E5%B5%8C%E5%A5%97%E2%80%9D%E6%8F%8F%E8%BF%B0%E7%8E%AF%E5%A2%83%E4%BC%9A%E8%87%AA%E5%8A%A8%E6%9F%A5%E6%89%BE%E6%AF%8F%E4%B8%80%E7%BA%A7%E6%9C%80%E5%AE%BD%E7%9A%84%E6%A0%87%E7%AD%BE%E5%AE%BD%E5%BA%A6%EF%BC%9F.png)
我很高兴找到了一种方法,可以根据最宽的标签自动调整描述列表的标签宽度(从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}