在 Tikz 中为树指定 2 级样式

在 Tikz 中为树指定 2 级样式

这个想法是通过应用程序代码读取数据库并根据队友的姓名和他们的层次结构计算节点数和大小来自动生成组织结构图。

阅读完 PGF 手册第 319-220 页后,我设计了以下 MWE 来绘制示例静态组织结构图:

\documentclass[10pt,landscape,ansibpaper]{article}
\usepackage{tikz}

\usepackage{geometry}
\geometry{
    left=2em,
    right=2em,
    top=2em,
    bottom=2em,
}

\usetikzlibrary{trees}
\tikzstyle{every node}=[draw=black, thin, minimum height=3em]

\begin{document}
\footnotesize
\begin{tikzpicture}[
    supervisor/.style={%
        text centered, text width=12em,
        text=black
    },
    teammate/.style={%
        text centered, text width=12em,
        text=black
    },
    subordinate/.style={%
        grow=down,
        xshift=-3.2em, % Horizontal position of the child node
        text centered, text width=12em,
        edge from parent path={(\tikzparentnode.205) |- (\tikzchildnode.west)}
    },
    level1/.style ={level distance=4em,anchor=west},
    level2/.style ={level distance=8em,anchor=west},
    level3/.style ={level distance=12em,anchor=west},
    level4/.style ={level distance=16em,anchor=west},
    level 1/.style={edge from parent fork down,sibling distance=14em,level distance=5em}
%   level 2/.style={edge from parent fork down,sibling distance=28em,level distance=5em}
]
    \node[anchor=south,supervisor](super){Supervisor\\Supervisory position\\Location}[]
    child{node [teammate] {Teammate6\\Position4\\Location4}
        child{node [teammate] {Teammate61\\Position4\\Location4}
            child[subordinate,level1] {node {Subordinate161}}
            child[subordinate,level2] {node {Subordinate261}}}
        child{node [teammate] {Teammate62\\Position4\\Location4}
            child[subordinate,level1] {node {Subordinate162}}
            child[subordinate,level2] {node {Subordinate262}}}  
        child{node [teammate] {Teammate62\\Position4\\Location4}
            child[subordinate,level1] {node {Subordinate162}}
            child[subordinate,level2] {node {Subordinate262}}}  
    }
    child{node [teammate] {Teammate7\\Position5\\Location5}
        child{node [teammate] {Teammate7\\Position5\\Location5}
            child[subordinate,level1] {node {First\\Subordinate}}
            child[subordinate,level2] {node {Subordinate2}}
            child[subordinate,level3] {node {Third\\Teammate}}
            child[subordinate,level4] {node {Longtext-\\teammate}}}
        child{node [teammate] {Teammate7\\Position5\\Location5}
            child[subordinate,level1] {node {First\\Subordinate}}
            child[subordinate,level2] {node {Subordinate2}}
            child[subordinate,level3] {node {Third\\Teammate}}
            child[subordinate,level4] {node {Longtext-\\teammate}}}
    };

\end{tikzpicture}

\end{document}

输出结果(可以理解)是乱码:

在此处输入图片描述

我以为取消注释该level 2行可以解决这个问题,但它让事情变得更糟:

在此处输入图片描述

我在这里做错了什么?

答案1

level 1/.style={...}和之间缺少一个逗号level 2/.style={...},因此您必须增加 中的兄弟距离level 1并减少 中的兄弟距离level 2

\documentclass[10pt,landscape,ansibpaper]{article}
\usepackage[margin=2em]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees}
\tikzstyle{every node}=[draw=black, thin, minimum height=3em]

\begin{document}
\footnotesize
\begin{tikzpicture}[
    supervisor/.style={%
        text centered, text width=12em,
        text=black
    },
    teammate/.style={%
        text centered, text width=12em,
        text=black
    },
    subordinate/.style={%
        grow=down,
        xshift=-3.2em, % Horizontal position of the child node
        text centered, text width=12em,
        edge from parent path={(\tikzparentnode.205) |- (\tikzchildnode.west)}
    },
    level1/.style ={level distance=4em,anchor=west},
    level2/.style ={level distance=8em,anchor=west},
    level3/.style ={level distance=12em,anchor=west},
    level4/.style ={level distance=16em,anchor=west},
    level 1/.style={edge from parent fork down,sibling distance=45em,level distance=5em},
    level 2/.style={edge from parent fork down,sibling distance=18em}
]
    \node[anchor=south,supervisor](super){Supervisor\\Supervisory position\\Location}[]
    child{node [teammate] {Teammate6\\Position4\\Location4}
        child{node [teammate] {Teammate61\\Position4\\Location4}
            child[subordinate,level1] {node {Subordinate161}}
            child[subordinate,level2] {node {Subordinate261}}}
        child{node [teammate] {Teammate62\\Position4\\Location4}
            child[subordinate,level1] {node {Subordinate162}}
            child[subordinate,level2] {node {Subordinate262}}}  
        child{node [teammate] {Teammate62\\Position4\\Location4}
            child[subordinate,level1] {node {Subordinate162}}
            child[subordinate,level2] {node {Subordinate262}}}  
    }
    child{node [teammate] {Teammate7\\Position5\\Location5}
        child{node [teammate] {Teammate7\\Position5\\Location5}
            child[subordinate,level1] {node {First\\Subordinate}}
            child[subordinate,level2] {node {Subordinate2}}
            child[subordinate,level3] {node {Third\\Teammate}}
            child[subordinate,level4] {node {Longtext-\\teammate}}}
        child{node [teammate] {Teammate7\\Position5\\Location5}
            child[subordinate,level1] {node {First\\Subordinate}}
            child[subordinate,level2] {node {Subordinate2}}
            child[subordinate,level3] {node {Third\\Teammate}}
            child[subordinate,level4] {node {Longtext-\\teammate}}}
    };
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容