編輯

編輯

我想在 TikZ 中製作以下軟體架構:

在此輸入影像描述

請參閱下面我的程式碼:

\documentclass[border=2px]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzset{
    green/.style  = {draw, rectangle, minimum width=1cm, minimum height=1cm, text centered, text width=1.2cm, font=\footnotesize, draw=black, fill=green!30},
    blue/.style   = {draw, rectangle, minimum width=6cm, minimum height=1cm, text centered, text width=5.0cm, font=\footnotesize, draw=black, fill=blue!30},
    yellow/.style = {draw, rectangle, minimum width=6cm, minimum height=1cm, text centered, text width=5.0cm, font=\footnotesize, draw=black, fill=yellow!30},
}

\begin{document}
\begin{tikzpicture}[node distance=1.1cm]
    \node (r1c1) [green] {VC++};
    \node (r1c2) [green, right of=r1c1, xshift=0.5cm] {VB.NET};
    \node (r1c3) [green, right of=r1c2, xshift=0.5cm] {C\#.NET};
    \node (r1c4) [green, right of=r1c3, xshift=0.5cm] {Others};
    \node (r2c1) [blue,  below of=r1c2, xshift=0.8cm] {COM-API Interface};
    \node (r3c1) [yellow, below of=r2c1] {Autodesk Inventor Application};

    \draw (r1c1);
    \draw (r1c2);
    \draw (r1c3);
    \draw (r1c4);
    \draw (r2c1);
    \draw (r3c1);
\end{tikzpicture}
\end{document}

下圖是產生的圖:

在此輸入影像描述

生成圖中的模組未正確對齊。請問有什麼建議嗎?

答案1

只是為了好玩,正如有人提議的那樣tcolorbox......

\documentclass{article}

\usepackage[most]{tcolorbox}

\tcbset{enhanced, fontupper=\bfseries, notitle, sharp corners, halign=center, valign=center}

\begin{document}
\begin{tcbitemize}[%
    raster equal height=rows, 
    raster columns=4,
    raster equal height,
    raster every box/.style={height=2cm},
    raster column skip=1mm,
    raster row skip=1mm,
    colback=green!70!black]
\tcbitem VC++
\tcbitem VB.NET
\tcbitem C\#.NET
\tcbitem Others
\tcbitem[colback=blue!40, raster multicolumn=4]
COM-API Interface
\tcbitem[colback=yellow!50, raster multicolumn=4]
Autodesk Inventor Application
\end{tcbitemize}
\end{document}

在此輸入影像描述

答案2

使用鏈的另一種解決方案:

\documentclass[border=2px]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, chains, calc}

\tikzset{
    green/.style  = {draw, rectangle, minimum width=2cm, minimum height=1cm, text centered, text width=1.2cm, font=\footnotesize, draw=black, fill=green!30},
    blue/.style   = {draw, rectangle, minimum width=8cm+3\pgflinewidth, minimum height=1cm, text centered, text width=5.0cm, font=\footnotesize, draw=black, fill=blue!30},
    yellow/.style = {draw, rectangle, minimum width=8cm+3\pgflinewidth, minimum height=1cm, text centered, text width=5.0cm, font=\footnotesize, draw=black, fill=yellow!30},
}

\begin{document}
\begin{tikzpicture}[start chain=1 going right,
  start chain=2 going below, node distance=1mm]
  \node [name=r1c1, on chain=1, green] {VC++};
  \node [name=r1c2, on chain=1, green] {VB.NET};
  \node [name=r1c3, on chain=1, green] {C\#.NET};
  \node [name=r1c4, on chain=1, green] {Others};
  \draw let \p1=($(r1c4.east)-(r1c1.west)$), \n1 = {veclen(\x1,\y1)} in
  node [name=r2c1, on chain=2, blue, anchor=north west, yshift=-1mm,
  minimum width=\n1-\pgflinewidth]
  at (r1c1.south west) {COM-API Interface};
  \draw let \p1=($(r1c4.east)-(r1c1.west)$), \n1 = {veclen(\x1,\y1)} in
  node [name=r3c1, on chain=2, yellow, minimum width=\n1-\pgflinewidth] {Autodesk Inventor Application};

\end{tikzpicture}
\end{document}

在此輸入影像描述

答案3

我在 TikZ 中的解決方案:使用給定的座標,因為您可以更好地控制它們。為了使用我的座標,我必須將中層和底層框的最小寬度調整為 8 公分。

編輯

根據評論中的要求,框之間應該有水平間隙。

因此我outer sep=1mm在樣式定義中添加了選項。外部 sep 適用於盒子的左側和右側尺寸。因此,第一個和最後一個框的對齊將被破壞。我透過相應地向第一個和最後一個框的 x 座標添加一些空格來糾正這個問題。請看看我的 MWE 中的評論。

給你:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzset{
    green/.style  = {draw, rectangle, 
      minimum width=1.6cm, minimum height=1cm, 
      %% NEW: added outer space
      outer sep=1mm,
      %% continued as before
      text centered, text width=1.2cm, font=\footnotesize,
      draw=black, fill=green!30}, 
    blue/.style   = {draw, rectangle,
      minimum width=8cm, minimum height=1cm,
      text centered, text width=5.0cm, font=\footnotesize,
      draw=black, fill=blue!30}, 
    yellow/.style = {draw, rectangle, 
      minimum width=8cm, minimum height=1cm, 
      text centered, text width=5.0cm, font=\footnotesize,
      draw=black, fill=yellow!30}, 
}

\begin{document}
\begin{tikzpicture}
  %% Define the nodes of the rectangles in the top layer.
  %% Corrected the x-position of the VC++ and Other box.
  \node at (-0.2,3) [green] {VC++} ;
  \node at (2,3) [green] {VB.net} ;
  \node at (4,3) [green] {C\#} ;
  \node at (6.2,3) [green] {Others} ;
  %% Node of the second/middle layer
  \node at (3,2) [blue]  {COM-API Interface} ;
  %% Node of the bottom layer
  \node at (3,1) [yellow] {Autodesk Inventor Applikation} ;
\end{tikzpicture}
\end{document}

和(也編輯過的)結果:

在此輸入影像描述

相關內容