% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% %
% 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}
我尝试移动代码开头的边距值,但语法树位置没有改变。只有页眉和页脚。另外,有什么想法可以让它们不相互重叠吗?我也已经调整了兄弟距离,但总是这样。任何帮助都将不胜感激 ;)
答案1
Forest 默认自动避免重叠并生成更紧凑的树。此外,它可以tier
直接处理 s 的对齐。
我提出两种解决方案。第一种使用 a figure
,第二种使用 a sidewaysfigure
。两者都不需要缩放,甚至不需要使用较小的字体来适应文本块的宽度和高度。
我还简化了示例的序言,并根据更好的做法对其中的一些进行了更正。这包括使用geometry
而不是欺骗它,parskip
尽可能加载和使用 LaTeX 宏来创建新命令,这比等等更安全。我替换了过时的长字体命令,删除了误导性的花括号,并替换了特殊上下文之外的\def
使用。还有一些我忘记的更一般的整理。\newline
\\
但是,由于我无法在所有情况下读取排版后的树,因此在转换过程中我可能无意中损坏了一些位。
\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}