8

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
Stefan Kottwitz
  • 231,401
Łukasz Lew
  • 8,543
  • 12
  • 43
  • 42

3 Answers3

2
  • 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
Stefan Kottwitz
  • 231,401
2

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}
Sharpie
  • 12,844
  • 6
  • 48
  • 58
2

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.

Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195