latextikz

tikz how to refer to pics or shapes created by newcommand and add anchors


I have two different ways to create a scaled image for use in a larger diagram. I don't know how to name the objects and add anchors to them. Do I have to somehow fit them into a named node? Or do .pics have anchors already but then what is the pics' name?

\documentclass{article}
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths

\usepackage{adjustbox}
\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}

\newcommand{\mazeN}[4]{%scale, x origin, y origin, line width
    \scalebox{1}{%
    \path (#2,#3) coordinate (O);
    \begin{scope}[line width=#4, cap=round, rounded corners=1pt,color=blue!50!black,scale=#1]
        \draw[] (O)  -| ++(9.0,1.0) +(-2.0,0) -| ++(0.0,3.0) +(0.0,5.0) |- ++(-2.0,0.0) |- ++(1.0,1.0) -- ++(0.0,1.0);
        \draw[] (O) ++(1.0,1.0) -| ++(-1.0,2.0) -| +(1.0,2.0) ++(0,0) |- ++(7.0,6.0) +(0.0,-1.0) |- ++(1.0,0.0) |- ++(-2.0,-2.0) -- ++(0.0,1.0);
        \draw[] (O) ++(1.0,6.0) |- ++(1.0,2.0)  +(0.0,1.0)  |- ++(3.0,0.0) -- ++(0.0,-3.0) coordinate (J1)  |- ++(-1.0,-1.0)  |- ++(-1.0,-1.0) |- ++(1.0,-1.0) (J1) -- ++(1.0,0) |- +(1.0,1.0) ++(0,0) -- ++(0.0,-2.0) ++(-1.0,0) -| ++(3.0,-1.0) -- ++(-3.0,0.0);
        \draw[] (O) ++(2.0,5.0) |- ++(2.0,2.0) |- ++(-1.0,-2.0) -- +(0.0,1.0) |- ++(-1.0,-1.0) |- ++(-1.0,-2.0) -| ++(1.0,-1.0) -| ++(2.0,-1.0) |- ++(2.0,1.0);

    % Start and End Points
    \draw[-latex,line width=#4/2,red] (O) ++(0.0,0.5) -- ++(1.0,0);
    \draw[-latex,line width=#4/2,red] (O) ++(8.5,8.0) -- ++(0.0,1.0);   
    \end{scope}
    }
}
    
\tikzset{
    maze/.pic={
    \begin{scope}[line width=2pt, cap=round, rounded corners=1pt,color=blue!50!black,]
        \path (0,0) coordinate (O);
        \draw[] (O)  -| ++(9.0,1.0) +(-2.0,0) -| ++(0.0,3.0) +(0.0,5.0) |- ++(-2.0,0.0) |- ++(1.0,1.0) -- ++(0.0,1.0);
        \draw[] (O) ++(1.0,1.0) -| ++(-1.0,2.0) -| +(1.0,2.0) ++(0,0) |- ++(7.0,6.0) +(0.0,-1.0) |- ++(1.0,0.0) |- ++(-2.0,-2.0) -- ++(0.0,1.0);
        \draw[] (O) ++(1.0,6.0) |- ++(1.0,2.0)  +(0.0,1.0)  |- ++(3.0,0.0) -- ++(0.0,-3.0) coordinate (J1)  |- ++(-1.0,-1.0)  |- ++(-1.0,-1.0) |- ++(1.0,-1.0) (J1) -- ++(1.0,0) |- +(1.0,1.0) ++(0,0) -- ++(0.0,-2.0) ++(-1.0,0) -| ++(3.0,-1.0) -- ++(-3.0,0.0);
        \draw[] (O) ++(2.0,5.0) |- ++(2.0,2.0) |- ++(-1.0,-2.0) -- +(0.0,1.0) |- ++(-1.0,-1.0) |- ++(-1.0,-2.0) -| ++(1.0,-1.0) -| ++(2.0,-1.0) |- ++(2.0,1.0);
    \end{scope}

    % Start and End Points
    \draw[-latex,line width=1pt,red]  (0,0) ++(0.0,0.5) -- ++(1.0,0);
    \draw[-latex,line width=1pt,red] (0,0) ++(8.5,8.0) -- ++(0.0,1.0);  
    }
}
    
\begin{document}

    \begin{tikzpicture}
        \draw[help lines] (0,0) grid (8,8);
        
        \mazeN{0.25}{1}{2}{2pt};
        \node[align=left] at (2.75,1.25) {newcommand example};
        
        \draw (1,5) pic[scale=0.25] {maze};
        \node[align=left] at (2.25,4.75) {pic example};
    \end{tikzpicture}
\end{document}

enter image description here


Solution

  • You can define coordinates in your pics, which you can later reference with a name prefix:

    \documentclass{article}
    \usepackage{amssymb} %maths
    \usepackage{amsmath} %maths
    
    \usepackage{adjustbox}
    \usepackage[svgnames]{xcolor}
    \usepackage{pgfplots}
        
    \tikzset{
        maze/.pic={
        \begin{scope}[line width=2pt, cap=round, rounded corners=1pt,color=blue!50!black,]
            \path (0,0) coordinate (O);
            \draw[] (O)  -| ++(9.0,1.0) +(-2.0,0) -| ++(0.0,3.0) +(0.0,5.0) |- ++(-2.0,0.0) |- ++(1.0,1.0) -- ++(0.0,1.0);
            \draw[] (O) ++(1.0,1.0) -| ++(-1.0,2.0) -| +(1.0,2.0) ++(0,0) |- ++(7.0,6.0) +(0.0,-1.0) |- ++(1.0,0.0) |- ++(-2.0,-2.0) -- ++(0.0,1.0);
            \draw[] (O) ++(1.0,6.0) |- ++(1.0,2.0)  +(0.0,1.0)  |- ++(3.0,0.0) -- ++(0.0,-3.0) coordinate (J1)  |- ++(-1.0,-1.0)  |- ++(-1.0,-1.0) |- ++(1.0,-1.0) (J1) -- ++(1.0,0) |- +(1.0,1.0) ++(0,0) -- ++(0.0,-2.0) ++(-1.0,0) -| ++(3.0,-1.0) -- ++(-3.0,0.0);
            \draw[] (O) ++(2.0,5.0) |- ++(2.0,2.0) |- ++(-1.0,-2.0) -- +(0.0,1.0) |- ++(-1.0,-1.0) |- ++(-1.0,-2.0) -| ++(1.0,-1.0) -| ++(2.0,-1.0) |- ++(2.0,1.0);
        \end{scope}
    
        % Start and End Points
        \draw[-latex,line width=1pt,red]  (0,0) ++(0.0,0.5) -- ++(1.0,0);
        \draw[-latex,line width=1pt,red] (0,0) ++(8.5,8.0) -- ++(0.0,1.0);  
        
        % define new coordinate at the bottom centre of the \pic
        \coordinate (-south) at (4.5,0);
        }
        
        
    }
        
    \begin{document}
    
        \begin{tikzpicture}
            \draw[help lines] (0,0) grid (8,8);       
            \pic[scale=0.25] (Alexandra) at (1,5) {maze};        
            \node[anchor=north] at (Alexandra-south) {pic example};
        \end{tikzpicture}
    \end{document}