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}

관련 정보