新的 tikz 命令产生“失控参数”以及其他错误

新的 tikz 命令产生“失控参数”以及其他错误

我正在尝试为 tikz 编写一个新命令,创建一个带有一些文本的侧向三角形(我将它用于模拟框图。它需要三个输入 - x 位置、y 位置和要放入其中的文本。(x,y)是三角形的左下角。当我运行代码时,它会爆炸。我得到了 16 个长错误,包括“失控参数”、“未知运算符‘$’”等等。

\documentclass{article}
\usepackage{tikz}
\newcommand{\analogblock}[3]{
\begin{tikzpicture}
\draw (#1, #2) -- (#1, 1);
\draw (#1, #2) -- (#1 + 1, #2/2);
\draw (#1, 1) -- (#1 + 1, #2/2);
\node[draw] at (#1 + #1/2,#2/2) {#3};
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\analogblock{0,0, $-\int$}
\end{tikzpicture}
\end{document}

这里发生了什么?

输出的草图如下:

在此处输入图片描述

答案1

问题在于,多个参数宏的每个参数都应该用一对花括号括起来,而不是用逗号分隔:

\documentclass{article}
\usepackage{tikz}
\newcommand{\analogblock}[3]{
\begin{tikzpicture}
\draw (#1, #2) -- (#1, 1);
\draw (#1, #2) -- (#1 + 1, #2/2);
\draw (#1, 1) -- (#1 + 1, #2/2);
\node[draw] at (#1 + #1/2,#2/2) {#3};
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\analogblock{0}{0}{$-\int$}
\end{tikzpicture}
\end{document}

如果由于某种原因您想要使用逗号分隔的宏,请尝试:

\documentclass{article}
\usepackage{tikz}
\def\analogblock#1{\analogblockhelper#1\nil}
\def\analogblockhelper#1,#2,#3\nil{%
   \begin{tikzpicture}
   \draw (#1, #2) -- (#1, 1);
   \draw (#1, #2) -- (#1 + 1, #2/2);
   \draw (#1, 1) -- (#1 + 1, #2/2);
   \node[draw] at (#1 + #1/2,#2/2) {#3};
   \end{tikzpicture}%
}
\begin{document}
\begin{tikzpicture}
\analogblock{0,0, $-\int$}
\end{tikzpicture}
\end{document}

答案2

如果您需要构建块来绘制某些方案,那么您应该考虑将它们绘制为节点或小图片\pic。使用节点比使用锚点有一些优势pic,但它们仅限于选定的节点形状。

此类构件的例子有:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{calc,
                positioning,
                shapes.geometric}
\newcommand\ppbb{path picture bounding box}
\tikzset{
shorten <>/.style = {shorten <=#1, shorten >=#1},
sx/.style = {xshift=#1 mm},
sy/.style = {yshift=#1 mm},
%---------------------------------------------------------------%
%   ANP, parameter:  #1: fill color                             %
%---------------------------------------------------------------%
amp/.style = {% amplifuer
    shape=isosceles triangle, draw, fill=#1,
    minimum width=12mm, minimum height=16mm,
    inner sep=1mm, outer sep=0mm,
    node contents={$-f$},
            },% end of amp
amp/.default=white,
%---------------------------------------------------------------%
%   Binary SUM, parameter:  #1: fill color                      %
%---------------------------------------------------------------%
bsum/.style = {% Binary SUM
    shape=rectangle, draw, fill=#1,
    minimum size=6mm, align=center,
    inner sep=1mm, outer sep=0mm,
    path picture={%
    \draw[thick,fill=white]       (\ppbb.center)  circle  (2mm);
    \draw[thick,shorten <>=1mm]   (\ppbb.north)  edge (\ppbb.south)
                   (\ppbb.west)    --  (\ppbb.east);
                },
    node contents={},
            },% end of cmp
bsum/.default=white,
%%-------------------------------------------------------------%%
%   COMPARATOR                                                  %
%---------------------------------------------------------------%
cmp/.style = {% CoMParator
    shape=rectangle, draw,
    minimum height=12mm, text width=7mm, align=center,
    inner sep=1mm, outer sep=0mm,
    path picture={%
    \draw[shorten <>=1mm]   (\ppbb.west)  edge (\ppbb.east)
                            (\ppbb.north)  --  (\ppbb.south);
    \draw[very thick]   ([sx=+2,sy=-3] \ppbb.north)  -| (\ppbb.center)
                        ([sx=-2,sy=+3] \ppbb.south)  -| (\ppbb.center);
                },
    node contents={},
            },% end of cmp
%%-------------------------------------------------------------%%
%   MULTIPLYING, parameters: #1: fill color                     %
%---------------------------------------------------------------%
mlt/.style={fill=#1,
    rectangle, draw=black, minimum size=6mm,
    path picture={\draw[very thick,shorten <>=1.5mm,-]
    (\ppbb.north west) edge (\ppbb.south east)
    (\ppbb.south west)  --  (\ppbb.north east);
                },% end of node contents
            node contents={}},
mlt/.default = white,
%%-------------------------------------------------------------%%
%   PHASE COMPARATOR, parameters:  #1: fill color               %
%---------------------------------------------------------------%
phc/.style={fill=#1,
    circle, draw=black, minimum size=6mm,
    path picture={\draw[very thick,-]
    (\ppbb.north west) -- (\ppbb.south east)
    (\ppbb.south west) -- (\ppbb.north east);
                },% end of node contents
            node contents={}},
phc/.default = white,
%%-------------------------------------------------------------%%
%   SUM, parameters:  #1: fill color                            %
%---------------------------------------------------------------%
sum/.style={fill=#1,
    circle, draw=black, minimum size=6mm,
    path picture={\draw[very thick,shorten <>=1mm,-]
    (\ppbb.north) edge (\ppbb.south)
    (\ppbb.west)   --  (\ppbb.east);
                },% end of node contents
            node contents={}},
sum/.default = white,
}

\begin{document}
\begin{tikzpicture}[node distance=3mm]
\node (n1) [bsum];
\node (n2) [cmp, right=of n1];
\node (n3) [mlt, right=of n2];
\node (n4) [phc, right=of n3];
\node (n5) [sum, right=of n4];
\node (n6) [amp, right=of n5];
%
\draw (n6.east) -| ++ (1,1) -| ([sx=-10] n1.west) -- (n1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

以及我评论中的示例,但现在使用append after command选项:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, calc}

\begin{document}
    \begin{tikzpicture}[
node distance= 5mm,
            >= Straight Barb,
block/.style = {
    rectangle, draw, thick,
    minimum height=9mm, minimum width=12mm,
                  },
  sat/.style = {block,
        append after command={
             \pgfextra{\let\LN\tikzlastnode
                \draw[->] ($(\LN.south) + (0,0.5ex)$) edge ($(\LN.north) + (0,-0.5ex)$)
                          ($(\LN.west)  + (0.5ex,0)$)  to  ($(\LN.east)  + (-0.5ex,0)$);
                \draw[very thick, opacity=0.75]
            ($(\LN.south west) + (1ex,1ex)$) --
                ($(\LN.south) + (-1ex,1ex)$) --
                ($(\LN.north) + (1ex,-1ex)$) --
                ($(\LN.north east) + (-1ex,-1ex)$);
                    }% end \pgfextra
                            },% end after command
                }
    ]
\node (X) [,sat] at (0,0) {};
    \end{tikzpicture}
\end{document}

这使:

在此处输入图片描述

相关内容