各レベルで最も広いラベル幅を自動的に検出する「ネスト可能な」記述環境ですか?

各レベルで最も広いラベル幅を自動的に検出する「ネスト可能な」記述環境ですか?

説明リストのラベル幅を最も広いラベル(この質問に対するゴンサロ・メディナの答え)。以下は 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}

関連情報