How to create custom shapes with \newcommand in TikZ and shift them
I do get an error when testing that example.
Anyways, note that the options to a \draw
and similar is a list of comma separated entries, so if the <value>
in a <key>=<value>
pair contains a comma, you need to use <key>={<value>}
, otherwise the parser is confused. Hence, use shift={(#1)}
and your example works as expected.
Anyways, some other suggestions:
scope
You could simplify your code a bit by using a scope
environment:
\newcommand{\tree}[4]{
\begin{scope}[shift={(#1)}]
\draw ...
\end{scope}
}
Less manual drawing
Instead of drawing everything with explicit coordinates, you could place nodes relative to each other, and add the draw
option to the nodes to draw their border.
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand{\tree}[4]{
\begin{scope}[
shift={(#1)},
every node/.style={
draw,
font=\footnotesize,
node distance=3mm and 4mm
}
]
\node (a) {#4};
\node [circle,inner sep=2pt,above=of a] (op) {$-$};
\node [above left=of op] (b) {#2};
\node [above right=of op] (c) {#3};
\draw (a) -- (op) -| (b)
(op) -| (c);
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\tree{0,0}{100}{54}{8}
\end{tikzpicture}
\end{document}
pic
s
TikZ 3.0 introduced the pic
concept, which has the purpose of making small, reusable diagrams.
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
pics/tree/.style args={#1#2#3}{
code={
\begin{scope}[
every node/.style={
draw,
font=\footnotesize,
node distance=3mm and 4mm
}
]
\node (a) {#3};
\node [circle,inner sep=2pt,above=of a] (op) {$-$};
\node [above left=of op] (b) {#1};
\node [above right=of op] (c) {#2};
\draw (a) -- (op) -| (b)
(op) -| (c);
\end{scope}
}
}
}
\begin{document}
\begin{tikzpicture}
\pic at (0,0) {tree={100}{54}{8}};
\end{tikzpicture}
\end{document}
small variation of \pic
concept in Torbjørn T. answer:
\documentclass[12pt, tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
calc,
positioning,
quotes}
\tikzset{node distance = 7mm,
TBbox/.style = {draw, minimum width=12mm, minimum height=5mm,
inner sep=1mm, outer sep=0mm},
TBsum/.style = {circle, draw, minimum size=5mm, inner sep=0mm, font=\Large},
pics/TBB/.style n args % Tree Building Block
= {4}{code={
\node (@tbb-1) [TBbox] {#1};
\node (@tbb-2) [TBbox, right=6mm of @tbb-1] {#2};
\node (@tbb-3) [TBsum, below=1mm of $(@tbb-1.south)!0.5!(@tbb-2.south)$] {$#3$};
\node (@tbb-4) [TBbox, below=3mm of @tbb-3] {#4};
\coordinate (-in1) at (@tbb-1.north);
\coordinate (-in2) at (@tbb-2.north);
\coordinate (-out) at (@tbb-4.south);
\draw[gray,->] (@tbb-1) |- (@tbb-3);
\draw[gray,->] (@tbb-2) |- (@tbb-3);
\draw[gray,->] (@tbb-3) -- (@tbb-4);
}% end of code
},% end of style
every edge quotes/.style = {font=\footnotesize, inner sep=1mm, auto}
}% endof tikzset
\begin{document}
\begin{tikzpicture}
\pic (a) at (0,0) {TBB={100} % input 1
{54} % input 2
{-} % math operation
{8} % result
};
\pic (b) [below=of a-out] {TBB={8}{2}{+}{10}};
\draw[->] (a-out) to ["copy"] (b-in1);
\pic (c) [below=of b-out] {TBB={10}{2}{\times}{\textbf{20}}};
\draw[->] (b-out) to ["copy"] (c-in1);
\end{tikzpicture}
\end{document}