编辑

编辑

我想重现下面显示的树形图。我的主要问题是如何将文本(是/否/曝光...)放在线条旁边。

,我

答案1

细节留给读者练习......重点是问题的主题,即添加标签。

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- +(0,-2.5mm) -| (.child anchor)\forestoption{edge label};
    },
    parent anchor=south,
    child anchor=north,
    draw,
    l sep+=2.5mm,
    font=\sffamily,
    edge={font=\scriptsize\sffamily}% style for labels can go here
  }
  [Text
    [Query?
    [Option A, edge label={node[pos=.75, left]{Yes}}, l sep+=5mm, for children={circle, inner sep=1pt, edge path={\noexpand\path [\forestoption{edge}] (!u.parent anchor) -- (.child anchor)\forestoption{edge label};} }
        [a]
        [b]
        [c, edge label={node[midway, right, align=left]{Something\\Something else\\Yet another thing}}]
      ]
      [Option B, edge label={node[pos=.75, right]{No}}]
    ]
  ]
\end{forest}
\end{document}

edge label以通常的方式添加标签,节点作为路径规范的一部分添加。

添加标签

编辑

这是您发布的代码另一个答案,稍微整理了一下,并启用了多行节点。我定义了几种样式来节省输入,并使添加“是/否”标签更加容易。

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\forestset{
  my edge label/.style={
    if n=1{
      edge label={node[pos=.75, left]{Yes}}
    }{
      edge label={node[pos=.75, right]{No}}
    }
  },
  my circle/.style={circle, inner sep=1pt, minimum width=60pt, anchor=center},
  light grey/.style={fill=gray!20},
  dark grey/.style={fill=gray!60},
}
\begin{forest}
  for tree={
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- +(0,-2.5mm) -| (.child anchor)\forestoption{edge label};
    },
    parent anchor=south,
    child anchor=north,
    draw,
    l sep+=2.5mm,
    font=\sffamily,
    edge={font=\scriptsize\sffamily},
    align=center,
    light grey
  }
  [Did investigator\\assign exposures?, for children={dark grey}
    [Observational study, my edge label
      [Ramdom allocation?, for children={my circle}
        [Ramsomised\\controlled\\trial, my edge label]
        [Non-\\ramsomised\\controlled\\trial, my edge label]
      ]
    ]
    [Observational study, my edge label
      [Comparison group?, for children={dark grey}
        [Analytical\\study, my edge label
          [Direction?, l sep+=5mm, s sep+=25pt, dark grey,
            for children={%
              my circle,
              edge path={%
                \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- (.child anchor)\forestoption{edge label};
              },
            }
            [Cohort\\study, edge label={node[midway, left, align=left]{Exposure - Outcome}}]
            [Case-\\control\\study, edge label={node[pos=.9, left, align=left]{Exposure - Outcome}}]
            [Cross-\\sectiuonal\\study, edge label={node[midway, right, align=left]{Exposure and\\\hspace*{2.5em}outcome at\\\hspace*{5em}the same time}}]
          ]
        ]
        [Descriptive\\study, my edge label
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

结果如下:

修改了多行节点的代码

答案2

好了,图表几乎完成了。谢谢@cfr,你的例子很完美。

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
edge path={
  \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- +(0,-2.5mm) -| (.child anchor)\forestoption{edge label};
},
parent anchor=south,
child anchor=north,
draw,
l sep+=2.5mm,
font=\sffamily,
edge={font=\scriptsize\sffamily}% style for labels can go here
  }
[Did investigator assign exposures?
    [Observational study, edge label={node[pos=.75, left]{Yes}}
        [Ramdom allocation?
            [Ramsomised controlled trial, edge label={node[pos=.75, left]{Yes}}]
            [Non-ramsomised controlled trial, edge label={node[pos=.75, right]{No}}]]]
    [Observational study, edge label={node[pos=.75, right]{No}}
        [Comparison group?
            [Analytical study, edge label={node[pos=.75, left]{Yes}}
                [Direction?, l sep+=5mm, for children={circle, inner sep=1pt, edge path={\noexpand\path [\forestoption{edge}] (!u.parent anchor) -- (.child anchor)\forestoption{edge label};} }
                    [Cohort study, edge label={node[midway, left, align=left]{Exposure - Outcome}}]
                    [Case-control study, edge label={node[midway, left, align=left]{Exposure - Outcome}}]
                    [Cross-sectiuonal study, edge label={node[midway, right, align=left]{Exposure and\\outcome at\\the same time}}]
  ]]
            [Descriptive study, edge label={node[pos=.75, right]{No}}
            ]
        ]
    ]
]
\end{forest}
\end{document}

在此处输入图片描述

相关内容