
下列的@StevenB.Segletes 的回答,他也是該包的創建者之一listofitems
,我現在嘗試使用該套件能夠從單分支解析的內容肝細胞生長因子宏內的字串。
這是我希望能夠實現的目標,但我認為我遇到了擴展問題,因為我收到了錯誤Missing \endcsname inserted
:
\long\def\Firstof#1#2\endFirstof{#1}
\ignoreemptyitems
\newcommand{\parseSgf}[1]{
\setsepchar{;/[||]}
\readlist*\Z{#1}
% This loop basically splits on `;`, which is a node/move delimiter in SGF
\foreachitem \i \in \Z[]{
\itemtomacro\Z[\icnt, 1]\stoneColor
\itemtomacro\Z[\icnt, 2]\sgfCoords
% These if's test the `key[value]` SGF format for the keys `B` and `W`, which denote Black and White moves, respecitvely
% Other keys typically refer to metadata, or other secondary info.
\if\stoneColor B
\drawStoneFromSgfCoords{black}{\sgfCoords} % if I hardcode the arguments, it works.
% \drawStoneFromSgfCoords{black}{Z[\icnt, 2]} % I've also tried stuff like this directly...
\fi
\if\stoneColor AB
\drawStoneFromSgfCoords{black}{\sgfCoords}
\fi
\if\stoneColor W
\drawStoneFromSgfCoords{white}{\sgfCoords}
\fi
\if\stoneColor AW
\drawStoneFromSgfCoords{white}{\sgfCoords}
\fi
}
}
理想情況下,如果有人能夠找到解決方案,我希望它能夠找到黑棋或白棋(B
和W
)以及編輯(添加)的棋子(AB
和AW
),因此 if 看起來像這樣:
\if\stoneColor { B \OR AB }
...
\if\stoneColor { W \OR AW }
但是,當然,如果問題太多的話,我可以把這個問題留到另一個問題。
這是一個完整的範例:
\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];
}
\usepackage{listofitems}
% From [this answer by @StevenB.Segletes](https://tex.stackexchange.com/a/709014/64441).
\long\def\Firstof#1#2\endFirstof{#1}
\ignoreemptyitems
\newcommand{\parseSgf}[1]{
\setsepchar{;/[||]}
\readlist*\Z{#1}
\foreachitem \i \in \Z[]{
\itemtomacro\Z[\icnt, 1]\color
\itemtomacro\Z[\icnt, 2]\sgfCoords
\expandafter\if\expandafter\Firstof\stoneColor\endFirstof B
\drawStoneFromSgfCoords{black}{ab}
\else\expandafter\if\expandafter\Firstof\stoneColor\endFirstof W
\drawStoneFromSgfCoords{white}{cd}
\fi\fi
}
}
\def\sgfA{;B[ab];W[cd]} % not truly valid SGF, just one simple example
\def\sgfB{(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2024-02-05];B[as];W[bs];B[cs])}
\def\sgfC{(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2024-02-05];B[as];W[bs];B[cs];PL[W]AB[dq]AW[eq])}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\step}{10 / 18}
\draw[step=\step] (0, 0) grid (10, 10);
% \drawStoneFromSgfCoords{black}{ab}
% \drawStoneFromSgfCoords{white}{cd}
\parseSgf{\sgfA}
\end{tikzpicture}
\end{document}
答案1
幾個問題:
不要使用
\color
,因為它會覆蓋現有的巨集...我將其更改為\Color
.的值
\Color
將是B
或W
,它不是實際的顏色。因此,我引入\newcommand\thecolorofB{black}
並\newcommand\thecolorofW{white}
轉換\Color
為實際顏色。SGF 定義中的括號也會造成問題。它們沒有被解析,而是解釋為 SGF 命令(但缺少關聯的
[]
資料)。因此,我硬連線解析以消化它們。這種方法可以解決眼前的問題,但可能不適合,這取決於您將來想要解決這個問題的地方。\Color
在將和的值\sgfCoords
提供給巨集之前,需要對其進行擴展\drawStoneFromSgfCoords
。我透過將它們擴展為\tmp
然後擴展\tmp
為巨集呼叫來實現這一點。
氣象局:
\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];
}
\usepackage{listofitems}
% From [this answer by @StevenB.Segletes](https://tex.stackexchange.com/a/709014/64441).
\long\def\Firstof#1#2\endFirstof{#1}
\newcommand\thecolorofB{black}
\newcommand\thecolorofW{white}
\ignoreemptyitems
\newcommand{\parseSgf}[1]{%
\setsepchar{;||(||)/[||]}%
\readlist*\Z{#1}%
%
\foreachitem \i \in \Z[]{%
\itemtomacro\Z[\icnt, 1]\Color
\itemtomacro\Z[\icnt, 2]\sgfCoords
\edef\tmp{{\csname thecolorof\Color\endcsname}{\sgfCoords}}%
%
\expandafter\if\expandafter\Firstof\Color\endFirstof B
\expandafter\drawStoneFromSgfCoords\tmp
\else\expandafter\if\expandafter\Firstof\Color\endFirstof W
\expandafter\drawStoneFromSgfCoords\tmp
\fi\fi
}%
}
\def\sgfA{;B[ab];W[cd]}
\def\sgfB{(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2024-02-05];B[as];W[bs];B[cs])}
\def\sgfC{(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.52.2]KM[6.5]SZ[19]DT[2024-02-05];B[as];W[bs];B[cs];PL[W]AB[dq]AW[eq])}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\step}{10 / 18}
\draw[step=\step] (0, 0) grid (10, 10);
\parseSgf{\sgfC}
\end{tikzpicture}
\end{document}