如何根据另一个形状设置其大小

如何根据另一个形状设置其大小

我想根据fit环境产生的另一个形状的大小来设置形状的大小。我使用的 MWE 如下:

\documentclass{report}
\usepackage[english]{babel}

\usepackage{tikz}
\usetikzlibrary{positioning, fit}

\tikzset{
  node distance = 1.5cm,
  block/.style = {
    text width = 3cm,
    minimum height = 2cm,
    draw = blue!80,
    fill = blue!20
  },
  module/.style = {
    draw = black!50,
    fill = yellow!20,
    inner sep = 10pt
  }
}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\begin{tikzpicture} 
  \node[block]                (B1) {B1};
  \node[block, right = of B1] (B2) {B2};

  \begin{pgfonlayer}{background}
    \node[module, fit = (B1) (B2)] (F) { }; 
  \end{pgfonlayer}
\end{tikzpicture}

\end{document}

我想要的是在下面添加一个与形状大小完全相同的黄色形状F在此处输入图片描述

答案1

以下是两个可能的选择:

  1. 使用节点的角绘制一个矩形F,并将其稍微向下移动:

    \filldraw [draw=black!50,fill=yellow!20] ([yshift=-4cm]F.south west) rectangle ([yshift=-4cm]F.north east);
    
  2. 使用该库来计算操作calc的宽度/高度,并根据这些尺寸创建一个节点。Flet

    \draw let
      \p1=(F.south west),\p2=(F.north east),\n1={(\x2-\x1)},\n2={(\y2-\y1)} in
      node[module,minimum width=\n1,minimum height=\n2,below=6cm of F.south] (F2) {};
    
\documentclass{standalone}
\usepackage[english]{babel}

\usepackage{tikz}
\usetikzlibrary{positioning, fit, calc}

\tikzset{
  node distance = 1.5cm,
  block/.style = {
    text width = 3cm,
    minimum height = 2cm,
    draw = blue!80,
    fill = blue!20
  },
  module/.style = {
    draw = black!50,
    fill = yellow!20,
    inner sep = 10pt
  }
}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\begin{tikzpicture} 
  \node[block]                (B1) {B1};
  \node[block, right = of B1] (B2) {B2};

  \begin{pgfonlayer}{background}
    \node[module, fit = (B1) (B2)] (F) { }; 
  \end{pgfonlayer}

  \filldraw [draw=black!50,fill=yellow!20] ([yshift=-3cm]F.south west) rectangle ([yshift=-3cm]F.north east);

  \draw let
  \p1=(F.south west),\p2=(F.north east),\n1={(\x2-\x1)},\n2={(\y2-\y1)} in
  node[module,minimum width=\n1,minimum height=\n2,below=3.3cm of F.south] (F2) {};

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容