Schleife durch Array von Zeichenfolgen (eigentlich ein Array von Arrays von Zeichenfolgen, zB `{{black, ab}, {white, cd}}`)

Schleife durch Array von Zeichenfolgen (eigentlich ein Array von Arrays von Zeichenfolgen, zB `{{black, ab}, {white, cd}}`)

Ich zeichne Steine ​​auf einem Go-Brett mitSGFKoordinaten (was ich nur mit der Hilfe der Leute aufdiese Frage) so was \drawStoneFromSgfCoords{ab}.

Es kann jedoch sein, dass pro Diagramm viele Steine ​​vorhanden sind, sodass es praktisch wäre, zusätzlich ein weiteres Makro wie dieses zu haben: \drawStonesFromSgfCoords{{black, ab}, {white, cd}}(die Farben wechseln sich normalerweise ab, manchmal ist es jedoch praktischer, eine Liste mit schwarzen Koordinaten und dann mit weißen zu haben).

Folgendes versuche ich gerade, obwohl es nicht funktioniert (ich meine, es funktioniert, nur nicht mit dem neuen Makro):

\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}

Was ist die richtige Art und Weise, auf Arrays zuzugreifen oder sie zu übergeben (innerhalb einer Schleife)?

(Ich verwende PGFs, \foreachweil ich mich damit auskenne.)

Antwort1

clistIch denke, es ist eine bessere Idee, die verschachtelten Listen (eine durch Kommas getrennte Liste) hier mit Expl-Tools zu verarbeiten, da es dort bereits Tools zum Umgang mit solchen Listen gibt:

\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}

Bildbeschreibung hier eingeben


Einige Hinweise zu den verwendeten Befehlen (eine gute Übersicht und eine ausführliche Dokumentation finden Sie imL3-Schnittstellen PDF):

  • \clist_set:Nn \l_tmpa_clist { #1 }speichert eine clist(eine durch Kommas getrennte Liste) in einer Token-Variable, um sie zugänglich zu machen. Im Beispielfall wird in der Token-Variable {{black, ab}, {white, cd}}als gespeichert .clist\l_tmpa_clist
  • \clist_map_inline:Nn \l_tmpa_clist { ... }durchläuft alle Elemente in clist(gespeichert in der Token-Variable) und führt den Code zwischen den geschweiften Klammern aus. Auf das aktuelle Element wird im Codebeispiel über #1( zugegriffen, da es eine Ebene tiefer verschachtelt ist).##1
  • \clist_pop:NN \l_tmpb_clist \l_tmpa_tlgreift auf das erste Element zu clistund speichert es in einer Token-Variable ( l_tmpa_tl). Anschließend entfernt es dieses Element aus clist.
  • \exp_args:Noo \drawStoneFromSgfCoords { \l_tmpa_tl } { \l_tmpb_tl }erweitert zuerst die beiden Token-Variablen und fügt das Ergebnis als Argumente ein \drawStoneFromSgfCoords.

Schauen wir uns also Schritt für Schritt an, was passiert, wenn wir sagen \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

    }
}

verwandte Informationen