0

I am trying to write a new command for tikz that creates a sideways triangle with some text in it (I'm using it for an analog block diagram. It takes in three inputs - x position, y position, and the text to go inside. (x,y) is the lower left corner of the triangle. When I run the code, it blows up. I get a selection of 16 long errors including "runaway argument", "unknown operator '$'", and so forth.

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

What's going on here?

Sketch of what the output should look like:

enter image description here

7
  • 3
    The arguments must be each enclosed by a pair of braces, not comma-separated: \analogblock{0}{0}{$-\int$}. If you want comma-separated values you have to define a command to do so. See this. Jun 27, 2018 at 19:12
  • 1
    off-topic: i suspect that \analogblock is intended to be building block for some scheme. in this case can be more appropriate define it as miniature picture: \pic or as node, in which by us of path picture={...} draw contain of node.
    – Zarko
    Jun 27, 2018 at 19:26
  • @Zarko thank you, I will look into that (you are right, I'm trying to draw a diagram with basically a bunch of these and some lines connecting).
    – auden
    Jun 27, 2018 at 19:39
  • see, if this can help: ... \usetikzlibrary{calc} \newcommand\ppbb{path picture bounding box}\begin{document}\begin{tikzpicture}[block/.style={rectangle,draw,thick,fill=white,minimum height=9mm,minimum width=12mm},sat/.style={block,path picture={\draw[-latex,thin]($(\ppbb.south)+(0,0.5ex)$) edge ($(\ppbb.north)+(0,-0.5ex)$) ($(\ppbb.west)+(0.5ex,0)$) to ($(\ppbb.east)+(-0.5ex,0)$);\draw[very thick]($(\ppbb.south west)+(1ex,1ex)$)--($(\ppbb.south)+(-1ex,1ex)$)--($(\ppbb.north)+(1ex,-1ex)$)--($(\ppbb.north east)+(-1ex,-1ex)$);}}]\node[sat] {};\end{tikzpicture}...
    – Zarko
    Jun 27, 2018 at 20:14
  • @Zarko do you want to write that up as an answer?
    – auden
    Jun 27, 2018 at 21:22

2 Answers 2

3

The problem is that multiple argument macros should have each argument surrounded by a pair of curly braces, not delimited by commas:

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

If for some reason you want to use a comma delimited macro, try:

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

if you need building blocks to draw some scheme, than you should consider to draw them as nodes or small pictures \pic. using nodes has some advantages over pic regarding anchors, however they are limited to selected node shape.

examples of such building blocks are:

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

enter image description here

and example from my comment, but now with use of append after command option:

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

which gives:

enter image description here

1
  • Wow, this is great! I wish I could give you more upvotes.
    – auden
    Jun 27, 2018 at 23:15

You must log in to answer this question.

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