Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have created a figure for tikz that looks like this:

enter image description here

But when I add it inside a matrix node it became like this:

enter image description here

Why does this happen?

My code for the first is:

\begin{tikzpicture}
\node[](users){\includegraphics[scale=0.5]{icons/user.png} };
\node[below of=users,yshift=-20](google){\includegraphics[scale=0.3]{icons/googleplus.png} };
\node[below left of=users,yshift=-10](facebook){\includegraphics[scale=0.3]{icons/facebook.png} };
\node[below right of=users,yshift=-10](twitter){\includegraphics[scale=0.5]{icons/tweets.png} };
\draw[->,line width=1] (users)--(facebook);
\draw[->,line width=1] (users)--(google);
\draw[->,line width=1] (users)--(twitter);
\node[below of=google,yshift=8]{Content};
\end{tikzpicture}

My code for the second is:

\begin{tikzpicture}
\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick,rectangle, rounded corners,minimum width=1.5cm,minimum height=3cm]
\tikzstyle{fancytitle} =[fill=red, text=white,rounded corners]
\node [matrix,mybox] (data){
\node[minimum size=2cm]{};
\node[](users){\includegraphics[scale=0.5]{icons/user.png} };
\node[below of=users,yshift=-20](google){\includegraphics[scale=0.3]{icons/googleplus.png} };
\node[below left of=users,yshift=-10](facebook){\includegraphics[scale=0.3]{icons/facebook.png} };
\node[below right of=users,yshift=-10](twitter){\includegraphics[scale=0.5]{icons/tweets.png} };
\draw[->,line width=1] (users)--(facebook);
\draw[->,line width=1] (users)--(google);
\draw[->,line width=1] (users)--(twitter);
\node[below of=google,yshift=8]{Content};
\\
};
\node[fancytitle, right=10pt] at (data.north west) {Data};
\end{tikzpicture}
share|improve this question
    
Do you really need to place \nodes inside \nodes? If you are only trying to box the first diagrams, this can be done in different, more appropriate ways. –  Gonzalo Medina May 6 at 14:23
    
Could you please give me some info related to this? I used node inside node just for the box –  nikosdi May 6 at 14:38

1 Answer 1

up vote 2 down vote accepted

Next code shows how to draw this diagram with a matrix.

If you use a matrix don't need to use positioning, all nodes are adjusted according to rows and columns with certain column sep and row sep. In this case default column and row separation is used, except between first and second row where a particular separation has been fixed with \\[1cm].

A matrix is also a node, therefore all options applied to matrix node are also applied to inner nodes. So minimum width and minimum height in mybox fixes minimum width and height of inner nodes. We can change it with nodes={...} at the end of matrix options declaration. In any case I think it's better not using minimum width/height in matrix options because the matrix will be big enough to encompass all inner nodes.

Contents text below google icon has been drawn as a label instead of a regular node.

I've changed icons so, adjust them to your taste.

I've also changed tikzstyle to tikzset. Please look at Should \tikzset or \tikzstyle be used to define TikZ styles?

I've used a matrix because OP did it. There exist some other solutions to get similar results: Place icons and surround them with fit library help, draw inner figure inside a tcolorbox or mdframe, ...

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning, matrix}

\tikzset{mybox/.style={draw=red, fill=blue!20, very thick,
            rectangle, rounded corners,
            minimum width=1.5cm,minimum height=3cm},
    fancytitle/.style={fill=red, text=white,rounded corners}}

\begin{document}

\begin{tikzpicture}
\node [matrix,mybox, nodes={minimum height=0pt}] (data){
& 
\node[](users){\includegraphics[scale=0.25]{user} }; & \\[1cm]

\node[](facebook){\includegraphics[scale=0.25]{facebook} }; & & 
\node[](twitter){\includegraphics[scale=0.25]{tweeter} };
\\
& \node[label=below:Content](google){\includegraphics[scale=0.25]{google+} }; \\
};
\draw[->,line width=1] (users)--(facebook);
\draw[->,line width=1] (users)--(google);
\draw[->,line width=1] (users)--(twitter);
\node[fancytitle, right=10pt] at (data.north west) {Data};
\end{tikzpicture}

\end{document}

enter image description here

2nd version: No matrix but positioning and background fit node:

\begin{tikzpicture}

\node (users){\includegraphics[scale=0.25]{user}}; 

\node[below left = 1cm and 1mm of users](facebook){\includegraphics[scale=0.25]{facebook} }; 
\node[below right = 1cm and 1mm of users](twitter){\includegraphics[scale=0.25]{tweeter} };
\node[label={[name=label]below:Content}, below=18mm of users](google){\includegraphics[scale=0.25]{google+} };

\draw[->,line width=1] (users)--(facebook);
\draw[->,line width=1] (users)--(google);
\draw[->,line width=1] (users)--(twitter);
\begin{scope}[on background layer]
\node[fit={(users) (twitter) (label) (facebook)}, draw=red, fill=blue!20, very thick, rounded corners, append after command={node[fancytitle, anchor=west] at ([xshift=3mm]\tikzlastnode.north west) {Data}}] {};
\end{scope}
\end{tikzpicture}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.