在NewEnviron中使用自訂字段

在NewEnviron中使用自訂字段

我想在自訂環境中使用自訂函數。我添加了一個範例,顯示該函數在環境外部完美運行,但在環境內部myenv,似乎myarr保持為空。我實在不明白為什麼。

\documentclass{article}
\usepackage{pgfmath,pgffor}
\usepackage{environ}

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

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

\NewEnviron{myenv}[1][]{%
\myarr\empty%
\setcounter{lines}{1}%
}

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

\end{document}

相關內容