Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to set shape's size basing on the size of another shape produced by the fit environment. The MWE I use is the following:

\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}

What I want is to add one more yellow shape below with the exact size of shape F: enter image description here

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Here are two possible options:

  1. Draw a rectangle using the corners of the F node, shifting it down a bit:

    \filldraw [draw=black!50,fill=yellow!20] ([yshift=-4cm]F.south west) rectangle ([yshift=-4cm]F.north east);
    
  2. Use the calc library to calculate the width/height of F with the let operation, and create a node with these dimensions.

    \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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.