TikZ:使节点具有线宽

TikZ:使节点具有线宽

我想创建以下图片tikz

在此处输入图片描述

我已使 MWE 运行,但遇到以下问题

  • 我希望两个框跨越当前\linewidth
  • 我希望“内部”节点相对于框进行定位

有可能实现这个吗?


平均能量损失

\documentclass{standalone}
%
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usetikzlibrary{positioning}
% 
\begin{document}
% 
\pgfdeclarelayer{bg}    % declare background layer
\pgfsetlayers{bg,main}  % set the order of the layers (main is the standard layer)
%
\tikzset{
My Node Style/.style={
    rectangle
,rounded corners
,fill=gray
,minimum width=6em
,minimum height=5ex
}
}
% 
\tikzset{
My RectLabel Style/.style={
    draw=black
,fill=white
}
}
% 
\tikzset{
My Bg Style/.style={
    inner ysep=5pt
,inner ysep=20pt
,draw=black
,thick
,fill=lightgray
}
}
% 
\begin{tikzpicture}
\node (lib11) [My Node Style] at (0,0)            {Library 1-1};
\node (lib12) [My Node Style, right=1em of lib11] {Library 1-2};
\node (lib13) [My Node Style, right=1em of lib12] {Library 1-3};
\node (lib14) [My Node Style, right=1em of lib13] {Library 1-4};
%
\node (lib21) [My Node Style,above=15ex of lib11]  {Library 2-1};
\node (lib22) [My Node Style, right=1em of lib21] {Library 2-2};
\node (lib23) [My Node Style, right=1em of lib22] {Library 2-3};
\node (lib24) [My Node Style, right=1em of lib23] {Library 2-4};
\node (lib25) [My Node Style, right=1em of lib24] {Library 2-5};
%
\begin{pgfonlayer}{bg}    % select the background layer
    \node[fit={(lib11) (lib14)}, My Bg Style] (rect1) {};
    \node[My RectLabel Style] (rect1Label) at ($(rect1.north west)!0.5!(rect1.north east)$) {Layer 1};
\end{pgfonlayer}
%
\begin{pgfonlayer}{bg}    % select the background layer
    \node[fit={(lib21) (lib25)}, My Bg Style] (rect2) {};
    \node[My RectLabel Style] (rect2Label) at ($(rect2.north west)!0.5!(rect2.north east)$) {Layer 2};
\end{pgfonlayer}
%
\draw[->] (rect1Label.north) -- (rect2.south);
\end{tikzpicture}
% 
\end{document}

答案1

为什么不使用tcolorbox?Atcbraster会为我们做一些工作。

\documentclass{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\tcbset{
    myraster/.style={
        raster columns=#1, 
        raster equal height, 
        size=small, 
        rounded corners, 
        frame hidden, 
        colback=gray,
        halign=center,
    },
    mybox/.style={
        colframe=black,
        sharp corners,
        coltitle=black,
        fontupper=\small,
        enhanced,
        attach boxed title to top center={yshift*=-\tcboxedtitleheight/2},
        boxed title style={
            colback=white,
            colframe=black,
            sharp corners,
            remember as={title #1},
        },
        title={#1},
        remember as={#1},
    }
}

\begin{document}
\lipsum[1]
\begin{tcboxeditemize}%
[%inner boxes properties
    myraster=5
]{%outer box properties
    mybox={Layer 2}
}
\tcbitem Library 2-1
\tcbitem Library 2-2
\tcbitem Library 2-3
\tcbitem Library 2-4
\tcbitem Library 2-5
\end{tcboxeditemize}
\vspace*{.5cm}
\begin{tcboxeditemize}%
[%inner boxes properties
    myraster=3
]{%outer box properties
    mybox={Layer 1}
}
\tcbitem Library 1-1
\tcbitem Library 1-2
\tcbitem Library 1-3
\end{tcboxeditemize}

\tikz[remember picture, overlay] \draw[<-] (Layer 2)--(title Layer 1);
\end{document}

在此处输入图片描述

更新:

当光栅使用多条线时,您可以使用raster halign=center选项将最后一行内容居中。

将其添加到myraster/.style前面的代码中将得到:

在此处输入图片描述

答案2

您可以使用 使框跨越线宽minimum width。最简单的方法是均匀分布这些框是pos沿着从左到右贯穿整个页面的路径使用的。如果框的宽度不同,则此方法不起作用,但对于您的示例,此方法有效。编辑:简化了代码,非常感谢 Peter Grill!

\documentclass{article}
\usepackage[margin=1in]{geometry}
%
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usetikzlibrary{positioning}
% 
\begin{document}
%\setlength{\linewidth}{16cm}
% 
\pgfdeclarelayer{bg}    % declare background layer
\pgfsetlayers{bg,main}  % set the order of the layers (main is the standard layer)
%
\tikzset{
My Node Style/.style={
    rectangle
,rounded corners
,fill=gray
,minimum width=6em
,minimum height=5ex
}
}
% 
\tikzset{
My RectLabel Style/.style={
    draw=black
,fill=white
}
}
% 
\tikzset{
My Bg Style/.style={
    inner ysep=5pt,outer xsep=0pt,
,inner ysep=20pt
,draw=black
,thick
,fill=lightgray,minimum
    width=\linewidth-2*\pgflinewidth
}
}
% 
\begin{tikzpicture}
\path (2cm,0) -- (\linewidth-2cm,0)\foreach \X in {1,...,4}
{node (lib1\X) [My Node Style,pos=(\X-1)/3]             {Library 1-\X}};
\path (2cm,20ex) -- (\linewidth-2cm,20ex)\foreach \X in {1,...,5}
{node (lib2\X) [My Node Style,pos=(\X-1)/4]             {Library 2-\X}};
\begin{pgfonlayer}{bg}    % select the background layer
    \node[fit={(lib11) (lib14)}, My Bg Style] (rect1) {};
    \node[My RectLabel Style] (rect1Label) at ($(rect1.north west)!0.5!(rect1.north east)$) {Layer 1};
\end{pgfonlayer}
%
\begin{pgfonlayer}{bg}    % select the background layer
    \node[fit={(lib21) (lib25)}, My Bg Style] (rect2) {};
    \node[My RectLabel Style] (rect2Label) at ($(rect2.north west)!0.5!(rect2.north east)$) {Layer 2};
\end{pgfonlayer}
%
\draw[->] (rect1Label.north) -- (rect2.south);
\end{tikzpicture}
% 
\end{document}

在此处输入图片描述

相关内容