\draw 和 \node[draw...] 产生不同的输出

\draw 和 \node[draw...] 产生不同的输出

不幸的是,我无法理解 Tikz 的基础知识。

我想画一个矩形,里面有两个椭圆。我需要添加标签,并想使用锚点。请看以下代码:

\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}

输出:

输出

第一个(1.)是正确的,但我想像U第二个(2.)那样使用锚点添加标签。

当我替换(在 1. 中):

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

与(参见2.):

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

出来的图片不正确。两个椭圆位于矩形的外面。

为什么?这应该与矩形的尺寸有关,但我真的不明白 2 的问题。

答案1

A\node将把您的节点置于当前坐标的中心,以便使用以下命令将其放置在正确的(0,0) rectangle (5,3)位置:anchoranchor=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}

在此处输入图片描述

相关内容