tikz圖片中節點的對齊方式

tikz圖片中節點的對齊方式

我正在嘗試將下圖中的“Core 1”與其餘節點對齊:

在此輸入影像描述

然而,由於某種原因,Core 1 節點與其他節點相比似乎有所偏移,我不確定為什麼會發生這種情況。這是我的程式碼:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows, calc, chains, positioning, shapes}

\tikzset
{
core/.style = {
    rectangle,
    rounded corners,
    draw           = black, thick,
    text width     = 5em, 
    minimum height = 3em,
    align=center,
    on chain
            },
cache/.style = {
    core,
    draw           = gray,
            }
}% end of tikzset

\begin{document}

\begin{tikzpicture}[
node distance = 4mm, 
start chain = going below ]
\node [core, minimum width=4cm,midway]           (c1)  {Core 1};
\node [cache]  (l11) {L1 data\\  32 KB}; 
\node [cache,right=of l11]  (l12) {L1 \\  32 KB}; 
%shared l2 cache
\path   let \p1 = ($(l12.east)-(l11.west)$), 
        \n1 = {veclen(\x1,\y1)} in
    node [core,minimum width = \n1,
         below=of $(l11.south)!0.5!(l12.south)$]    (l13) {L2 \\ 256 KB};
\end{tikzpicture}
\end{document}

答案1

有很多方法可以解決這個問題,這就是其中之一。有一個問題是on chain關鍵。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}

\tikzset
{
core/.style = {
    rectangle,
    rounded corners,
    draw           = black, thick,
    text width     = 5em, 
    minimum height = 3em,
    align=center,
            },
cache/.style = {
    core,
    draw           = gray,
            }
}% end of tikzset

\begin{document}

\begin{tikzpicture}[node distance = 4mm]
\node [core, minimum width=4.5cm,midway]           (c1)  {Core 1};
\node [cache,below=of c1.south west,anchor=north west]  (l11) {L1 data\\  32 KB}; 
\node [cache,below=of c1.south east,anchor=north east]  (l12) {L1 \\  32 KB}; 
%shared l2 cache
\path   let \p1 = ($(l12.east)-(l11.west)$), 
        \n1 = {veclen(\x1,\y1)} in
    node [core,minimum width = \n1,
         below=of $(l11.south)!0.5!(l12.south)$]    (l13) {L2 \\ 256 KB};
\end{tikzpicture}
\end{document}

在此輸入影像描述

請注意,在這種情況下,您不需要計算下部節點的寬度,但我保留了計算,因為它可能在其他情況下變得相關。您也可以從中間層節點開始,並使用此寬度計算來建構其他層節點。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}

\tikzset
{
core/.style = {
    rectangle,
    rounded corners,
    draw           = black, thick,
    minimum width    = 5em, 
    minimum height = 3em,
    align=center,
            },
cache/.style = {
    core,
    draw           = gray,
            }
}% end of tikzset

\begin{document}

\begin{tikzpicture}[node distance = 4mm]
\node [cache]  (l11) {L1 data\\  32 KB}; 
\node [cache,right=of l11]  (l12) {L1 \\  32 KB}; 
%shared l2 cache
\path   let \p1 = ($(l12.east)-(l11.west)$), 
        \n1 = {veclen(\x1,\y1)} in
    node [core,minimum width = \n1,
         below=of $(l11.south)!0.5!(l12.south)$]    (c2) {L2 \\ 256 KB}
    node [core,minimum width = \n1,
         above=of $(l11.north)!0.5!(l12.north)$]    (c1) {Core 1}
         ;
\end{tikzpicture}
\end{document}

在此輸入影像描述

即使快取節點變得更寬,這也將繼續提供良好的結果。

答案2

其他答案的一個小變化:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[
  node distance = 4mm and 0mm,
box/.style args = {#1/#2}{draw=#1, minimum width=#2,
                          rounded corners=5, minimum height=10mm,
                          align=center},
box/.default    = gray/22mm
                        ]
\node (n1) [box=black/50mm]  {L2\\256 Kb};
\node (n21)[box, above right=of n1.north west] {L1 data\\32 Kb};
\node (n22)[box, above  left=of n1.north east] {L1 \\32 Kb};
\node (n3) [box=black/50mm, above=of n1 |- n21.north] {Core 1};
    \end{tikzpicture}
\end{document}

在此輸入影像描述

答案3

下面是我朋友的程式碼。他的程式碼簡短而簡單(我無法讓它變得更簡單)。我們應該以優雅簡單的方式編寫程式碼嗎?

在此輸入影像描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\path[every node/.style={draw,rounded corners=5,minimum height=10mm,align=center}]
(0,0) node[minimum width=5cm]{Core 1}
(-1.5,-1.5) node[minimum width=2cm,draw=gray]{L1 data\\32 Kb}
(1.5,-1.5) node[minimum width=2cm,draw=gray]{L1\\32 Kb}
(0,-3) node[minimum width=5cm]{L2\\256 Kb};
\end{tikzpicture}
\end{document}

相關內容