![如何排列树形图以节省空间?](https://linux22.com/image/434362/%E5%A6%82%E4%BD%95%E6%8E%92%E5%88%97%E6%A0%91%E5%BD%A2%E5%9B%BE%E4%BB%A5%E8%8A%82%E7%9C%81%E7%A9%BA%E9%97%B4%EF%BC%9F.png)
我想以一种美观的方式排列树形图以节省空间。这是与该图相对应的 latex 代码。请帮我。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[labelformat=simple]{subcaption}
\renewcommand\thesubfigure{\alph{subfigure}}
\DeclareCaptionLabelFormat{subcaptionlabel}{\normalfont(\textbf{#2}\normalfont)}
\captionsetup[subfigure]{labelformat=subcaptionlabel}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}
\colorlet{linecol}{black!75}
\usepackage{geometry}
\usepackage[templates]{genealogytree}
\usepackage{lmodern}
\usepackage{graphics}
\tikzset{%
parent/.style={align=center,text width=3cm,rounded corners=3pt},
child/.style={align=center,text width=3cm,rounded corners=3pt}
}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{lipsum}
\definecolor{blueframe}{RGB}{0,0,0}
\definecolor{bluefill}{rgb}{0.37, 0.62, 0.63}
\def\shifta{1cm}
\def\shiftb{.2cm}
\tikzset{%
every node/.style={draw=blueframe, minimum width=3cm, minimum height=1cm
, fill=green!5
},
}
\usepackage{varwidth}
\usepackage{ragged2e}
\usepackage[edges]{forest}
\forestset{declare boolean={switch me}{0}}
\begin{document}
% \maketitle
\begin{figure}[t]
\centering
\tikzstyle{every node}=[font=\normalsize\sffamily]
% \tiny
% \scriptsize
% \footnotesize
% \small
\begin{center}
\resizebox{.9\textwidth}{!}{%
\begin{forest}
where n children=0{!u.switch me}{},
delay={
where switch me={
replace by/.process={ Ow {content} {{[#1, fit=rectangle, append]}}},
no edge,
shape=coordinate,
content=,
for tree={
grow'=0,
},
for children={
folder,
edge path'={(!uu.parent anchor) |- (!.child anchor)},
},
}{},
},
before typesetting nodes={
where level=0{fill=gray!2}{
if level=1{fill=yellow!2}{
if level=2{
% fill=green!2
}{
% fill=blue!2
},
},
},
for tree={
draw,
thick,
rounded corners,
edge+={->,thick},
align=center,
font=\normalsize\sffamily,
parent anchor=children,
},
where switch me={
for current and ancestors={
edge path'={(!u.parent anchor) -- ++(0pt,-5pt) -| (.child anchor) }, % forked edge seems to get confused
child anchor=parent,
},
for nodewalk={fake=1,descendants}{
folder,
child anchor=west,
}
}{},
},
[\textbf{ABCD EFGH}, fill=gray!2, parent
[\textbf{ABCD EFGH},
for tree={fill=brown!2, child}
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
[ 7 ]
[ 8 ]
[ 9 ]
[ 10 ]
[ 11 ]
[ 12 ]
[ 13 ]
[ 14 ]
[ 15 ]
[ 16 ]
[ 17 ]
[ 18 ]
[ 19 ]
[ 20 ]
[ 21 ]
[ 22 ]
[ 23 ]
[ 24 ]
[ 25 ]
[ 26 ]
]
[\textbf{ABCD EFGH}, for tree={fill=red!2,child}
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
[ 7 ]
[ 8 ]
]
[\textbf{ABCD EFGH}, for tree={fill=yellow!2, child}
[ 1 ]
[ 2 ]
]
[\textbf{ABCD EFGH}, for tree={fill=purple!2,child}
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
]
[\textbf{ABCD EFGH}, for tree={fill=green!2, child}
[ 1 ]
[ 2 ]
[ 3 ]
]
[\textbf{${N}$-slope}, for tree={fill=green!2, child}
[ 1 ]
[ 2 ]
[ 3 ]
]
]
]
\end{forest}
}
\end{center}
\caption{5 line long caption}
\label{fig:fig}
\end{figure}
\end{document}
答案1
您可以在添加节点后更改grow
方向。您需要先调整以绘制水平边缘。另外,我简化了您的代码。rotate
coordinate
edge path'
\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\begin{figure}
\centering
\resizebox{.9\textwidth}{!}{%
\begin{forest}
forked edges,
where={level()<1}{}{folder, grow'=east},
for tree={
font=\sffamily,
draw, thick, rounded corners=3pt, edge={thick, ->},
fork sep=4mm,
minimum height=5mm,
if level=1{fill=yellow!10}{minimum width=3cm}
}
[ABCD EFGH, fill=gray!10, calign=edge midpoint
[ABCD EFGH, for descendants={fill=brown!10}
[1][2][3][4][5][6][7][8]
[, coordinate, edge={thick, -}, for tree={grow'=north, rotate=90}, for descendants={edge path'={(!u)-|(.child anchor)}}
[9][10][11][12][13][14][15][16][17][18][19][20][21][22][23][24][25][26]
]
]
[ABCD EFGH, for children={fill=red!10}
[1][2][3][4][5][6][7][8]
]
[ABCD EFGH, for children={fill=yellow!10}
[1][2]
]
[ABCD EFGH, for children={fill=purple!10}
[1][2][3][4]
]
[ABCD EFGH, for children={fill=green!10}
[1][2][3]
]
[$N$-slope, for children={fill=green!10}
[1][2][3]
]
]
\end{forest}
}
\caption{5 line long caption}
\label{fig:fig}
\end{figure}
\end{document}