我该怎么做才能改善 Tikz 树中的间距?

我该怎么做才能改善 Tikz 树中的间距?

我终于设法用 Tikz 制作了一个家谱树,但是有些空间我不知道如何在节点之间增加:

\documentclass[a4paper,12pt,DIV=calc,headings=small{scrreprt}%,chapterprefix=true,numbers=noenddot
\usepackage{scrpage2}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\setmainfont[Numbers=OldStyle]{Minion Pro}
\setsansfont[Numbers=OldStyle, Ligatures=Rare]{Minion Pro}
\setmonofont[Scale=MatchLowercase]{Consolas}
\usepackage{polyglossia}
\setmainlanguage{brazil}
\usepackage[dvipsnames]{xcolor}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{tikz}
\usetikzlibrary{mindmap,shadows,trees}



\begin{document}


\begin{tikzpicture}[draw, every node/.style={fill=Green, rectangle, rounded     corners},sibling distance=4cm,
level 6/.style={sibling distance=6cm}
]
\node {Maomé}
child {node (fatima) {Fátima}}
child {node (ali) {1. Ali} edge from parent [draw=none]
child {node {2. Hasan}}
child {node {3. Husayn} 
child {node {4. Ali Zaynu'l-Abidin}
child {node {Zayd} child {node [fill=green!30]{Zayditas}}}
child {node {5. Muhammad al-Baqir}
child {node {6. Jaafar al-Sadiq}
child {node {7. Ismail} child {node [fill=green!30] {Califado Ismaelita Fatímida do Egito}} child {node [fill=green!30]{Xiitas Ismaelitas}}}
child {node {7. Musa al-Kazim}
child {node {8. Ali al-Rida}
child {node {9. Muhammad al-Taqi}
child {node {10. Ali al-Hadi}
child {node {11. Hasan al-Askari}
child {node {12. Imam Mahdi}
child {node [fill=green!30] {Xiitas Duodecimais}}
}}}}}}}}}}};

\draw (fatima) -- (ali);
\end{tikzpicture}




\end{document}

看起来像这样:

在此处输入图片描述

我试过了,但无法增加 7-8 之间的空间。有什么方法可以让这些节点之间的空间更均匀吗?

答案1

您可以提供level distance=<larger value>child开始第八级的。这将影响所有后续级别,因此您需要level distance=<old value>为下一个子级重置它。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{mindmap,trees}

\begin{document}
\begin{tikzpicture}[
    draw,
    every node/.style={
        fill=green!70!black,
         rectangle,
        rounded corners
    },
    sibling distance=4cm,
    level distance=12mm,
    level 6/.style={
        sibling distance=6cm
    }
]
\node {Maomé}
child {node (fatima) {Fátima}}
child {node (ali) {1. Ali} edge from parent [draw=none]
child {node {2. Hasan}}
child {node {3. Husayn} 
child {node {4. Ali Zaynu'l-Abidin}
child {node {Zayd} child {node [fill=green!30]{Zayditas}}}
child {node {5. Muhammad al-Baqir}
child {node {6. Jaafar al-Sadiq}
child {node {7. Ismail} child {node [fill=green!30] {Califado Ismaelita Fatímida do Egito}} child {node [fill=green!30]{Xiitas Ismaelitas}}}
child {node {7. Musa al-Kazim}
child [level distance=24mm] {node {8. Ali al-Rida}
child [level distance=12mm] {node {9. Muhammad al-Taqi}
child {node {10. Ali al-Hadi}
child {node {11. Hasan al-Askari}
child {node {12. Imam Mahdi}
child {node [fill=green!30] {Xiitas Duodecimais}}
}}}}}}}}}}};

\draw (fatima) -- (ali);
\end{tikzpicture}


\end{document}

答案2

使用 的力量的解决方案forest。这演示了如何使用fit=band在子树之间获得一些分离(将节点分离在 7/8 处)以及如何使用tikz在兄弟节点之间绘制连接。

定义了两种样式来管理编号:

  • next增加数字并编号节点
  • again对节点进行编号而不增加编号

pale green样式适用于较浅的节点。

森林家庭

\PassOptionsToPackage{dvipsnames}{xcolor}
\documentclass[tikz,12pt,border=5pt]{standalone}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
% use default fonts as I don't have proper Minion except for (pdf)TeX
\usepackage{polyglossia}
\setmainlanguage{brazil}
\usetikzlibrary{shadows,trees}
\usepackage{forest}

\begin{document}
\newcounter{mycount}
\begin{forest}
  pale green/.style={fill=green!30},
  next/.style={
    before typesetting nodes={
      TeX/.wrap pgfmath arg={\stepcounter{mycount}\def\mycounted{\themycount. ##1}}{content()},
      content/.expanded={\mycounted},
    }
  },
  again/.style={
    before typesetting nodes={
      TeX/.wrap pgfmath arg={\def\mycounted{\themycount. ##1}}{content()},
      content/.expanded={\mycounted},
    }
  },
  for tree={
    fill=Green,
    draw,
    rounded corners,
    drop shadow,
    edge=thick,
  }
  [Maomé
    [Fátima, tikz={\draw [thick] () -- (!n);}]
    [Ali, no edge, next
      [Hasan, next]
      [Husayn, next
        [Ali Zaynu'l-Abidin, next
          [Zayd
            [Zayditas, pale green]]
          [Muhammad al-Baqir, next
            [Jaafar al-Sadiq, next, for children={fit=band}
            [Ismail node, for children={pale green}, next
              [Califado Ismaelita Fatímida do Egito]
              [Xiitas Ismaelitas]
            ]
            [Musa al-Kazim, again
              [Ali al-Rida, next
                [Muhammad al-Taqi, next
                  [Ali al-Hadi, next
                    [Hasan al-Askari, next
                      [Imam Mahdi, next
                        [Xiitas Duodecimais, pale green]
  ]]]]]]]]]]]]
\end{forest}
\end{document}

答案3

我的回答不是直接回答你的问题,而是尝试提供一种不同的方法来绘制你的树。这里,这个家谱树是用包制作的genealogytree。与原始 TikZ 树的主要区别在于,你可以有多个父级(通常每个家庭两个),并且节点由自动布局算法放置,这应该会提供更好的间距。

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[dvipsnames]{xcolor}
\usepackage{geometry}
\usepackage[all]{genealogytree}

\begin{document}

\begin{genealogypicture}[processing=tcbox*,
  level size=5mm,
  node size from=5mm to 50mm,
  child distance=8mm,parent distance=8mm,further distance=8mm,
  box={size=fbox,boxrule=0pt,frame hidden,colback=Green,arc=3pt},
  edges={rounded=1mm,foreground={black,line width=0.3mm},background={white,line width=0.8mm}},
]
child{ g{Maomé}
  child{ g{Fátima} p{1. Ali} c{2. Hasan}
    child{ g{3. Husayn}
      child{ g{4. Ali Zaynu'l-Abidin}
        child{ g{Zayd} c[box={colback=green!30}]{Zayditas}
        }
        child{ g{5. Muhammad al-Baqir}
          child{ g{6. Jaafar al-Sadiq}
            child{ g{7. Ismail}
              c[box={colback=green!30}]{Califado Ismaelita Fatímida do Egito}
              c[box={colback=green!30}]{Xiitas Ismaelitas}
            }
            child{ g{7. Musa al-Kazim}
              child{ g{8. Ali al-Rida}
                child{ g{9. Muhammad al-Taqi}
                  child{ g{10. Ali al-Hadi}
                    child{ g{11. Hasan al-Askari}
                      child{ g{12. Imam Mahdi}
                        c[box={colback=green!30}]{Xiitas Duodecimais}
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
\end{genealogypicture}

\end{document}

为了完整性:更改级别距离是可能的,但会影响级别的所有节点以保持自动布局算法正常工作。因此,如果第 7 代和第 8 代之间应该有代际跳跃,您可以通过以下方式插入一个不可见的链接节点

child{ g[phantom*,box={height=0mm}]{} ... }

这使:

在此处输入图片描述

完整代码:

\documentclass[a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[dvipsnames]{xcolor}
\usepackage{geometry}
\usepackage[all]{genealogytree}

\begin{document}

\begin{genealogypicture}[processing=tcbox*,
  level size=5mm,
  node size from=5mm to 50mm,
  child distance=8mm,parent distance=8mm,further distance=8mm,
  box={size=fbox,boxrule=0pt,frame hidden,colback=Green,arc=3pt},
  edges={rounded=1mm,foreground={black,line width=0.3mm},background={white,line width=0.8mm}},
]
child{ g{Maomé}
  child{ g{Fátima} p{1. Ali} c{2. Hasan}
    child{ g{3. Husayn}
      child{ g{4. Ali Zaynu'l-Abidin}
        child{ g{Zayd} c[box={colback=green!30}]{Zayditas}
        }
        child{ g{5. Muhammad al-Baqir}
          child{ g{6. Jaafar al-Sadiq}
            child{ g{7. Ismail}
              c[box={colback=green!30}]{Califado Ismaelita Fatímida do Egito}
              c[box={colback=green!30}]{Xiitas Ismaelitas}
            }
            child{ g{7. Musa al-Kazim}
              child{ g[phantom*,box={height=0mm}]{} child{ g{8. Ali al-Rida}
                child{ g{9. Muhammad al-Taqi}
                  child{ g{10. Ali al-Hadi}
                    child{ g{11. Hasan al-Askari}
                      child{ g{12. Imam Mahdi}
                        c[box={colback=green!30}]{Xiitas Duodecimais}
}}}}}}}}}}}}}
\end{genealogypicture}

\end{document}

相关内容