包含自定义环境中的BODY

包含自定义环境中的BODY

这是一个后续问题创建特定计数器值的数组在 NewEnviron 中使用自定义字段

我想使用根据 的值mtadd填充数组的函数,在创建列之间线的环境内。为此,我必须包括在外部定义的主体。但这会产生错误,显然与从第 10 行开始的 的重新定义有关,我并不完全理解(取自 Claudio Fiandrino 在mtarrlinesmyenv! Misplaced alignment tab character &.&如何创建垂直时间线?)。注释掉后\BODY会得到正确的行,但是函数mtadd不起作用,因此myarr保持为空。

以下是代码:

\documentclass{article}
\usepackage{pgfmath,pgffor}
\usepackage{environ}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\usepackage{charter}

\makeatletter
\let\matamp=&
\catcode`\&=13
\makeatletter
\def&{\iftikz@is@matrix
  \pgfmatrixnextcell
  \else
  \matamp
  \fi}
\makeatother

\newcounter{lines}
\def\endlr{\stepcounter{lines}\\}

\newcounter{envcount}
\setcounter{envcount}{0}

\newcommand*\myarr{}
\newcommand{\mtadd}{
\ifx\myarr\empty
\edef\myarr{\arabic{lines}}
\else\edef\myarr{\myarr,\arabic{lines}}
\fi}

\tikzset{description/.style={
  column 2/.append style={#1}
 },
 line color/.store in=\lcolor,
 line color=white!80!black,
 line color st/.style={fill=\lcolor,draw=\lcolor},
}

\NewEnviron{myenv}[1][]{%
\def\myarr{}
\setcounter{lines}{1}
\stepcounter{envcount}%
\BODY
\begin{tikzpicture}[column 1/.style={anchor=east},
 column 2/.style={anchor=west},
 #1
]
\matrix(myenv\theenvcount)[matrix of nodes]{\BODY};
\pgfmathtruncatemacro\endmtx{\thelines-1}
\path [line color st]
($(myenv\theenvcount-1-1.north east)!0.5!(myenv\theenvcount-1-2.north west)$)--
($(myenv\theenvcount-\endmtx-1.south east)!0.5!(myenv\theenvcount-\endmtx-2.south west)$);
\end{tikzpicture}
}

\def\names{{"Katie","Frank","Laura","Joe","Peter","Jens","Carolin","Anna","Steph","Stef"}}
\begin{document}
\begin{myenv}
test &  now\mtadd\endlr
test &  \foreach \i in \myarr {%
     Name \i: \pgfmathparse{\names[\i]}\pgfmathresult, }\endlr
\end{myenv}

\end{document}

相关内容