[Tex/LaTex] Centering ellipses inside a rectangular node

subfloatstikz-pgf

The following TikZ picture consists of a rectangle containing two ellipses. I'm constructing this by putting a TikZ picture environment inside the node of an including TikZ picture environment. I've seen this idiom used before, but it doesn't seem particularly standard. I've also not seen any documentation on this in the PGF manual.

My main question is, what do I need to do to get the two ellipses centered in the rectangle? To be more precise I want both ellipses to be side by side, at equal distances from the top and bottom of the rectangle, and equal distances on either side of an imaginary vertical line dividing the rectangle into two equal pieces. I guess some resizing of either or both the rectangle and the ellipses will be necessary, and that's Ok. I haven't tried to do that, since I can't even get the ellipses centered correctly. I tried using at (0,0) for ellipse OUTPUT1, and at (3,0) for ellipse OUTPUT2, and experimented with changing those values. TikZ ignores the former as far as I can tell, but does move OUTPUT2 around in response to the latter, but I'm not sure what it is doing. I'm unclear overall on what this at option is doing.

A secondary question is: how do I get the text within the ellipses centered? I've been having problems with this in various contexts (within TikZ), so I must be missing something.

On a more general note, as with lots of things about TikZ, I'm unclear on how this is supposed to work. Is the sub-picture aware of the enclosing node? Is there some "natural" way we can teach the sub-ellipses to respect the bounds of the enclosing node?

I should add that I plan to be using this rectangle as an element in a matrix. Thus it needs to look like one unit. This obviates possible other strategies like using the fit library to draw around the ellipses, per How to write inside an ellipse in TikZ?. I haven't actually tried doing this, so don't know if it would work.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows, positioning}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5em}%
\begin{document}
\begin{tikzpicture}[auto]
  \tikzstyle{file} = [ellipse, rounded corners, text width=12em, text height=3em, minimum height=1em, minimum width=1em, align=center, draw]
  \tikzstyle{block} = [rectangle, rounded corners, text width=12em, text height=3em, minimum height=2em, minimum width=15em, align=center, draw]
  \node [block, minimum height=6em, minimum width=40em] (OUTPUT)
  {
    \begin{tikzpicture}[anchor=center]
      \node [file, inner sep=0pt, draw] (OUTPUT1) at (0,0) {MAP/PED files};
      \node [file, right=2em of OUTPUT1, inner sep=0pt, draw] at (3, 0) (OUTPUT2){TFAM/TPED files};
    \end{tikzpicture}
    \textbf{Data files}\\
  };
\end{tikzpicture}
\end{document}

Best Answer

Nesting tikzpictures is Not Good. One of the problems is with scoping: your ellipses are inheriting some characteristics from the rectangular node and that messes them up (which is why the alignment isn't working). You should do your best to draw things as a single picture. In this case there is no need to nest things. The rectangular node doesn't need to "contain" the ellipses formally, so long as they are drawn in the right places then they will look right.

To get the ellipses centred at the right places, use the calc library to compute coordinates. As you want them at the left and right "midpoints", we can easily compute their centres using the centre anchor and the east/west anchors. Specifically, ($(OUTPUT)!.5!(OUTPUT.west)$) is a point that is centred vertically and is halfway between the centre and the left-hand edge.

With regard to the centring of the text, that is the default behaviour and so adding keys like text width is in danger of changing that. There are always ways to restore it, but often it's best to avoid changing it in the first place. So, for example, to ensure that the ellipses are a certain side, use just the minimum width and minimum height and don't use text width and text height.

Here's some code that achieves what I think you want in a more concise way and with only one tikzpicture. It might not be what you actually want, but I can't tell from your example which numbers should be specified and which should be allowed to "grow". For example, it's not clear to me whether you want the boundary of the ellipses to be the same distance from the nearside edge as from the top/bottom, or you just want the centre to be centred.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows, positioning,calc}
\begin{document}
\begin{tikzpicture}
  \tikzstyle{file} = [ellipse, minimum height=3em, minimum width=12em, draw]
  \tikzstyle{block} = [rectangle, rounded corners, minimum height=2em, minimum width=15em, draw]
  \node [block, minimum height=6em, minimum width=40em] (OUTPUT) {\textbf{Data files}};
  \node [file, draw] (OUTPUT1) at ($(OUTPUT)!.5!(OUTPUT.west)$)  {MAP/PED files};
  \node [file, draw] (OUTPUT2) at ($(OUTPUT)!.5!(OUTPUT.east)$) {TFAM/TPED files};
\end{tikzpicture}
\end{document}

Result:

ellipses in a rectangle

And to put them in a matrix:

\documentclass{standalone}
%\url{http://tex.stackexchange.com/q/27793/86}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows, positioning,calc}
\begin{document}
\begin{tikzpicture}
  \tikzstyle{file} = [ellipse, minimum height=3em, minimum width=12em, draw]
  \tikzstyle{block} = [rectangle, rounded corners, minimum height=2em, minimum width=15em, draw]
\matrix {
  \node [block, minimum height=6em, minimum width=40em] (OUTPUT) {\textbf{Data files}};
  \node [file, draw] (OUTPUT1) at ($(OUTPUT)!.5!(OUTPUT.west)$)  {MAP/PED files};
  \node [file, draw] (OUTPUT2) at ($(OUTPUT)!.5!(OUTPUT.east)$) {TFAM/TPED files};
&
  \node [block, minimum height=6em, minimum width=40em] (INPUT) {\textbf{Data files}};
  \node [file, draw] (INPUT1) at ($(INPUT)!.5!(INPUT.west)$)  {MAP/PED files};
  \node [file, draw] (INPUT2) at ($(INPUT)!.5!(INPUT.east)$) {TFAM/TPED files};

\\};
\end{tikzpicture}
\end{document}

Result:

nodes in matrix cells