
我正在圍棋(遊戲)棋盤上畫棋子肝細胞生長因子座標(我只能從人們給我的幫助中做到這個問題) 像這樣\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
}
}