I need to create some dynamic name macro and I want to create them from a coma separated list of parameter in #1
in one loop. foreach
is not the solution because it forces the use of gdef and I want only edef.
What would be an alternative solution ?
\makeatletter
\newcommand{\NewPoints}[1]{%
\foreach \i in {#1} {%
\expandafter\xdef\csname \i\endcsname{%
a-\NumNode-\theLittNode}
\stepcounter{LittNode}
}
}
\makeatother
Is not suitable because of xdef
global definition.
\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}
\edef
but can't use\xdef
?\foreach
used for defining macros.\Newpoints
macro to declare all macros at one.\xdef
seams to work.Manuel
's answer due to global definition issue. Then you can see where I thought to use aforeach
.\edef
and can't use\gdef
.