33

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.

  • You can use align=left – Marco Daniel Mar 2 '12 at 9:24
26

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}
  • 1
    Thanks. It works. But isn't there a command opposite to minimum width in tikz? – user11232 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? – user11232 Mar 3 '12 at 0:09
  • 1
    @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
3

I just wanted to augment Tobi's answer regarding execute at begin/end node, and put that part into an own style with an argument for the width.

\tikzset{
    max width/.style args={#1}{
        execute at begin node={\begin{varwidth}{#1}},
        execute at end node={\end{varwidth}}
    }
}
\begin{tikzpicture}
    \node[max width=3cm] {this node is maximum 3cm wide although its text is wider};
    \node[max width=10cm] {shrinks to textwidth};
\end{tikzpicture}

Dont forget to load the varwidth package beforehand: \usepackage{varwidth}

  • 2
    I assume it should be \usepackage{varwidth} at the end? – Joel Mar 21 '18 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy