0
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% %
%                          Begin   Header                                  

    %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
\documentclass[a4paper,11pt,tikz]{article}    
\usepackage[margin=0.5cm]{geometry}
\usepackage{tikz-qtree}
% bestimmt das Aussehen eines Dokuments

\usepackage[ngerman]{babel}         % Neue deutsche Rechtschreibung
\usepackage[T1]{fontenc}            % Bessere Schriftdarstellung
\usepackage{lmodern}                % Aktuelle Schrift

\usepackage[intlimits]{amsmath}     % Zusaetzliche Matheumgebungen
\usepackage{amssymb}                % Mathematische Symbole
\usepackage{graphicx}               % notwendig fuer \includegraphics
\usepackage{fancyhdr}               % Kopf- und Fusszeile
\usepackage{lastpage}               % erzeugt Referez zu der letzten Seite
\usepackage{moreverb}               % verbatimtab Umgeung
%\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,shapes,decorations.text,decorations,calc,positioning,automata,matrix,arrows}


% Seiteneinstelungen
\setlength\textwidth{165mm}           % Breite
\setlength\textheight{235mm}          % Hoehe
\setlength\headheight{41pt}           % Hoehe der Kopfzeile
\setlength\topmargin{-12mm}           % Abstand oben
\setlength\oddsidemargin{-20mm}         % Linker Rand
\setlength\parindent{0pt}             % und ohne Einrueckung
\setlength\parskip{1.7\medskipamount} % Absaetze abgesetzt
\sloppy\pagestyle{fancy}

%Kopf- und Fusszeileeinstellungen
\renewcommand{\headrulewidth}{0.4pt}    %obere Trennlinie
\fancyfoot[C]{Seite:~\thepage~von~\pageref{LastPage}} %Seitennummer
\renewcommand{\footrulewidth}{0.4pt}    %untere Trennlinie

