循环遍历字符串数组(实际上是字符串数组的数组,例如 `{{black,ab},{white,cd}}`)

循环遍历字符串数组(实际上是字符串数组的数组,例如 `{{black,ab},{white,cd}}`)

我在围棋棋盘上画棋子新加坡金融管理局坐标(我只能通过人们在这个问题) 像这样\drawStoneFromSgfCoords{ab}

但是,每个图表可能有许多石头,因此,再有一个像这样的宏也是很实用的:(\drawStonesFromSgfCoords{{black, ab}, {white, cd}}颜色通常会交替出现,但有时有一个黑色坐标列表,然后是白色坐标列表更为实用)。

这是我现在正在尝试的,尽管它不起作用(我的意思是,它确实有效,只是不使用新的宏):

\documentclass{article}

\usepackage{tikz}

% From [this answer by @DavidCarlisle](https://tex.stackexchange.com/a/708876/64441).
\newcommand\notwhite{black}
\newcommand\notblack{white}

% From [this answer by @DavidCarlisle](https://tex.stackexchange.com/a/708893/64441).
\ExplSyntaxOn
  \cs_generate_variant:Nn \int_from_alph:n {e}

  \newcommand\stringToCoordX[1]{
    \int_from_alph:e{\use_i:nn#1}
  }
  \newcommand\stringToCoordY[1]{
    ~\int_from_alph:e{\use_ii:nn#1}
  }
\ExplSyntaxOff

\newcommand{\drawStoneFromSgfCoords}[2]{
  \pgfmathsetmacro{\x}{\stringToCoordX{#2} - 1}
  \pgfmathsetmacro{\y}{\stringToCoordY{#2} - 1}

  \draw[draw = \UseName{not#1}, fill = #1, line width = 0.1mm]
    (\x * 10cm / 18, \y * 10cm / 18)
    circle [radius = 0.2575cm];
    node[color = white] {1};
}

% Example usage: `drawStonesFromSgfCoords{{black, ab}, {white, cd}}`
\newcommand{\drawStonesFromSgfCoords}[1]{
  \foreach \coords in {#1}{
    \drawStoneFromSgfCoords{{\coords}[0]}{{\coords}[1]}
  }
}

\begin{document}
  \begin{tikzpicture}
    \pgfmathsetmacro{\step}{10 / 18}

    \draw[step=\step] (0, 0) grid (10, 10);

    \drawStoneFromSgfCoords{black}{ab}
    \drawStoneFromSgfCoords{white}{cd}
    
    % \drawStonesFromSgfCoords{{black, ab}, {white, cd}}
  \end{tikzpicture}
\end{document}

访问或传递数组(在循环内)的正确方法是什么?

(我使用 PGF 是\foreach因为这是我所了解的。)

答案1

我认为,通过这里的 expl 工具处理嵌套clist(逗号分隔的列表)是一个更好的主意,因为那里已经存在处理此类列表的工具:

\documentclass{article}
\usepackage{tikz}

% From [this answer by @DavidCarlisle](https://tex.stackexchange.com/a/708876/64441).
\newcommand\notwhite{black}
\newcommand\notblack{white}

% From [this answer by @DavidCarlisle](https://tex.stackexchange.com/a/708893/64441).
\ExplSyntaxOn
  \cs_generate_variant:Nn \int_from_alph:n {e}

  \NewExpandableDocumentCommand{\stringToCoordX}{ m }{
    \int_from_alph:e { \use_i:nn #1 }
  }
  \NewExpandableDocumentCommand{\stringToCoordY}{ m }{
    \int_from_alph:e { \use_ii:nn #1 }
  }
\ExplSyntaxOff

\newcommand{\drawStoneFromSgfCoords}[2]{
  \pgfmathsetmacro{\x}{\stringToCoordX{#2} - 1}
  \pgfmathsetmacro{\y}{\stringToCoordY{#2} - 1}

  \draw[draw = \UseName{not#1}, fill = #1, line width = 0.1mm]
    (\x * 10cm / 18, \y * 10cm / 18)
    circle [radius = 0.2575cm];
}

\ExplSyntaxOn
  % Example usage: `drawStonesFromSgfCoords{{black, ab}, {white, cd}}`
  \NewExpandableDocumentCommand{\drawStonesFromSgfCoords}{ m }{
    \clist_set:Nn \l_tmpa_clist { #1 }
    \clist_map_inline:Nn \l_tmpa_clist {
      \clist_set:Nn \l_tmpb_clist { ##1 }
      \clist_pop:NN \l_tmpb_clist \l_tmpa_tl 
      \clist_pop:NN \l_tmpb_clist \l_tmpb_tl 
      \exp_args:Noo \drawStoneFromSgfCoords { \l_tmpa_tl } { \l_tmpb_tl }
      
    }
  }
\ExplSyntaxOff

\begin{document}
  \begin{tikzpicture}
    \pgfmathsetmacro{\step}{10 / 18}

    \draw[step=\step] (0, 0) grid (10, 10);

    %\drawStoneFromSgfCoords{black}{ab}
    %\drawStoneFromSgfCoords{white}{cd}
    \drawStonesFromSgfCoords{{black, ab}, {white, cd}}
    
  \end{tikzpicture}
\end{document}

在此处输入图片描述


关于所用命令的一些注释(可以在L3 接口 PDF):

  • \clist_set:Nn \l_tmpa_clist { #1 }clist(逗号分隔列表)存储在令牌变量中,以使其可访问。在示例中,{{black, ab}, {white, cd}}存储clist在令牌变量中\l_tmpa_clist
  • \clist_map_inline:Nn \l_tmpa_clist { ... }循环遍历clist(存储在 token 变量中)中的每个项目并执行花括号之间的代码。在代码示例中,当前项目通过#1(访问##1,因为它嵌套更深一层)。
  • \clist_pop:NN \l_tmpb_clist \l_tmpa_tl抓取 的第一项clist并将其存储在标记变量 ( l_tmpa_tl) 中。然后从 中删除此项clist
  • \exp_args:Noo \drawStoneFromSgfCoords { \l_tmpa_tl } { \l_tmpb_tl }首先扩展两个标记变量并将结果作为参数插入\drawStoneFromSgfCoords

那么,让我们一步步看看如果我们说\drawStonesFromSgfCoords{{black, ab}, {white, cd}}

\NewExpandableDocumentCommand{\drawStonesFromSgfCoords}{ m }{

    % store {{black, ab}, {white, cd}} in \l_tmpa_clist 
    \clist_set:Nn \l_tmpa_clist { #1 }

    % loop over items in \l_tmpa_clist 
    \clist_map_inline:Nn \l_tmpa_clist {

      % store {black, ab} in \l_tmpb_clist
      \clist_set:Nn \l_tmpb_clist { ##1 }

      % store black in \l_tmpa_tl and remove item from clist
      \clist_pop:NN \l_tmpb_clist \l_tmpa_tl 

      % store ab in \l_tmpb_tl and remove item from clist
      \clist_pop:NN \l_tmpb_clist \l_tmpb_tl 

      % expand \l_tmpa_tl and \l_tmpb_tl so that we get
      % \drawStoneFromSgfCoords{black}{ab}
      \exp_args:Noo \drawStoneFromSgfCoords { \l_tmpa_tl } { \l_tmpb_tl }

      % repeat this loop for the next item in the clist in \l_tmpa_clist

    }
}

相关内容