「可嵌套」描述環境可以自動找到每個層級最寬的標籤寬度?

「可嵌套」描述環境可以自動找到每個層級最寬的標籤寬度?

我很高興找到一種方法來根據最寬的標籤自動調整描述清單的標籤寬度(來自貢薩洛·梅迪納對此問題的回答)。這是 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}

相關內容