我用 newcommand 创建的命令无法按顺序调用,除非使用双分号或换行符

我用 newcommand 创建的命令无法按顺序调用,除非使用双分号或换行符

我创建了一个\newcommandwithtikz命令,它将有助于绘制具有一些特殊锚点的红色和蓝色方块。当我调用我的命令时\placePic,它会正常工作。但如果我之后直接再次调用它,它就不会绘制正方形。只有当我在第一次调用后直接有两个分号或两个新行时,它才会绘制它。

我的命令出了什么问题?

这些变体通过蓝色方块创建了正确的结果:

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

或者

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

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

应该看起来像这样

但当没有双分号或换行符时,看起来会像这样:

但看起来像这样

\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

\newcommand{\placePic}[7]即使您只使用 5 个参数,您也会输入。因此,它以两个分号作为参数。因此,将其更改为\newcommand{\placePic}[5]将使其工作!

相关内容