I'm trying to parse an SGF string with the package listofitems. SGFs are trees in text form, but right now, I'm working on only single-branched SGFs, for simplicity.
Here's an example of what I'm trying to do:
\documentclass{article}
\usepackage{listofitems}
\usepackage{tikz}
\newcommand{\parseSgf}[1]{
% From this answer by @StevenB.Segletes.
\setsepchar{;}
\defpair{[]}
\readlist\Z{#1}
\begin{itemize}
\foreach \i in {\Z}{
% TODO: if key is either B or W:
\item Color: {\i}[0] and Coords: {\i}[1]
}
\end{itemize}
}
\begin{document}
\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])}
\parseSgf{\sgfA}
\parseSgf{\sgfB}
\end{document}
sgfA isn't proper SGF, just a simple example. I would like it to print an unordered list like this:
- Color:
Band Coords:ab - Color:
Wand Coords:cd
But I haven't been able to figure out how to use \setsepchar and \defpair to separate things properly, or how to use them in a \foreach.
sgfB is a proper SGF string. And there's the additional problem of ignoring keys that are not B or W. If anyone knows how to do it with listofitems, that would be a plus.


\foreachfor this. It is especially created for TikZ/PGF and does several things such as scoping and expanding that you will find annoying when used in another context. Why not stick to the functions provided by thelistofitemspackage? – Jasper Habicht Feb 07 '24 at 14:45sgf2dgwritten in Perl that seems to allow to translate SGF to diagrams also in TeX format. – Jasper Habicht Feb 07 '24 at 14:52sgf2dg. Does it generate TeX code or EPS pictures? I only knew about the ancientpsgoandigopackages, and that's why I'm trying to create a new package, because those are too old, and difficult to work with (also, I'm trying to learn more about TeX). In the past, I used to generate EPS diagrams through GoWrite2. – psygo Feb 07 '24 at 15:08sgf2dg. I just stumbled upon it when looking for more infomration about the SGF syntax. – Jasper Habicht Feb 07 '24 at 15:12;andkey[value]should be enough. – psygo Feb 07 '24 at 15:19;and then at[(and at]or just delete]). Are you bound to thelistofitemspackage? – Jasper Habicht Feb 07 '24 at 15:53listofitems. It just seemed very handy. – psygo Feb 07 '24 at 16:34\sgfB, what should the output look like? – Steven B. Segletes Feb 07 '24 at 17:18\sgfB, these are the moves:;B[as];W[bs];B[cs], the beginning is game metadata. So the output should be a this formatted as a list:Color: B and Coords: as,Color: W and Coords: bs, andColor: B and Coords: cs. – psygo Feb 07 '24 at 17:30listofitemspackage if you're able to. This way I can tag it here. – psygo Feb 07 '24 at 17:34;GM[1]...is not part of a color/coords specification? – Steven B. Segletes Feb 07 '24 at 17:47GM, notBorW. All the fields are of the shapekey[value]. Moves are represented by the keysB(Black) andW(White). There are plenty other "official" keys in the SGF specification (the grammar actually doesn't disallow any key actually), but those are the two most important ones. – psygo Feb 07 '24 at 17:52