Flow Charts – Creating Flow Chart with Defined Box Width

flow chartssize;tikz-trees

I created a flowchart with tikz. While I am happy with the result it bothers me that I create the size of the boxes through manually applied linebreaks. Is there a way to tell the program to adjust the size of the boxes to the amount of text they contain? It would be ideal to define a maximum width for each box. Here is my example:

  \documentclass[border=10pt,
               tikz]{standalone}
\usetikzlibrary{trees}% <-- new
\usepackage[osf]{mathpazo}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

    \begin{document}
\begin{tikzpicture}[
every node/.style = {shape=rectangle, rounded corners,
                     draw, align=center, fill=white},  level distance=4cm,            
   level 1/.style = {sibling distance = 60em},% <-- new
   level 2/.style = {sibling distance = 24em},% <-- new
   level 3/.style = {sibling distance = 13em},% <-- new
   level 4/.style = {sibling distance = 11em},% <-- new
      level 5/.style = {sibling distance = 21em},% <-- new
                    ]
\node {Aristoteles’ Einteilung \\menschlichen Handelns\\ (Rhetorik, 1368b 33)}
    [edge from parent fork down]
 child {node{was man nicht\\ von sich aus tut}
     child {node{Aus Zufall}
    child {node{Zufall und Schicksalsfügung\\ gehören zu den Ursachen\\ Physik, 195b4\\ Zufall im Bereich menschlichen \\Handelns im Ggs. zum Vorsatz.\\Bewegursache }child{node {Unbestimmte,\\ akzidentelle Ursachen}}
   }}child{node {Aus Notwendigkeit}child{node {durch Gewalt (Zwang?)}}child{node {von Natur aus}child{node {Ursache in den Dingen selbst}child{node {Weder freiwillig noch unfreiwillige \textit{Vorgänge}\\z.\,B. Altern und Sterben}}child{node {Nichtfreiwillige Bewegungen\\z.\,B. Schlaf, Erwachen Atmung}}}}}}
    child{node {was man von\\ sich aus tut}child{node {Tun aus Gewohnheit}child{node {Kein Habitus \\im Sinne der Tugend}child{node {Problem: ist ein Tun aus\\ Gewohnheit freiwillig\\ im eigentlichen Sinne? }}}}child{node {aus Streben}child{node {vernünftiges Streben}child{node {Wollen\\ d.\,i. Streben nach Gutem}}}child{node {vernunftloses Streben}child{node {Zorn und Begierde}}}}}
   ;
\end{tikzpicture}
    \end{document}

Best Answer

This shows two potential options for making a node where text width is set relative to the length of the text in the node.

enter image description here

\documentclass[border=5mm,tikz]{standalone}
\newcommand\foo[1]{node [text width={0.7*width("#1")}] {#1}}
\begin{document}
\begin{tikzpicture}[
   foo/.style={
       draw,
       % measure the width of the argument, and set text width to 60% of this
       text width={0.6*width("#1")},
       % use node contents to define the text of the node
       node contents={#1}
       }
]

\node[foo={this is some long text, with comma},at={(0,1)}];

\path \foo{This is another long text, also with a comma};
\end{tikzpicture}
\end{document}

And the macro version implemented for your tree. Note that I set the text width to whichever is larger of 30% the text length or 3cm, otherwise it wouldn't work well for shorter nodes. Note also that this method did not work for the node that had \textit in it, so for that one the text width is set manually.

enter image description here

\documentclass[border=10pt,
               tikz]{standalone}
\usetikzlibrary{trees}% <-- new
\usepackage[osf]{mathpazo}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\newcommand\makenode[1]{node [text width={max(0.3*width("#1"),3cm)}] {#1}}
\begin{document}
\begin{tikzpicture}[
   every node/.style = {shape=rectangle, rounded corners,
                        draw, align=center, fill=white},  level distance=4cm,            
   level 1/.style = {sibling distance = 50em},% <-- new
   level 2/.style = {sibling distance = 24em},% <-- new
   level 3/.style = {sibling distance = 13em},% <-- new
   level 4/.style = {sibling distance = 11em},% <-- new
   level 5/.style = {sibling distance = 21em},% <-- new
                    ]
\path
\makenode{Aristoteles’ Einteilung menschlichen Handelns (Rhetorik, 1368b 33)}
    [edge from parent fork down]
  child { 
    \makenode{was man nicht von sich aus tut}
      child { \makenode{Aus Zufall}
        child { \makenode{Zufall und Schicksalsfügung
                     gehören zu den Ursachen
                     Physik, 195b4
                     Zufall im Bereich menschlichen
                     Handelns im Ggs. zum Vorsatz.
                     Bewegursache}
          child { \makenode{Unbestimmte, akzidentelle Ursachen} }
              }
            } 
    child{ \makenode{Aus Notwendigkeit}
      child{ \makenode{durch Gewalt (Zwang?)}}
      child{ \makenode{von Natur aus}
        child{ \makenode{Ursache in den Dingen selbst}
          child{ node[text width=3.5cm]{Weder freiwillig noch unfreiwillige \textit{Vorgänge}
                      z.\,B. Altern und Sterben} % doesn't work with \textit
                }
          child{ \makenode{Nichtfreiwillige Bewegungen z.\,B. Schlaf, Erwachen Atmung}}
             }
           }
         }
       }
  child{ \makenode{was man von sich aus tut}
    child{ \makenode{Tun aus Gewohnheit}
      child{ \makenode{Kein Habitus im Sinne der Tugend}
        child{ \makenode{Problem: ist ein Tun aus
                           Gewohnheit freiwillig
                           im eigentlichen Sinne?}
             }
           }
         }
    child{ \makenode{aus Streben}
      child{ \makenode{vernünftiges Streben}
        child{ \makenode{Wollen d.\,i. Streben nach Gutem}}
           }
      child{ \makenode{vernunftloses Streben}
        child{\makenode{Zorn und Begierde}}
           }
         }
      };
\end{tikzpicture}
\end{document}
Related Question