伪代码

伪代码

我愿意接受有关我的问题措辞的建议。

我希望所有描述列表都支持一个表示节点名称前缀的可选参数。这将传递给项目的定义,最终用于确保全局节点中节点名称的唯一性(我所说的全局节点是指使用 的参数的节点tikzremember picture

我想要自动执行以下代码。

伪代码

\documentclass{article}
\usepackage{fontspec}
\usepackage{xparse}
\usepackage{tikz}
\newcounter{itemcounter}
\NewDocumentCommand{\itemcount}{}{\addtocounter{itemcounter}{1}\arabic{itemcounter}}
\NewDocumentCommand{\tmark}{ m }{\tikz [remember picture,overlay,baseline={([yshift=-.8ex]current bounding box.center)}] \node [draw] (#1) {};} % used in definition of \item

%Some fancy schmancy code to transform the following item:
%\item [item] Some text.
%into this: 
%\item [\tmark{\nodeprefix\itemcount}item] Some text. %expands to [\tikz [remember picture, overlay] \node [draw] (lista1) {};]

\begin{document}
\begin{description}[nodeprefix=lista] % Not sure how to make a key or pass value of it to macro \nodeprefix
  \item [Apple] This is a fruit.
  \item [Broccoli] This is a vegetable.
\end{description}
\begin{description}[nodeprefix=listb] % Not sure how to make a key or pass value of it to macro \nodeprefix
  \item [Silver] This is a solid. 
  \item [Water] This is a liquid.
\end{description}
\begin{description}[nodeprefix=listc] % Not sure how to make a key or pass value of it to macro \nodeprefix
  \item [Happiness] This is a state of being.
  \item [Sadness] This is an unfortunate state of being. 
\end{description}
\end{document}

该代码产生了一些我以后可以引用的节点tikzpicture

\node (lista1) {};
\node (lista2) {};
\node (listb1) {};
\node (listb2) {};
\node (listc1) {};
\node (listc2) {};

重新定义description

重新定义将包括节点前缀可选参数,并将项目计数重置为 1(\setcounter{itemcount}{1}),以便新列表可以使用新数字。

\makeatletter % Redefine description list https://tex.stackexchange.com/a/66386/13552
\renewenvironment{description}
  {
  \list{}{\labelwidth\z@ \itemindent-\leftmargin
   \let\makelabel\descriptionlabel}
  }
  {
  \endlist
  }
  \let\descriptionlabel\relax
\newcommand*\descriptionlabel[1]{\hspace\labelsep\normalfont\bfseries #1}
\makeatother

重新定义item

重新定义将包括\tmark宏。

与计数器相关的问题:TikZ:节点自动编号,\value{counterName}

\expandafter\def\expandafter\description\expandafter{% https://tex.stackexchange.com/a/230316/13552
  \description \let\olditem\item
  \def\item[##1]{\olditem[##1]\mbox{}\\}}

答案1

如果我理解正确的话,你想要这样的东西吗?

\documentclass{article}
\usepackage{fontspec}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{enumitem}
\usepackage{etoolbox}

\newcounter{itemcounter}
\NewDocumentCommand{\tmark}{ m }{\tikz [remember picture,overlay] \node [draw] (#1) {};}

\pretocmd{\item}{\stepcounter{itemcounter}}{}{}
\BeforeBeginEnvironment{description}{\setcounter{itemcounter}{0}}

\newcommand\nodeprefix[1]{%
  \renewcommand\makelabel[1]{\tmark{#1\theitemcounter}\bfseries##1}}

\begin{document}

\begin{description}[before={\nodeprefix{lista}}]
  \item [Apple] This is a fruit.
  \item [Broccoli] This is a vegetable.
\end{description}
\begin{description}[before={\nodeprefix{listb}}]
  \item [Silver] This is a solid. 
  \item [Water] This is a liquid.
\end{description}
\begin{description}[before={\nodeprefix{listc}}]
  \item [Happiness] This is a state of being.
  \item [Sadness] This is an unfortunate state of being. 
\end{description}

\begin{tikzpicture}[remember picture,overlay]
\draw[red!70!black,line width=1pt]
  (lista1) -- ++(-30pt,0pt) |- (lista2)
  (lista1) -- ++(-30pt,0pt) |- (listb1)
  (lista1) -- ++(-30pt,0pt) |- (listb2)
  (lista1) -- ++(-30pt,0pt) |- (listc1)
  (lista1) -- ++(-30pt,0pt) |- (listc2);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容