tikz图片中节点的对齐

tikz图片中节点的对齐

我试图将下图中的“核心 1”与其余节点对齐:

在此处输入图片描述

但是,出于某种原因,核心 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}

相关内容