2

In this example, I want to create nodes generically named (for the user) \N{a}, \N{b} ... in each macro but named differently (for tikz) to avoid confusion and overwriting when calling macro successively.

The names of the nodes (tikz side) are a-i-x where i is the level of imbrication on calling macros and x is the name given by the user using \N{x}.

I tried this, but there is an inconvenient due to expansion of the macros. When I pass nodes to the next macro, I must anticipate the future expansion and have to use a different macro to refer to this node \Np{x}.

In the example below, I'd preferred to use \Middles[\N{b},\N{c},\N{d}](#2,#3,#4) instead of using \Middles[\Np{b},\Np{c},\Np{d}](#2,#3,#4)

How to unify the syntax ?

\documentclass{article}
\usepackage{tikz} 
\usetikzlibrary{quotes}

% counter for naming nodes
\newcounter{LittNode}

% level of calling
\pgfmathtruncatemacro{\NumNode}{1}

% node named #1 at the current level
\newcommand{\N}[1]{a-\NumNode-#1}

% node named #1 at the previous level
\newcommand{\Np}[1]{a-\NumNodep-#1}

% Pass the created nodes back to the calling macro
\newcommand{\KeepUsefullNodes}[1]{%
    % 
    \setcounter{LittNode}{1}
    \foreach \Nd in {#1} {%
        \coordinate (\Nd) at (\N{\alph{LittNode}}) ;
        \stepcounter{LittNode}
    }
    \pgfmathtruncatemacro{\NumNode}{\NumNode-1}
}

% one level up at the begining of a macro
\newcommand{\AtBeginTikzMacro}{%
    \pgfmathtruncatemacro{\NumNodep}{\NumNode}
    \pgfmathtruncatemacro{\NumNode}{\NumNode+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){

% new level of nodes
\AtBeginTikzMacro

\path[coordinate](barycentric cs:#2=1,#3=1) coordinate (\N{c});
\path[coordinate](barycentric cs:#2=1,#4=1) coordinate (\N{b});
\path[coordinate](barycentric cs:#3=1,#4=1) coordinate (\N{a});

\KeepUsefullNodes{#1}
}
%-------------------------------------------------------------

%-------------------------------------------------------------
\def\Medians[#1](#2,#3,#4){

% new level of nodes
\AtBeginTikzMacro

% use of `\Np{b}` ... to anticipate the expansion
\Middles[\Np{b},\Np{c},\Np{d}](#2,#3,#4)

% and refer now by `\N{b}` to the same node
\path[coordinate](barycentric cs:\N{b}=1,\N{c}=1,\N{d}=1) coordinate (\N{a});
\draw[green] (#2)--(\N{b}) (#3)--(\N{c}) (#4)--(\N{d});

\KeepUsefullNodes{#1}
}
%-------------------------------------------------------------

\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 Answer 1

1

Full customizable and human readable node names with no interaction between different macros.

\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}

\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){
\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .