3

Unfortunately, I am not able to understand the basics of Tikz.

I would like to draw a rectangle with two ellipses in it. I need to add labels and would like to use anchor points. Please look at the following code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}

\begin{document}

1.
\begin{tikzpicture}
% frame
\draw (0,0) rectangle (5, 3);

\node [draw,fill=blue!20,draw=blue,line width=1pt,ellipse,minimum width=2cm,minimum height=1.5cm] (TP) at (1.5,2) {};
\node [right] at (TP.west) {$A$};

\node [draw,fill=red!20,draw=red,line width=1pt,ellipse,minimum width=2cm,minimum height=1.5cm] (TN) at (3.5,1) {};
\node [right] at (TN.west) {$B$};
\end{tikzpicture}

2.
\begin{tikzpicture}
% frame
\node [draw,rectangle,minimum width = 5cm, minimum height = 3cm] (F) at (0,0) {};
\node [below left] at (F.north east) {U};

\node [draw,fill=blue!20,draw=blue,line width=1pt,ellipse,minimum width=2cm,minimum height=1.5cm] (TP) at (1.5,2) {};
\node [right] at (TP.west) {$A$};

\node [draw,fill=red!20,draw=red,line width=1pt,ellipse,minimum width=2cm,minimum height=1.5cm] (TN) at (3.5,1) {};
\node [right] at (TN.west) {$B$};
\end{tikzpicture}

\end{document}

that outputs:

output

The first one (1.) is correct, but I would like to add the label U as in the second one (2.), using anchor points.

When I substitute (in 1.):

\draw (0,0) rectangle (5, 3);

with (see 2.):

\node [draw,rectangle,minimum width = 5cm, minimum height = 3cm] (F) at (0,0) {};

the picture that comes out is not correct. The two ellipses are placed outside of the rectangle.

Why? It should be related to the dimensions of the rectangle but I cannot really understand the problem with 2.

2
  • For the 'U' inside the rectangle 1. add the code \node at (4.7,2.7) () {U}; inside the first tikzpicture environment. May 26 at 8:19
  • rectangle (5, 3) will draw a rectangle that is 5cm times 3cm big. On the other hand in your node the rectangle will be (5cm + inner-sep) times (3cm + inner-sep). If you want to get your exact dimensions add inner sep=0 to your node's options.
    – Skillmon
    May 26 at 8:55

1 Answer 1

8

A \node will centre your node on the current coordinate, to get the same placement as (0,0) rectangle (5,3) you'll need to anchor it at the correct spot by using anchor=south west:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}

\begin{document}

1.
\begin{tikzpicture}
% frame
\draw (0,0) rectangle (5, 3);

\node [draw,fill=blue!20,draw=blue,line width=1pt,ellipse,minimum width=2cm,minimum height=1.5cm] (TP) at (1.5,2) {};
\node [right] at (TP.west) {$A$};

\node [draw,fill=red!20,draw=red,line width=1pt,ellipse,minimum width=2cm,minimum height=1.5cm] (TN) at (3.5,1) {};
\node [right] at (TN.west) {$B$};
\end{tikzpicture}

2.
\begin{tikzpicture}
% frame
\node [draw,rectangle,minimum width = 5cm, minimum height = 3cm, anchor=south west, inner sep=0] (F) at (0,0) {};
\node [below left] at (F.north east) {U};

\node [draw,fill=blue!20,draw=blue,line width=1pt,ellipse,minimum width=2cm,minimum height=1.5cm] (TP) at (1.5,2) {};
\node [right] at (TP.west) {$A$};

\node [draw,fill=red!20,draw=red,line width=1pt,ellipse,minimum width=2cm,minimum height=1.5cm] (TN) at (3.5,1) {};
\node [right] at (TN.west) {$B$};
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .