如何在 foreach 循環中定義宏,使其在迭代之間和循環之後產生影響,而不使用全局?

如何在 foreach 循環中定義宏,使其在迭代之間和循環之後產生影響,而不使用全局?

不使用\global,以下程式碼:

\documentclass{minimal}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{etextools}

\newcommand\appendbyforeach[2]{%
  \AfterGroup*{\noexpand\AfterGroup{\noexpand\edef\noexpand#1{\noexpand#1#2}}}
}

\newcommand\defincbyforeach[2]{%
  \AfterGroup*{\noexpand\def\noexpand#1{#2}}
  \AfterGroup*{\noexpand\AfterGroup{\noexpand\def\noexpand#1{#2}}}
}

\newcommand\meaningwithname[1]{\textbackslash#1: \expandafter\meaning\csname #1\endcsname}

\newcommand\mymacro{
  \begingroup
  \def\myarray{init}
  \def\valmax{0}
  \foreach \i in {1,8,-3}{
    \appendbyforeach{\myarray}{,\i}
    \pgfmathsetmacro{\max}{\valmax < \i ? \i : \valmax}
    \defincbyforeach\valmax{\max}
    %%% print state of \myarray and \valmax
    foreach \i\\\meaningwithname{myarray}\\\meaningwithname{valmax}\par
  }
  inside\\\meaningwithname{myarray}\\\meaningwithname{valmax}\par
  \endgroup
  outside\\\meaningwithname{myarray}\\\meaningwithname{valmax}\par
}

\begin{document}\mymacro\end{document}

產生一個包含以下內容的文件:

    對於每個 1
\myarray: 巨集:->init
\valmax:宏:->0
    對於每個 8
\myarray: 巨集:->init
\valmax:宏:->1.0
    foreach -3
\myarray: 巨集:->init
\valmax:宏:->8.0
    裡面
\myarray: 巨集:->init,1,8,-3
\valmax:宏:->8.0
    外部
\myarray: \放鬆
\valmax: \放鬆

這個結果很適合我!

但是有沒有更好的方法來定義巨集\appendbyforeach\defincbyforeach同時達到相同的效果呢?

etextools這裡的套餐真的實用嗎?

答案1

筆記:此程式碼始終使用etextools.在接受我對自己問題的回答之前,我希望其他人能為我提供更有趣的解決方案(沒有etextools,更有效,更快,更簡單...)。但始終不用\global

編輯:這是一個沒有擴充值並且只有兩個巨集的新版本。

  1. \afteriterationdef在當前迭代之後(重新)定義巨集。

  2. \afterforeachdef在 foreach 之後(以及迭代之後)(重新)定義巨集。

這是這兩個巨集的程式碼:

\documentclass{minimal}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{etextools}

\makeatletter

% define a macro after current iteration
\newcommand\afteriterationdef[1]{\aftergroup@def#1}

% define a macro after foreach (and after iteration)
\newcommand\afterforeachdef[1]{\afteriterationdef{#1}\AfterGroup{\aftergroup@def#1}}

\makeatother

舉個例子:

\newcommand\meaningwithname[1]{%
  \textbackslash#1: \expandafter\meaning\csname #1\endcsname}

\newcommand\mymacro{
  {
    \def\myarray{init}
    \def\myvalue{1}
    \foreach \i in {1,8,-3,-1}{
      % append some value to \myarray
      \edef\myarray{\myarray,\i}
      \afterforeachdef\myarray
      % incremental definition of \mtvalue
      \pgfmathsetmacro{\myvalue}{\myvalue*\i}
      \afterforeachdef\myvalue
      %%% print meaning of \myarray and \myvalue at each iteration
      foreach \i: \meaningwithname{myarray}, \meaningwithname{myvalue}\par
    }
    %%% print meaning of \myarray and \myvalue after \foreach
    inside group: \meaningwithname{myarray}, \meaningwithname{myvalue}\par
  }
  %%% print meaning of \myarray and \myvalue outside
  outside group: \meaningwithname{myarray}, \meaningwithname{myvalue}\par
}

\begin{document}\mymacro\end{document}

此範例產生以下文字:

foreach 1: \myarray: 巨集:->init,1, \myvalue: 巨集:->1.0
foreach 8: \myarray: 巨集:->init,1,8, \myvalue: 巨集:->8.0
foreach -3: \myarray: 巨集:->init,1,8,-3, \myvalue: 巨集:->-24.0
foreach -1: \myarray: 巨集:->init,1,8,-3,-1, \myvalue: 巨集:->24.0
群組內: \myarray: 巨集:->init,1,8,-3,-1, \myvalue: 巨集:->24.0
外部群組:\myarray:\relax,\myvalue:\relax

編輯2:另一個有兩個嵌套的例子\foreach

\newcommand\mymacrowithnestedforeach{
  {
    \def\myarray{init}
    \def\myvalue{1}
    \foreach \i in {1,...,3}{
      \foreach \j in {1,...,3}{
        % append some value to \myarray
        \edef\myarray{\myarray,(\i,\j)}
        \afterforeachdef\myarray
        % incremental definition of \myvalue
        \pgfmathsetmacro{\myvalue}{\myvalue+\j*\i}
        \afterforeachdef\myvalue
        %%% print meaning of \myarray and \myvalue at each iteration
        int foreach \i: \meaningwithname{myarray}, \meaningwithname{myvalue}\par
      }
      % 
      \afterforeachdef\myarray
      \afterforeachdef\myvalue
      %%% print meaning of \myarray and \myvalue at each iteration
      ext foreach \i: \meaningwithname{myarray}, \meaningwithname{myvalue}\par
    }
    %%% print meaning of \myarray and \myvalue after \foreach
    inside  group: \meaningwithname{myarray}, \meaningwithname{myvalue}\par
  }
  %%% print meaning of \myarray and \myvalue outside
  outside group: \meaningwithname{myarray}, \meaningwithname{myvalue}\par
}

\begin{document}\mymacrowithnestedforeach\end{document}

此範例產生以下文字:

int foreach 1: \myarray: 巨集:->init,(1,1), \myvalue: 巨集:->2.0
int foreach 1: \myarray: 巨集:->init,(1,1),(1,2), \myvalue: 巨集:->4.0
int foreach 1: \myarray: 巨集:->init,(1,1),(1,2),(1,3), \myvalue: 巨集:->7.0
ext foreach 1: \myarray: 巨集:->init,(1,1),(1,2),(1,3), \myvalue: 巨集:->7.0
int foreach 2: \myarray: 巨集:->init,(1,1),(1,2),(1,3),(2,1), \myvalue: 巨集:->9.0
int foreach 2: \myarray: 巨集:->init,(1,1),(1,2),(1,3),(2,1),(2,2), \myvalue: 巨集:-> 13.0
int foreach 2: \myarray: 宏:->init,(1,1),(1,2),(1,3),(2,1),(2,2),(2,3), \我的值:宏:->19.0
ext foreach 2: \myarray: 宏:->init,(1,1),(1,2),(1,3),(2,1),(2,2),(2,3), \我的值:宏:->19.0
int foreach 3: \myarray: 宏:->init,(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),( 3,1), \myvalue: 巨集:->22.0
int foreach 3: \myarray: 宏:->init,(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),( 3,1),(3,2), \myvalue: 巨集:->28.0
int foreach 3: \myarray: 宏:->init,(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),( 3,1),(3,2),(3,3), \myvalue: 宏:->37.0
ext foreach 3: \myarray: 宏:->init,(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),( 3,1),(3,2),(3,3), \myvalue: 宏:->37.0
組內: \myarray: 宏:->init,(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3 ,1),(3,2),(3,3), \myvalue: 宏:->37.0
外部群組:\myarray:\relax,\myvalue:\relax

相關內容