# Fit only the height on a node

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:

\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)?

-
I am not sure I understand exactly what you want to fit? – Peter Grill Mar 29 '13 at 21:47
If you want to have the box hug the text in node a, you can write minimum height=0pt – hpesoj626 Mar 29 '13 at 21:53
@PeterGrill I've updated the picture to indicate what I'm trying to do, is it clear? – Hooked Mar 29 '13 at 21:54

\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.
-
Thanks for the tips, they are greatly appreciated! A few questions about your answer if you don't mind: 1] Why is a work-around for rotating text needed? 2] Was the 12pt padding just an aesthetic choice? 3] How does fit work in this case when you only give two coordinates... which way does it grow? – Hooked Mar 30 '13 at 1:53
@Hooked See my update. – hpesoj626 Mar 30 '13 at 6:48

Here's another option without using the fit library or additional coordinates. I used the let syntax to calculate the necessary length for the node; I also changed the old \tikzstyle syntax for the newer \tikzset:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\usepackage{lipsum}

\newcommand{\filler}[1]{{\small\lipsum[#1]}}

\begin{document}

\begin{tikzpicture}[node distance=2pt,align=justify]
\tikzset{
box/.style={rectangle,draw},
vline/.style={box, fill=red!20,align=center,minimum height=1cm,rotate=90},
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}};

\path let \p1 = (b1.north), \p2 = (b3.south) in
node[vline,inner xsep=0pt,text width=\y1-\y2-\pgflinewidth,anchor=south east]
at ([xshift=-2pt]b1.north west) {some text here};
\end{tikzpicture}

\end{document}


-

Another solution. It also uses fit tikzlibrary but there's no need for additional coordinates or calc tikzlibrary.

\node[vline, minimum width=1cm,
fit=(b1.north)(b3.south),
label={[rotate=90]center:\textbf{How do I extend this node to fit?}},
left=of b1.north west, anchor=north east] (a) {};


declares a node which height includes b1.north and b3.south and is placed with its top right corner (anchor=north east) at node distance from b1.north west. It's important to declare anchor after left=of because this last option fixes anchor to east.

\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=(b1.north)(b3.south),
label={[rotate=90]center:\textbf{How do I extend this node to fit?}},
left=of b1.north west, anchor=north east, minimum width=1cm ] (a) {};
\end{tikzpicture}

\end{document}


-