TikZ Fit – How to Fit Only the Height on a Node

fittikz-pgf

I've been trying to get the fit key in TikZ to scale a node so that it matches only the height of it's fit nodes but I haven't been successful. I'm trying to get the red box to be the size of the blue boxes:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}

\usetikzlibrary{positioning,fit}
\newcommand{\filler}[1]{\begin{minipage}{12cm}\small\lipsum[#1]\end{minipage}}
\thispagestyle{empty}

\begin{document}

\begin{tikzpicture}[node distance=0pt,outer sep = 2pt, text centered]
\tikzstyle{box}=[rectangle,draw,inner sep=0em]

\tikzstyle{vline}=[box, rotate=90, fill=red!20, minimum height=1cm]
\tikzstyle{hline}=[box, fill=blue!20, minimum width=10cm]

\node[vline] (a) at (0,0) {\textbf{How do I extend this node to fit?}};
\node[hline] (b1) [below right = of a.south east] {\filler{1}};
\node[hline] (b2) [below = of b1] {\filler{2}};
\node[hline] (b3) [below = of b2] {\filler{3}};
\end{tikzpicture}

\end{document}

Question: for node a, how do I keep the x dimension fixed, while the y dimension scales to the height of nodes (b1,b2,b3)?

Best Answer

enter image description here

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usepackage{lipsum}

\usetikzlibrary{positioning,fit}
\newcommand{\filler}[1]{\small\lipsum[#1]}
\thispagestyle{empty}

\begin{document}

\begin{tikzpicture}[node distance=2pt,align = justify]
\tikzset{
box/.style={rectangle,draw,inner sep=0em},
vline/.style={box, fill=red!20, align=center},
hline/.style={box, fill=blue!20, text width=12cm}
}

\node[hline] (b1) {\filler{1}};
\node[hline] (b2) [below = of b1] {\filler{2}};
\node[hline] (b3) [below = of b2] {\filler{3}};

\coordinate [left=12pt of b1.north west] (top);
\coordinate [left=12pt of b3.south west] (bottom);

\node[vline, minimum width=10pt,fit=(top)(bottom), 
    label={[rotate=90]center:\textbf{How do I extend this node to fit?}}] (a) {};
\end{tikzpicture}

\end{document}

Summary

  • I removed minipage environment from your \filler macro and replaced text centered with text justified in your tikzpicture options as this does the same effect.
  • I replaced node distance=0pt with node distance=2pt.
  • Two coordinates: top and bottom were defined so for use with the fit option for the node a. Now, instead of positioning b1 in terms of node a, node a was positioned in terms of nodes b1 and b3 using fit library. Update: It is enough to use two coordinates for the fit as the node (a) will simply create a bounding box around the two coordinates and any coordinate defined between these two points. As long as the coordinates are aligned, there is no harm in doing this as coordinates are theoretically dimensionless. And, for the 10pt width of the node (a), I thought that was what you were after; if not, you can change it to a dimension that you like :)
  • The work-around for rotating the text of node a is done with the label option. Update: The work-around is needed, since with this solution, doing rotate=90 directly in the node option will rotate both the node shape and the text.

Update

  • I have replaced the old \tikzstyle with \tikzset as in Gonzalo's answer.