#1
我需要创建一些动态名称宏,并且我想在一个循环中从逗号分隔的参数列表中创建它们。foreach
这不是解决方案,因为它强制使用 gdef 而我只想要 edef。
还有什么替代解决方案?
\makeatletter
\newcommand{\NewPoints}[1]{%
\foreach \i in {#1} {%
\expandafter\xdef\csname \i\endcsname{%
a-\NumNode-\theLittNode}
\stepcounter{LittNode}
}
}
\makeatother
xdef
由于全局定义,因此并不适合。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{quotes}
\makeatletter
\newcommand{\NewPoints}[1]{%
\expandafter\edef\csname #1\endcsname{%
a-\NumNode-\theLittNode}
\stepcounter{LittNode}
}
\makeatother
% counter for naming nodes
\newcounter{LittNode}
% level of calling
\pgfmathtruncatemacro{\NumNode}{0}
% node named #1 at the current level
\def\N#1{a-\NumNode-#1}
% Pass the created nodes back to the calling macro
\newcommand{\KeepUsefullNodes}{%
\edef\Sortie{\csname OutPut-\NumNode \endcsname}
\foreach \Nd [count=\i from 1]
in \Sortie {%
\coordinate (\Nd) at (\N{\i}) ;
% debbuging
%\node at (2*\NumNode,\i/2) {\Nd -- \N{\i}} ;
}
\pgfmathtruncatemacro{\NumNode}{\NumNode-1}
}
% one level up at the begining of a macro
\newcommand{\AtBeginTikzMacro}[1]{%
\pgfmathtruncatemacro{\NumNodep}{\NumNode}
\pgfmathtruncatemacro{\NumNode}{\NumNode+1}
\expandafter\xdef\csname OutPut-\NumNode\endcsname{#1}
\setcounter{LittNode}{1}
}
%-----------------------------------------------------------
\def\tr[#1](#2,#3,#4){\draw[#1] (#2) -- (#3) -- (#4) --cycle;}
\def\drawpoints(#1){%
\foreach \pt in {#1} {\fill (\pt) circle (2 pt);}}
\def\labelpoints(#1){%
\foreach \pt in {#1} {\path coordinate["\pt" below] () at (\pt) ;}}
%-----------------------------------------------------------
%-------------------------------------------------------------
\def\Middles[#1](#2,#3,#4){
\AtBeginTikzMacro{#1}
\NewPoints{A}
\NewPoints{B}
\NewPoints{C}
% new level of nodes
\path[coordinate](barycentric cs:#2=1,#3=1) coordinate (\C);
\path[coordinate](barycentric cs:#2=1,#4=1) coordinate (\B);
\path[coordinate](barycentric cs:#3=1,#4=1) coordinate (\A);
\KeepUsefullNodes
}
%-------------------------------------------------------------
%-------------------------------------------------------------
\def\Medians[#1](#2,#3,#4){
% new level of nodes
\AtBeginTikzMacro{#1}
% human compatible node name
% In in the order of their output
\NewPoints{Gravite}
\NewPoints{A}
\NewPoints{B}
\NewPoints{C}
\Middles[\A,\B,\C](#2,#3,#4)
\tr[blue](\A,\B,\C)
\path[coordinate](barycentric cs:\A=1,\B=1,\C=1) coordinate (\Gravite);
\draw[green] (#2)--(\A) (#3)--(\B) (#4)--(\C);
\KeepUsefullNodes
}
%-------------------------------------------------------------
\begin{document}
\begin{tikzpicture}
\path coordinate (a) at (0,1)
coordinate (b) at (5,2)
coordinate (c) at (1,6);
\tr[red](a,b,c)
% as final use
% we take the three middles with human names
%\Middles[I,J,K](a,b,c)
%\tr[blue](I,J,K)
\Medians[G,I,J,K](a,b,c)
\tr[blue](I,J,K)
\drawpoints(G)
\labelpoints(G)
\end{tikzpicture}
\end{document}
答案1
最好的选择当然是expl3
:
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\NewPoints}{m}
{
\clist_map_inline:nn { #1 }
{
\cs_set:cpx { ##1 } { a-\NumNode-\theLittNode }
\stepcounter{LittNode}
}
}
\ExplSyntaxOff