[Tex/LaTex] Can we define maximum width for a node

tikz-pgftikz-styles

My question is almost opposite to: this question.

I have defined a block:

\begin{tikzpicture}[node distance = 2cm, auto,->=stealth,point/.style= 
                   {circle,fill=red,minimum size=0pt,inner sep=0pt}]
\tikzstyle{block} = [rectangle, draw,thick,fill=blue!0,
    text centered, rounded corners, minimum height=1em]
\node [block] (start) {Start};
\node [block,below of=start] (start) {Start and blah blah blah blah blah blah
                       blah blah blah blah blah blah blah blah blah
                       blah blah blah blah blah blah blah blah blah
                       blah blah blah blah blah blah blah blah blah};
\end{tikzpicture}

The block adjusts itself to accommodate text.

enter image description here

But I would like to control the width of the block by using text width=15em:

\tikzstyle{block} = [rectangle, draw,thick,fill=blue!0,text width=20em,<---------= 
             text centered, rounded corners, minimum height=1em]

I get this:

enter image description here

Though the second block looks good, the first block (start) has lot of empty space in it. My question is

How to adjust the width of the block according to the text and at the same time limiting the maximum width of the block to (say) 15em?

In other words, How to make the first block to fit with start? Or generally speaking,

How to adjust the block size automatically to fit the text with maximum width of block as 15em?

Pl. note: I am aware that I can define block2 with a suitable width and put start in it. But my constraint is to use the single tikzstyle.

Best Answer

I guess there is no TikZ way, but you can use the varwidth package:

\documentclass{article}

\usepackage{tikz}
\usepackage{varwidth}

\begin{document}
\begin{tikzpicture}[node distance = 2cm, auto,->=stealth,point/.style= 
                   {circle,fill=red,minimum size=0pt,inner sep=0pt}]
\tikzstyle{block} = [rectangle, draw,thick,fill=blue!0,
    text centered, rounded corners, minimum height=1em]
\node [block] (start) {\begin{varwidth}{15em}Start\end{varwidth}};
\node [block,below of=start] (start) {%
   \begin{varwidth}{15em}
      Start and blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
    \end{varwidth}};
\end{tikzpicture}
\end{document}

result

You may wrap the {varwidth} environment in a shorter macro …

Update

It is possible to use execute at begin/end node to include {varwidth} in the sytle definition:

\begin{tikzpicture}[node distance = 2cm, auto,->=stealth]
\tikzstyle{block} = [%
   rectangle, draw,thick,fill=blue!0,
   text centered, rounded corners, minimum height=1em,
   execute at begin node={\begin{varwidth}{15em}},
   execute at end node={\end{varwidth}}]
\node [block] (start) {%
      Start
};
\node [block,below of=start] (start) {%
      Start and blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
};
\end{tikzpicture}
Related Question