
Estou tentando alinhar o "Núcleo 1" na imagem a seguir com o restante dos nós:
No entanto, por algum motivo, o nó Core 1 parece estar deslocado em comparação com o restante dos nós, e não tenho certeza do motivo. Aqui está o meu código:
\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}
Responder1
Há muitas maneiras de corrigir isso, esta é uma delas. Um problema era a on chain
chave.
\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}
Observe que neste caso você não precisa calcular a largura do nó inferior, mas mantive o cálculo, pois pode se tornar relevante em outros casos. Você também pode começar com os nós da camada intermediária e construir os outros com este cálculo de largura.
\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}
Isto continuará a dar bons resultados mesmo que os nós de cache se tornem mais largos.
Responder2
Uma pequena variação de outras respostas:
\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}
Responder3
Abaixo está o código do meu amigo. O código dele é curto e simples (não tenho como simplificar). É certo que devemos codificar com simplicidade elegante?
\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}