1

I have created a \newcommand with tikz-commands which will help drawing the red and blue squares with some special anchoring. When I call my command \placePic it works as it should. But if I call it again directly after, it will not draw the square. It will only draw it if I either have two semicolons or two new lines directly after the first call.

What is the problem with my command?

These variants create the right outcome with the blue square:

\placePic{red rectangle}{(base-circle)}{specialRectangle}{-left point}{red};;
\placePic{blue rectangle}{(base-circle)}{specialRectangle}{-right point}{blue}

or

\placePic{red rectangle}{(base-circle)}{specialRectangle}{-left point}{red}

    
\placePic{blue rectangle}{(base-circle)}{specialRectangle}{-right point}{blue}

Should look like this

But will look like this when not having either double semicolon or new lines:

But looks like this

\documentclass[tikz,border=2cm]{standalone}
\usetikzlibrary{positioning,shapes.geometric}

% code by Andrew:
% https://tex.stackexchange.com/a/33765/13304
\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y};;%
}
\makeatother

% #1 = name of new placement
% #2 = where to place (e.g. myA-star)
% #3 = pic type
% #4 = internal node to anchor (eg. -left point)
% #5 = pic actions
% #6 = shift
\newcommand{\placePic}[7]
{
    \path pic (#1-PATH) at (0,0) {#3};
    \gettikzxy{(#1-PATH#4)}{\sdx}{\sdy}
    \gettikzxy{#2}{\sbx}{\sby}
    \def\newX{\sbx - \sdx}
    \def\newY{\sby - \sdy}
    \path [pic shift={(\newX,\newY)}] pic[draw,#5] (#1) at (0,0) {#3};
    \node[draw,circle,inner sep=0,orange] at (\newX,\newY) {};
}

\tikzset
{
    pic shift/.store in=\shiftcoord,
    pic shift={(0,0)},
    myRectangle/.pic=
    {
        \begin{scope}[shift={\shiftcoord}]
            \draw[very thick,pic actions] (0,0) rectangle (2,2);
            \node[very thick,fill=orange,circle,inner sep=0,minimum size=1mm] at (0,0) {};
            \node[draw,circle,minimum size=2mm,inner sep=0,thick,pic actions] (-circle) at (.5,.5) {};
            \node[draw,rectangle,minimum size=2mm,inner sep=0,thick,pic actions] (-square) at (.5,1.5) {};
            \node[draw,diamond,minimum size=2.5mm,inner sep=0,thick,pic actions] (-diamond) at (1.5,1.5) {};
            \node[draw,star,minimum size=2.5mm,inner sep=0,thick,pic actions] (-star) at (1.5,.5) {};
        \end{scope}
    },
    specialRectangle/.pic=
    {
        \begin{scope}[shift={\shiftcoord},thick,pic actions]
            \path[pic actions] (-2.5mm,-2.5mm) rectangle +(5mm,5mm);
            \path[pic actions] (-2.5mm,0) -- +(-0.7mm,0) coordinate (-left point);
            \path[pic actions] (2.5mm,0) -- +(0.7mm,0) coordinate (-right point);
            \path[pic actions] (0,2.5mm) -- +(0,0.7mm) coordinate (-upper point);
            \path[pic actions] (0,-2.5mm) -- +(0,-0.7mm) coordinate (-lower point);
            \coordinate (-upper left) at (-2.5mm,2.5mm);
            \coordinate (-upper right) at (2.5mm,2.5mm);
            \coordinate (-lower left) at (-2.5mm,-2.5mm);
            \coordinate (-lower right) at (2.5mm,-2.5mm);
        \end{scope}
    }
}




\begin{document}
\begin{tikzpicture}
    % Help lines (grid)
    \draw[help lines,ultra thin,step=5mm] (0,0) grid (5,5);
    \draw[help lines,thin,step=10mm] (0,0) grid (5,5);

    \coordinate (origin) at (0,0);

    \pic[black] (base) at (origin) {myRectangle};
    \placePic{red rectangle}{(base-circle)}{specialRectangle}{-left point}{red};; % <-- ISSUE
    \placePic{as rectangle}{(base-circle)}{specialRectangle}{-right point}{red}


\end{tikzpicture}
\end{document}

1 Answer 1

2

You put \newcommand{\placePic}[7] even if you're just using 5 arguments. So it took the two semicolon as arguments. Hence, changing it to \newcommand{\placePic}[5] will make it work!

You must log in to answer this question.

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