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.

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}
share|improve this question

2 Answers 2

up vote 4 down vote accepted

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

share|improve this answer
    
Well, my major reason for the nesting was because this whole construction (rectangle + ellipses) is supposed to be one element of a matrix array. A single node works, but I don't know if multiple nodes will. –  Faheem Mitha Sep 7 '11 at 19:30
    
By "a matrix array" do you mean a TikZ matrix? –  Loop Space Sep 7 '11 at 19:35
    
Yes. In hindsight I should have included that in the example, but I was trying to keep it simple. Shall I change it? Also, the matrix indeed does not seem to take kindly to having multiple nodes shoved into a single space. –  Faheem Mitha Sep 7 '11 at 19:42
    
You wrote, "your ellipses are inheriting some characteristics from the rectangular node and that messes them up". Is it possible to make them not inherit? –  Faheem Mitha Sep 7 '11 at 19:46
    
I don't get any problems when I put the above in to a matrix. I'll edit in a matrix example. –  Loop Space Sep 7 '11 at 19:47

Would this be what you're after? The secret lies in using the Tikz fit library, and removing text height, which is messing up your vertical alignment in the ellipses:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows, positioning,fit}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5em}%
\begin{document}
\begin{tikzpicture}[auto]
  \tikzstyle{file} = [ellipse, text width=10em,  minimum height=3em, minimum width=1em, align=center, draw]
  \tikzstyle{block} = [rectangle, rounded corners, text width=12em,  minimum height=2em, minimum width=15em, align=center, draw]
      \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};
  \node [block, inner xsep=1em,inner ysep=3em,fit=(OUTPUT1) (OUTPUT2)] (OUTPUT) {};
  \node [anchor=south] at (OUTPUT.south) {\textbf{Data files}};
\end{tikzpicture}
\end{document}
share|improve this answer
    
As I recently added to the question, the plan is to have this rectangle (with accompanying sub-ellipses) as an element of a matrix layout. I haven't tried this, but I imagine that having the ellipses as nodes in the same scope would create problems with the matrix layout, which expects one node per location. I might be wrong - I'll give it a try. –  Faheem Mitha Sep 7 '11 at 19:09
    
Ah. Not sure, but could each matrix cell contain a complete tikzpicture? –  Brent.Longborough Sep 7 '11 at 19:16
    
I think TikZ likes to have embedded tikzpictures inside nodes, but I'll let the experts comment on that. I'll give your solution a try and report back. Thanks. –  Faheem Mitha Sep 7 '11 at 19:28
    
This sort of works, but the matrix structure (I assume) sort of pushes the placement of the second ellipse to the right. I'm not good enough at this to understand why this is the case. So, the diagram that is produced is lopsided. If anyone cares to explain why @Andrew's works and this doesn't, I'd be interested. I can post an example of this behavior if necessary. –  Faheem Mitha Sep 7 '11 at 21:05
    
@Faheem: When I put Brent's code in a matrix then it looks fine. Maybe your matrix code is more complicated than just a simple matrix and it's some of the other stuff that is messing things around. –  Loop Space Sep 8 '11 at 7:54

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.