使用 TikZ 绘制一个矩形然后绘制一个圆形

使用 TikZ 绘制一个矩形然后绘制一个圆形

我正在尝试从官方 TikZ 文档中学习 TikZ。

我在几个地方卡住了。我想画一个框,然后在一条线上画一个圆圈。

在第一种方法中,我可以提供坐标,但无法正确设置节点中的文本。

在第二种方法中,我找不到与第一种方法类似的坐标给出方法。

在第三种方法中,我可以提供坐标,但不能设置宽度。

第一种方法

\begin{tikzpicture}
\draw[top color=blue,bottom color=blue]  (0,0) rectangle +(3,2) node {A} (4,0) circle (4mm) node {B};
\end{tikzpicture}

第二种方法

\begin{tikzpicture}[node distance=20mm,
boxx/.style={ 
rectangle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily
},
circlee/.style={circle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily}]
\node (A) [boxx] {A};
\node (B) [circlee, right=of A] {B};
\end{tikzpicture}

\begin{tikzpicture}
[node distance = 20mm,
every rectangle node/.style={rectangle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily},
every circle node/.style={circle,
minimum size=8mm,
very thick,draw=red!50!red!50,
top color=blue,
bottom color=red!50!black!20,
font=\ttfamily}
]
\draw (0,0) node[rectangle] {A};
\draw (1,0) node[circle] {B};
\end{tikzpicture}

该图的输出如下:

在此处输入图片描述

能否使用这三种方法得到一个长 3 厘米、高 2 厘米的矩形和一个半径为 4 毫米的圆。它们两个相距 20 毫米。矩形 A 的中心有文字“A”,圆 B 的中心有文字“B”。

答案1

你的意思是你想要这样的东西吗?

第一种方法需要手动操作,因为首先绘制的节点不是矩形的大小。(并且计算可能不正确,但您需要类似的东西。)

\begin{tikzpicture}
  \draw [draw=red, very thick, top color=blue, bottom color=red!50!black!20]  (0,0) rectangle +(3,2) node (A) [midway] {A} (A) +(37mm,0) circle (4mm) node {B};
\end{tikzpicture}

第二和第三只需要考虑节点具有锚点并且放置相对于这些锚点的事实,一起设置最小宽度和高度。

\begin{tikzpicture}[
  node distance=20mm,
  boxx/.style={
    rectangle,
    minimum size=8mm,
    minimum width=30mm,
    minimum height=20mm,
    very thick,
    draw=red!50!red!50,
    top color=blue,
    bottom color=red!50!black!20,
    font=\ttfamily,
    anchor=east,
  },
  circlee/.style={circle,
    minimum size=8mm,
    very thick,draw=red!50!red!50,
    top color=blue,
    bottom color=red!50!black!20,
    anchor=west,
    font=\ttfamily}
  ]
  \node (A) [boxx] {A};
  \node (B) [circlee, right=of A] {B};
\end{tikzpicture}
\begin{tikzpicture}
  [x = 20mm,
  every rectangle node/.style={rectangle,
    minimum size=8mm,
    minimum width=30mm,
    minimum height=20mm,
    very thick,
    draw=red!50!red!50,
    top color=blue,
    bottom color=red!50!black!20,
    anchor=east,
    font=\ttfamily},
  every circle node/.style={circle,
    minimum size=8mm,
    very thick,draw=red!50!red!50,
    top color=blue,
    bottom color=red!50!black!20,
    anchor=west,
    font=\ttfamily}
  ]
  \draw (0,0) node [rectangle] {A};
  \draw (1,0) node [circle] {B};
\end{tikzpicture}

3 种方法

完整代码:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \draw [draw=red, very thick, top color=blue, bottom color=red!50!black!20]  (0,0) rectangle +(3,2) node (A) [midway] {A} (A) +(37mm,0) circle (4mm) node {B};
\end{tikzpicture}
\begin{tikzpicture}[
  node distance=20mm,
  boxx/.style={
    rectangle,
    minimum size=8mm,
    minimum width=30mm,
    minimum height=20mm,
    very thick,
    draw=red!50!red!50,
    top color=blue,
    bottom color=red!50!black!20,
    font=\ttfamily,
    anchor=east,
  },
  circlee/.style={circle,
    minimum size=8mm,
    very thick,draw=red!50!red!50,
    top color=blue,
    bottom color=red!50!black!20,
    anchor=west,
    font=\ttfamily}
  ]
  \node (A) [boxx] {A};
  \node (B) [circlee, right=of A] {B};
\end{tikzpicture}
\begin{tikzpicture}
  [x = 20mm,
  every rectangle node/.style={rectangle,
    minimum size=8mm,
    minimum width=30mm,
    minimum height=20mm,
    very thick,
    draw=red!50!red!50,
    top color=blue,
    bottom color=red!50!black!20,
    anchor=east,
    font=\ttfamily},
  every circle node/.style={circle,
    minimum size=8mm,
    very thick,draw=red!50!red!50,
    top color=blue,
    bottom color=red!50!black!20,
    anchor=west,
    font=\ttfamily}
  ]
  \draw (0,0) node [rectangle] {A};
  \draw (1,0) node [circle] {B};
\end{tikzpicture}
\end{document}

相关内容