如何修復以下 tikz 圖片?

如何修復以下 tikz 圖片?

我有以下 tikz 圖:

\documentclass{article}
\usepackage{tikz}
\usepackage{color}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.pathreplacing}


\begin{document}

  \begin{tikzpicture}

    \begin{scope}[xshift=-3.5cm,yshift=0cm,scale=1]
      \draw [draw=black, line width=0.35mm, fill=red] (0,0) -- (0,1.5) -- (2.5,1.5) -- (2.5, 0) node[midway,above] {} -- (0,0) node[midway,right] {};
      \node at (0.75,1.5) {};
    \end{scope}

    \begin{scope}[xshift=-3.5cm,yshift=0cm,scale=1]
      \draw [draw=black, line width=0.35mm, fill=red] (2.7,0) -- (2.7,1.5) -- (5.2,1.5) -- (5.2, 0) node[midway,above] {} -- (0,0) node[midway,left] {};
      \node at (0.75,1.5) {};
    \end{scope}

  \end{tikzpicture}

\end{document}
  1. 我想在矩形上方添加一些文字,但使用那裡的可用節點(帶有 {})會將文本放置在我意想不到的地方。 (特別是對於中途,上面)。

  2. 我想在矩形的中間寫一個字。

  3. 有一條奇怪的線連接兩個矩形,我不確定它的來源是什麼(在底部)。有辦法去除嗎?

答案1

嘗試:

\documentclass{article}
\usepackage{tikz}
\usepackage{color}
\usetikzlibrary{decorations.pathreplacing, matrix, positioning}

\begin{document} 
\begin{tikzpicture}[
  node distance = 3mm,
box/.style = {% definition of rectangle as node
    draw, line width=0.35mm, fill=red,
  text width=23mm, minimum height=15mm, align=center,
  inner sep=1mm, outer sep=0pt}
                    ]
\node (box1) [box, label=above:label 1] {text 1};
\node (box2) [box, right=of box1,
              label=above:label 2]   {text 2};
\end{tikzpicture}
\end{document}

在此輸入影像描述

正如您所看到的,我在繪製矩形時採用了非常不同的方法。不適scope用於定位圖片的單一元素。為此,我使用 TikZ 庫positioning,並將節點之間的距離設為 3mm。由於矩形是由節點繪製的,因此您可以輕鬆地在其中添加文字以及廣告標籤。

「矩形」的屬性,即節點,是用 style 定義的box,其中確定了它的寬度、高度、文字位置、填充等。

確實,TikZ 手冊很龐大,但是,非常值得閱讀以下部分:TikZ 是 kein Ziechenprogram

相關內容