# Creating boxes around items in Tikz matrix

I have the following Tikz picture:

   \begin{center}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=2.5cm,
row sep=1cm,
]
{
$v_1$ &        & $v_3$ & $f_2$ & $v_5$ \\
&  $f_1$ &       &       &       \\
$v_2$ &        & $v_4$ &       &       \\
};
\draw[->] (tree-1-1) -- (tree-2-2) node [near end,above, rounded corners] {$1$};
\draw[->] (tree-3-1) -- (tree-2-2) node [near end,below] {$2$};
\draw[->] (tree-2-2) -- (tree-1-3) node [near start,above] {$1$};
\draw[->] (tree-2-2) -- (tree-3-3) node [near start,below] {$2$};
\draw[->] (tree-1-3) -- (tree-1-4) node [near end,above] {$1$};
\draw[->] (tree-1-4) -- (tree-1-5) node [near start,above] {$1$};
\end{tikzpicture}
\end{center}


Is it possible to get boxes around the $v_i$ and $f_j$? For instance circles? I would like to have different boxes for the $v_i$ and $f_j$, because they have a different meaning.

I'm new to Tikz btw.

Kind regards, Jens

• You can add nodes={circle,draw} to the matrix style. – Kpym Apr 3 at 16:34
• @JouleV you can do it, I have no time now, thanks. – Kpym Apr 3 at 16:38
• Thanks for the suggestion, but I would like different boxes for the $v_i$ and $f_j$. And I presume that this is not possible in setting the matrix style. Am I correct? – Jens Wagemaker Apr 3 at 16:43

### If you want to have the same "box" for all nodes

You can add option nodes={draw,circle} if you want all nodes to be in circle "box":

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=2.5cm,
row sep=1cm,
nodes={draw,circle}
]
{
$v_1$ &        & $v_3$ & $f_2$ & $v_5$\\
&  $f_1$ &       &       &      \\
$v_2$  &        & $v_4$ &       &      \\
};
\draw[->] (tree-1-1) -- (tree-2-2) node [near end,above, rounded corners] {$1$};
\draw[->] (tree-3-1) -- (tree-2-2) node [near end,below] {$2$};
\draw[->] (tree-2-2) -- (tree-1-3) node [near start,above] {$1$};
\draw[->] (tree-2-2) -- (tree-3-3) node [near start,below] {$2$};
\draw[->] (tree-1-3) -- (tree-1-4) node [near end,above] {$1$};
\draw[->] (tree-1-4) -- (tree-1-5) node [near start,above] {$1$};
\end{tikzpicture}
\end{document}


All credits go to Kpym, and this nice proposal is shown in this answer by the author's request.

### If you want different styles for different types of nodes

using matrix is still ok, but it is more complicated. I propose a normal solution:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\begin{scope}[every node/.style={minimum size=1cm,draw,circle}]
\node (v1) at (0,0) {$v_1$};
\node (v2) at (0,-2) {$v_2$};
\node (v3) at (5,0) {$v_3$};
\node (v4) at (5,-2) {$v_4$};
\node (v5) at (10,0) {$v_5$};
\end{scope}
\begin{scope}[every node/.style={minimum size=1cm,draw,rectangle}]
\node (f1) at (2.5,-1) {$f_1$};
\node (f2) at (7.5,0) {$f_2$};
\end{scope}
\draw[->] (v1) -- (f1) node [near end,above, rounded corners] {$1$};
\draw[->] (v2) -- (f1) node [near end,below] {$2$};
\draw[->] (f1) -- (v3) node [near start,above] {$1$};
\draw[->] (f1) -- (v4) node [near start,below] {$2$};
\draw[->] (v3) -- (f2) node [near end,above] {$1$};
\draw[->] (f2) -- (v5) node [near start,above] {$1$};
\end{tikzpicture}
\end{document}