
수행원@JasperHabicht의 답변입니다.\drawMoveFromSgfCoords
, 내 코드에서 해당 매크로에 옵션을 추가하여 플래그에 따라 또는 를 실행할 수 있도록 하고 싶습니다 \drawStoneFromSgfCoords
. 나는 아직 expl3에 대해 많이 알지 못하므로 이것은 일종의 의사 코드입니다.
% Parameters
%
% 1: array of {<black | white>, <coordinates}
%
% Example usage: `drawStonesOrMovesFromSgfCoords{{black, ab}, {white, cd}}{true}`
\ExplSyntaxOn
\NewExpandableDocumentCommand{\drawStonesOrMovesFromSgfCoords}{ m }{ 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
% I'm trying to fix what's in-between parentheses below:
\exp_args:Noo (\bool_if:NN ##2 \drawStoneFromSgfCoords \drawMoveFromSgfCoords) { \l_tmpa_tl } { \l_tmpb_tl }
}
}
\ExplSyntaxOff
다음은 다소 완전한 예입니다(구문을 알고 있다면 아마도 불필요할 것입니다).
\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];
}
\newcounter{moveCounter}
\setcounter{moveCounter}{1}
\newcommand{\drawMoveFromSgfCoords}[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 = \UseName{not#1}]
{\themoveCounter};
\stepcounter{moveCounter}
}
\ExplSyntaxOn
% Example usage: `drawMovesOrStonesFromSgfCoords{{black, ab}, {white, cd}}`
\NewExpandableDocumentCommand{\drawMovesOrStonesFromSgfCoords}{ 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}
\drawMovesOrStonesFromSgfCoords{{black, ab}, {white, cd}}
\end{tikzpicture}
\end{document}
답변1
넌 할 수있어:
\ExplSyntaxOn
% There is no point in doing \NewExpandableDocument as \clist_set:Nn and
% \clist_pop:NN and \clist_map_inline:Nn are not fully expandable.
\NewDocumentCommand{\drawMovesOrStonesFromSgfCoords}{ s m }{
% The s argument, denoted by #1, is "true" in case a star was provided.
% The s argument, denoted by #1, is "false" in case no star was provided.
\clist_set:Nn \l_tmpa_clist { #2 }
\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
\IfBooleanTF {#1} { \exp_args:NVV \drawStoneFromSgfCoords }
{ \exp_args:NVV \drawMoveFromSgfCoords }
\l_tmpa_tl \l_tmpb_tl
}
}
\ExplSyntaxOff
또는:
\ExplSyntaxOn
% There is no point in doing \NewExpandableDocument as \clist_set:Nn and
% \clist_pop:NN and \clist_map_inline:Nn are not fully expandable.
\NewDocumentCommand{\drawMovesOrStonesFromSgfCoords}{ s m }{
% The s argument, denoted by #1, is "true" in case a star was provided.
% The s argument, denoted by #1, is "false" in case no star was provided.
\clist_set:Nn \l_tmpa_clist { #2 }
\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:Nc
\exp_args:NVV
{draw\IfBooleanTF {#1}{Stone}{Move}FromSgfCoords}
\l_tmpa_tl \l_tmpb_tl
}
}
\ExplSyntaxOff
복잡한 것을 좋아한다면 다음의 -argument \exp_args:NnV \use:n {...} \Variable
내에 중첩할 수 있습니다 .n
\exp_args:NnV
\ExplSyntaxOn
% There is no point in doing \NewExpandableDocument as \clist_set:Nn and
% \clist_pop:NN and \clist_map_inline:Nn are not fully expandable.
\NewDocumentCommand{\drawMovesOrStonesFromSgfCoords}{ s m }{
% The s argument, denoted by #1, is "true" in case a star was provided.
% The s argument, denoted by #1, is "false" in case no star was provided.
\clist_set:Nn \l_tmpa_clist { #2 }
\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:NnV
\use:n
{
\exp_args:NnV
\use:n
{ \IfBooleanTF {#1} {\drawStoneFromSgfCoords} {\drawMoveFromSgfCoords} }
\l_tmpa_tl
}
\l_tmpb_tl
}
}
\ExplSyntaxOff
완전한 예는 다음과 같습니다.
\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];
}
\newcounter{moveCounter}
\setcounter{moveCounter}{1}
\newcommand{\drawMoveFromSgfCoords}[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 = \UseName{not#1}]
{\themoveCounter};
\stepcounter{moveCounter}
}
\ExplSyntaxOn
% There is no point in doing \NewExpandableDocument as \clist_set:Nn and
% \clist_pop:NN and \clist_map_inline:Nn are not fully expandable.
\NewDocumentCommand{\drawMovesOrStonesFromSgfCoords}{ s m }{
% The s argument, denoted by #1, is "true" in case a star was provided.
% The s argument, denoted by #1, is "false" in case no star was provided.
\clist_set:Nn \l_tmpa_clist { #2 }
\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
\IfBooleanTF {#1} { \exp_args:NVV \drawStoneFromSgfCoords }
{ \exp_args:NVV \drawMoveFromSgfCoords }
\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}
% The unstarred variant does \drawMoveFromSgfCoords:
\drawMovesOrStonesFromSgfCoords{{black, ab}, {white, cd}}
\end{tikzpicture}
\begin{tikzpicture}
\pgfmathsetmacro{\step}{10 / 18}
\draw[step=\step] (0, 0) grid (10, 10);
%\drawStoneFromSgfCoords{black}{ab}
%\drawStoneFromSgfCoords{white}{cd}
% The starred variant does \drawStoneFromSgfCoords:
\drawMovesOrStonesFromSgfCoords*{{black, ab}, {white, cd}}
\end{tikzpicture}
\end{document}
답변2
나 자신은 이런 식으로 작업을 수행하지 않겠지만 다음 코드는 원본을 비교적 보수적으로 변경하여 작동합니다.
\documentclass{standalone}
\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
\NewExpandableDocumentCommand{\stringToCoordX}{ mm }{
\int_from_alph:n { #1 }
}
\NewExpandableDocumentCommand{\stringToCoordY}{ mm }{
\int_from_alph:n { #2 }
}
\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 * 1.8cm},{\y * 1.8cm})
circle [radius = 0.2575cm];
}
\newcounter{moveCounter}
\setcounter{moveCounter}{1}
\newcommand{\drawMoveFromSgfCoords}[2]{
\pgfmathsetmacro{\x}{\stringToCoordX#2 - 1}
\pgfmathsetmacro{\y}{\stringToCoordY#2 - 1}
\draw[draw = \UseName{not#1}, fill = #1, line width = 0.1mm]
({\x * 1.8cm},{\y * 1.8cm})
circle [radius = 0.2575cm]
node[color = \UseName{not#1}]
{\themoveCounter};
\stepcounter{moveCounter}
}
\ExplSyntaxOn
% Example usage: `drawMovesOrStonesFromSgfCoords{{black, ab}, {white, cd}}`
\NewExpandableDocumentCommand{\drawMovesOrStonesFromSgfCoords}{ sm }{
\clist_set:Nn \l_tmpa_clist { #2 }
\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
\IfBooleanTF { #1 }
{
\ExpandArgs { VV } \drawStoneFromSgfCoords \l_tmpa_tl \l_tmpb_tl
}{
\ExpandArgs { VV } \drawMoveFromSgfCoords \l_tmpa_tl \l_tmpb_tl
}
}
}
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\step}{10 / 18}
\draw[step=\step] (0, 0) grid (10, 10);
\drawMovesOrStonesFromSgfCoords{{black,ab}, {white, cd}}
\end{tikzpicture}
\begin{tikzpicture}
\pgfmathsetmacro{\step}{10 / 18}
\draw[step=\step] (0, 0) grid (10, 10);
\drawMovesOrStonesFromSgfCoords*{{black, ab}, {white, cd}}
\end{tikzpicture}
\end{document}