如何排列树形图以节省空间?

如何排列树形图以节省空间?

在此处输入图片描述我想以一种美观的方式排列树形图以节省空间。这是与该图相对应的 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方向。您需要先调整以绘制水平边缘。另外,我简化了您的代码。rotatecoordinateedge 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}

相关内容