# How to create custom Tikz nodes?

I don't want to change style only. I want to create node that takes 3 numbers and display them with appropriate decorations. For instance:

\mynode{5,6,7}


Should display node with "a=5", "b=6" and "c=7" inside in 3 lines.

Update:

I have this code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}

\newcommand{\statnode}[3]{
\node[rectangle, draw]{
\begin{aligned} n &= #1 \\ \text{sum} &= #2 \\ \mu &= #3 \end{aligned}
}
}

\newcommand{\statnodex}[3]{
node[rectangle, draw]{
\begin{aligned} n &= #1 \\ \text{sum} &= #2 \\ \mu &= #3 \end{aligned}
}
}

\begin{document}

\begin{tikzpicture}[
level distance=100,
]

\statnode{1}{10}{0.1}
child {
\statnodex{1}{10}{0.1}
};
\end{tikzpicture}
\end{document}


But:

• it is ugly to have 2 commands
• it renders incorrectly for some reason
-
I want these nodes to be usable within TikZ tree (child command). –  Łukasz Lew Aug 8 '10 at 14:31
would it be possible to test (via some if statement) whether or not you are inside a \node command? –  Yossi Farjoun Aug 8 '10 at 15:53
It seems to me that the main issue is not that there is a double definition, but rather that the author must differentiate between using \statnode and \statnodex, similarto having to use \node vs. node in tikz –  Yossi Farjoun Aug 8 '10 at 16:44
Yes, that is exactly the problem –  Łukasz Lew Aug 9 '10 at 12:05

## 3 Answers

• Put the part, that both commands have in common, into another macro.
• use anchor=north if you would like to draw the line to the top but not further
-
anochor=north seems to have problems with deeper trees. –  Łukasz Lew Aug 8 '10 at 15:48

If you change the code which draws the tree you can use only one of your custom node macros:

  \draw \statnodex{1}{10}{0.1}
child {
\statnodex{1}{10}{0.1}
};


\node is just syntactic sugar for \draw node.

Now you could get really fancy and define a new multi-part node (see "Shapes with Multiple Text Parts" in the PGF manual, which is Section 39.6 in the 2.00 version) but unless you want to do this in many documents a macro seems fine.

-

Would the TikZ "chains" library work for your application?

Quick hack of your code to demonstrate:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{positioning}
\usepackage{amsmath}

\newcommand{\statnode}[3]{
\node[rectangle, draw, on chain]{
\begin{aligned} n &= #1 \\ \text{sum} &= #2 \\ \mu &= #3 \end{aligned}
}
}

\newcommand{\statnodex}[3]{
\node[rectangle, draw, on chain=going below, join]{
\begin{aligned} n &= #1 \\ \text{sum} &= #2 \\ \mu &= #3 \end{aligned}
}
}

\begin{document}

\begin{tikzpicture}[
start chain,
node distance=5mm
]

\statnode{1}{10}{0.1};
\statnodex{1}{10}{0.1};

\end{tikzpicture}
\end{document}

-