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.

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.

share|improve this question
    
You can use align=left –  Marco Daniel Mar 2 '12 at 9:24

1 Answer 1

up vote 15 down vote accepted

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}
share|improve this answer
1  
Thanks. It works. But isn't there a command opposite to minimum width in tikz? –  Harish Kumar Mar 2 '12 at 23:12
    
@HarishKumar: I didn’t found something like that in pgfmanual.pdf –  Tobi Mar 3 '12 at 0:04
    
May be somebody knowing these things well can dig into the definition of minimum width and change it appropriately for maximum width? –  Harish Kumar Mar 3 '12 at 0:09
    
@HarishKumar: I guess it’s not yet implemented because minimum width only requires to make a box of the given width but maximum width needs an additional calculation to check wether the text is longer than the maximum or not … but I know TikZ not well enough to look at the source and see what’s the thing … –  Tobi Mar 3 '12 at 0:28

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.