
Estoy intentando alinear el "Núcleo 1" en la siguiente imagen con el resto de los nodos:
Sin embargo, por alguna razón, el nodo Core 1 parece estar desplazado en comparación con el resto de los nodos, y no estoy seguro de por qué es así. Aquí está mi 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}
Respuesta1
Hay muchas formas de solucionar este problema, esta es una. Un problema fue la on chain
clave.
\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}
Tenga en cuenta que en este caso no es necesario calcular el ancho del nodo inferior, pero mantuve el cálculo ya que puede resultar relevante en otros casos. También puede comenzar con los nodos de la capa intermedia y construir los demás con este cálculo de ancho.
\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}
Esto seguirá dando buenos resultados incluso si los nodos de caché se vuelven más anchos.
Respuesta2
Una pequeña variación de otras respuestas:
\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}
Respuesta3
A continuación se muestra el código de mi amigo. Su código es corto y sencillo (no puedo hacerlo más sencillo). ¿Es correcto que codifiquemos con elegante simplicidad?
\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}