% Ein paar hilfreiche Zeichen
\newcommand{\R}{\mathbb{R}}                 % reelle Zahlen
\newcommand{\N}{\mathbb{N}}                 % natuerliche Zahlen
\newcommand{\e}{\text{e}}                   % eulersche Zahl
\newcommand{\E}[1]{\cdot10^{#1}}            % x 10^{...}
\newcommand{\qed}{\hspace*{\fill}q.e.d.}    % Beweis fertig
\newcommand{\ON}[1]{{\cal O}(#1)}       %O-Notation
%\newcommand{\Aufgabe}[1]{{\vskip5mm\bf Aufgabe #1.\\}}

\def\vblatt{~}
\def\vtermin{~}
\def\vbriefkasten{~}

%Blattnummer, Abgabedatum und Namen mir Matrikelnummern in der Kopfzeile anpassen
\newcommand{\dreinamen}[6]{\fancyhead[R]{#1 (#2)\\#3 (#4)\\#5 (#6)}} %Kopfzeile rechts
\fancyhead[C]{\large{\bf{Blatt \vblatt }}}
\fancyhead[L]{\textbf{Datenstrukturen und Algorithmen}\\ Sommersemester 2017 \\ \"Ubungsgruppe: \vtermin%\\Briefkasten: \vbriefkasten 
}

\newcommand{\blatt}[1]{\def\vblatt{#1}}
\newcommand{\termin}[1]{\def\vtermin{#1}}
\newcommand{\briefkasten}[1]{\def\vbriefkasten{#1}}

%!!!!Neu Einfaches einbinden von Quelltexten
\usepackage{listings}

%Algorithmennotation als Quelltext verwenden.
\lstdefinelanguage{alglang}
{keywords={if, then, endif, else, repeat, until, endrepeat, while, do, endwhile, var, module, endmodule, mod, return, pre, post, reads, changes, mem, Type, Functions, Preconditions, Axioms},
emph={Integer, Bool, Real, Char, String},
sensitive=false,
morecomment=[l]{--},
morecomment=[s]{\{}{\}},
morestring=[d]"  
}
\lstdefinestyle{algstyle}{
  mathescape=true, 
  basicstyle=\normalfont\sffamily, 
  commentstyle=\normalfont\sffamily, 
  keywordstyle=\sffamily\bfseries,
  identifierstyle=\sffamily\itshape, 
  emphstyle=\normalfont\sffamily, 
  stringstyle=,
  showstringspaces=false,        
  columns=[l]fullflexible     
}

\newcommand{\lstjava}{\lstset { numbers=left,language=java,tabsize=2,numberstyle=\tiny ,numbersep=5pt,basicstyle=\scriptsize}}
\newcommand{\lstalg}{\lstset { numbers=left,language=alglang,tabsize=2,numberstyle=\tiny,style=algstyle}}

\newcounter{aufgabe}
\setcounter{aufgabe}{0}

\newcommand{\Aufgabe}{\noindent\newline\addtocounter{aufgabe}{1}\textbf{Aufgabe \vblatt.\theaufgabe}\\
%\input{\theaufgabe}\bigskip
}

% Unteraufgaben (mit Enumeration)
\def\labelenumi{(\arabic{enumi})}
\parindent0mm % keine Absatzeinrückung

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
%                           Ende Header                                        %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %

%!!!!anpassen an das Betriebssystem!!!, um Umlaute zu verwenden
\usepackage[utf8]{inputenc}                      %Linux
%\usepackage[latin1]{inputenc}                    %Windows
%\usepackage[applemac]{inputenc}                  %Mac


%Namen und Matrikelnummern anpassen
\dreinamen{aaa}{1111111}{bbb}{2222222}{zzz}{3333333} %3er Gruppe


%Termin der Uebungsgruppe und Raum anpassen z. B. \termin{Mo 10:15-11:45, BB 915}
\termin{Do 10:00-12:00}

%Blattnummer anpassen z. B. \blatt{1}
\blatt{2}
\usetikzlibrary{arrows}
\tikzset{
  every node/.style={minimum size=4mm, inner sep=0.5mm, font=\sffamily},
  level 1/.style={every child/.style=,
    sibling distance=10em},
  level 2/.style={every child/.style=,
    sibling distance=10em},
  level 3/.style={every child/.style=,
    sibling distance=3em},
  level 4/.style={every child/.style=,
    sibling distance=5em},
}
\begin{document}
%Hier kommt der Text des Dokuments......

\lstjava %Setzen des Aussehens des Listings für Java
%\lstinputlisting{Sort.java}
\Aufgabe
\begin{center}
\resizebox{1.5\textwidth}{!}{
  \begin{tikzpicture}[<-,>=stealth']
    \node {algorithm}
      child { 
        node {declaration}
        child[fill=none]{ edge from parent[draw=none] }
        child { node {var} }
        child { 
            node {declarationlist} 
            child[fill=none]{ edge from parent[draw=none] }
            child { 
                node {namelist}
                child[fill=none]{ edge from parent[draw=none] }
                child {
                    node {name}
                    child {
                        node {letter}
                        child {node{a}}
                    }
                }
                child {
                    node {'}
                }
                child[fill=none] {edge from parent[draw=none]}
                child {
                    node {namelist}
                    child[fill=none]{ 
                        edge from parent[draw=none] }
                        child {
                            node {name}
                            child {
                                node {letter}
                                child {
                                    node{c}
                                }
                            }
                        }   
                } 
            }
            child { node {:} }
            child { 
                node {type} 
                child {node{integer}}
            }
        }
      }
      child {
        node {;}
        child[fill=none]{ edge from parent[draw=none] }
      }
      child { 
        node {statement}
        child {
          node {assignment}
          child { node {name}
            child {node {letter}
                child {node {a}
                }
            }
          }
          child[fill=none] {edge from parent[draw=none]}
          child { node {:=}}
          child[fill=none] {edge from parent[draw=none]}
          child { node {expression}
            child {node {arithmetic expr.}
                child {node {operand}
                    child {node {signed number}
                        child {node {number}
                            child {node {1}
                            }
                        }
                    }
                }
            }
          }
          child[fill=none] {edge from parent[draw=none]}
        }
      }
      child {
        node {;}
        child[fill=none]{ edge from parent[draw=none] }
      }
      child { 
            node {statement}
            child {
                node {iteration}
                child {
                node {repeatloop}
            child[fill=none]{ edge from parent[draw=none] }
                child {node {repeat}}
                child[fill=none] {edge from parent[draw=none]}
                child {
                    node {statement}
                    child {
                        node {assignment}
                        child {
                            node{name}
                            child {
                                node{letter}
                                child {node{c}}
                            }
                        }
                        child {node{:=}}
                        child {
                            node{expession}
                            child {
                                node{arithmetic expr.}
                                child {
                                    node{arithmetic expr.}
                                    child {
                                        node{operand}
                                        child {
                                            node{name}
                                            child {
                                                node {letter}
                                                child {
                                                    node{c}
                                                }
                                            }
                                        }
                                    }
                                }
                                child {
                                    node{arithmetic op.}
                                    child {
                                        node{-}
                                    }
                                }
                                child {    
                                    node{arithmetic expr.}
                                    child {
                                        node{operand}
                                        child {
                                            node{name}
                                            child {
                                                node {letter}
                                                child {
                                                    node{a}
                                                }
                                            }
                                        }
                                    }
                                }
                        }
                    }
                }  
            }
            child { node {until} }
            child { 
                node {boolean expr.} 
                child {
                    node{arithmetic expr.}
                    child {
                    node {operand}
                        child {
                        node {name}
                            child {
                            node {letter}
                                child {
                                node {c}
                                }
                            }
                        }
                    }
                }
                child {
                    node{comparison}
                    child {
                        node {<}
                    }
                }
                child {
                    node{arithmetic expr.}
                    child {
                    node {operand}
                        child {
                        node {name}
                            child {
                            node {letter}
                                child {
                                node {a}
                                }
                            }
                        }
                    }
                }
            }
            }
            }
        }
      ;

      \end{tikzpicture}
      }

\end{center}


\end{document}

I've tried to move the margin value at the beginning of the code, but the syntax tree position doesn't changed. Just the header and footer. Also, Any idea how to make it not overlapping each other? I also already adjust the sibling distance but it always like that. Any help would be appreciated ;) enter image description here

5
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. And thank you for posting a working example! At the same time, much of your code does not seem to be relevant to the question you're asking here. Please limit the example to only the code required for your issue to appear. You can have a look at this guide for how to prune your code for this purpose. May 2 '17 at 13:35
  • When you make the text area wider, you make \textwidth larger. so \resizebox{1.5\textwidth} will never work. May 2 '17 at 20:29
  • Forest will automatically avoid overlapping (usually) and draws more compact trees. It would be easier to use that package for something like this. Resizing is generally considered evil, but resizing to be 150% of the available space doesn't even make sense. Not sure what you are trying to do.
    – cfr
    May 7 '17 at 22:45
  • That code is rather more maximal than minimal, isn't it? Why all the irrelevant stuff?
    – cfr
    May 7 '17 at 22:46
  • You should not set page dimensions manually if using geometry. How can geometry do its job if you sneak behind its back and change everything when it isn't looking?
    – cfr
    May 7 '17 at 22:49
2

Forest automatically avoids overlaps and produces more compact trees by default. Moreover, it can handle alignment to tiers straightforwardly.

I present two solutions. The first uses a figure and the second a sidewaysfigure. Neither requires scaling or, even, a smaller font, to fit in the width and height of the text block.

I've also minimised your preamble for the example and corrected some of it in light of better practices. This includes using geometry rather than lying to it, loading parskip and using LaTeX macros to create new commands where possible, which is much safer than \def etc. I've replaced long obsolete font commands, removed misleading curly brackets and replaced the use of \newline and \\ outside special contexts. And some more general tidying up I've forgotten.

However, since I couldn't read your tree when typeset in all cases, I may have inadvertently mangled a few bits in converting it.

\documentclass[a4paper,11pt]{article}
\usepackage{geometry}
\usepackage{forest}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{rotating}
\usepackage{parskip}% don't just set parskip to 0 - see the style file for why

% either use geometry (recommended) or don't - it is mean to load it and then tell it lies
\geometry{left=5mm,right=40mm,headheight=41pt,textheight=235mm,top=37mm}
\setlength\parskip{1.7\medskipamount}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0.4pt}
\fancyfoot[C]{Seite:~\thepage~von~\pageref{LastPage}}
\renewcommand{\footrulewidth}{0.4pt}
% \newcommand is much safer than \def
\newcommand*\vblatt{~}
\newcommand*\vtermin{~}
\newcommand*\vbriefkasten{~}
\newcommand{\dreinamen}[6]{\fancyhead[R]{#1 (#2)\\#3 (#4)\\#5 (#6)}}
% \bf has been obsolete for use in LaTeX for 20+ years
% \large does not take an argument - it is a switch
\fancyhead[C]{\textbf{\large Blatt \vblatt }}
\fancyhead[L]{\textbf{Datenstrukturen und Algorithmen}\\ Sommersemester 2017 \\ \"Ubungsgruppe: \vtermin}
\newcommand{\blatt}[1]{\renewcommand*\vblatt{#1}}
\newcommand{\termin}[1]{\renewcommand*\vtermin{#1}}
\newcommand{\briefkasten}[1]{\renewcommand*\vbriefkasten{#1}}
\dreinamen{aaa}{1111111}{bbb}{2222222}{zzz}{3333333}
\termin{Do 10:00-12:00}
\blatt{2}

\newcounter{aufgabe}
\setcounter{aufgabe}{0}
\newcommand{\Aufgabe}{% \newline and \\ should not be used to break lines outside special contexts
  \noindent\stepcounter{aufgabe}\textbf{Aufgabe \vblatt.\theaufgabe}\par\noindent
}

\usetikzlibrary{arrows.meta}

\begin{document}

\begin{figure}
  \Aufgabe

  \centering
  \begin{forest}
    for tree={
      parent anchor=children,
      child anchor=parent,
      edge+={Stealth-},
      font=\sffamily
    },
    where={%
      >Ow+PO_>&{n children}{isodd(#1)}{n children}{1}%
    }{%
      calign=child edge,
      calign child/.process={Ow+n{n children}{(#1+1)/2}},
    }{},
    [algorithm
      [declaration
        [var]
        [declarationlist
          [namelist
            [name
              [letter
                [a]
              ]
            ]
            [']
            [namelist
              [name
                [letter
                  [c]
                ]
              ]
            ]
          ]
          [type
            [integer, tier=B]
          ]
        ]
      ]
      [;]
      [statement
        [assignment, tier=B
          [name
            [letter
              [a]
            ]
          ]
          [{:=}]
          [expression
            [arithmetic expr.
              [operand
                [signed number
                  [number
                    [1, tier=A]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
      [;]
      [statement
        [iteration, tier=B
          [repeatloop, tier=A
            [repeat]
            [statement
              [assignment
                [name
                  [letter
                    [c, tier=D]
                  ]
                ]
                [{:=}]
                [expession, calign with current edge
                  [arithmetic expr., tier=D
                    [arithmetic expr.
                      [operand
                        [name
                          [letter
                            [c]
                          ]
                        ]
                      ]
                    ]
                    [arithmetic op.
                      [-]
                    ]
                    [arithmetic expr., calign with current edge
                      [operand
                        [name
                          [letter
                            [a]
                          ]
                        ]
                      ]
                    ]
                  ]
                ]
              ]
            ]
            [until ]
            [boolean expr., for children={align=center}
              [arithmetic\\expr.
                [operand
                  [name
                    [letter
                      [c]
                    ]
                  ]
                ]
              ]
              [comparison
                [<]
              ]
              [arithmetic\\expr.
                [operand
                  [name
                    [letter
                      [a]
                    ]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
  \end{forest}
\end{figure}

\begin{sidewaysfigure}
  \centering
  \begin{forest}
    for tree={
      parent anchor=children,
      child anchor=parent,
      edge+={Stealth-},
      font=\sffamily
    },
    tikz+={
      \node [anchor=north west] at (current bounding box.north west) {\Aufgabe};
    },
    where={%
      >Ow+PO_>&{n children}{isodd(#1)}{n children}{1}%
    }{%
      calign=child edge,
      calign child/.process={Ow+n{n children}{(#1+1)/2}},
    }{},
    [algorithm
      [declaration
        [var]
        [declarationlist
          [namelist
            [name
              [letter
                [a]
              ]
            ]
            [']
            [namelist
              [name
                [letter
                  [c]
                ]
              ]
            ]
          ]
          [type
            [integer]
          ]
        ]
      ]
      [;]
      [statement
        [assignment
          [name
            [letter
              [a]
            ]
          ]
          [{:=}]
          [expression
            [arithmetic expr.
              [operand
                [signed number
                  [number
                    [1, tier=A]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
      [;]
      [statement
        [iteration
          [repeatloop
            [repeat]
            [statement
              [assignment, tier=A
                [name
                  [letter
                    [c]
                  ]
                ]
                [{:=}]
                [expession, calign with current
                  [arithmetic expr.
                    [arithmetic expr.
                      [operand
                        [name
                          [letter
                            [c]
                          ]
                        ]
                      ]
                    ]
                    [arithmetic op.
                      [-]
                    ]
                    [arithmetic expr.
                      [operand
                        [name
                          [letter
                            [a]
                          ]
                        ]
                      ]
                    ]
                  ]
                ]
              ]
            ]
            [, phantom]
            [until ]
            [boolean expr.
              [arithmetic expr.
                [operand
                  [name
                    [letter
                      [c]
                    ]
                  ]
                ]
              ]
              [comparison
                [<]
              ]
              [arithmetic expr.
                [operand
                  [name
                    [letter
                      [a]
                    ]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
  \end{forest}
\end{sidewaysfigure}
\end{document}

two trees

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.