森林树箭头问题

森林树箭头问题

在下图中,我无法将 (A2) 与 (B1) (A3) 与 (B1) 连接起来。我无法发现错误。您能帮我吗?

\documentclass[12pt]{article}

\usepackage{amssymb,amsmath,amsfonts,eurosym,geometry,ulem,graphicx,caption,subcaption, hyperref, color,setspace,sectsty,comment,footmisc,caption,natbib,pdflscape,subfigure,array, enumerate, natbib, indentfirst, float, tikz, rotating, lipsum, adjustbox, booktabs, multirow, soul, changepage,threeparttable, forest}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}



\usepackage[unicode, bookmarks, colorlinks, breaklinks]{hyperref}  

\hypersetup{colorlinks=true, pdfstartview=FitV, linkcolor=blue, citecolor=black, plainpages=false, urlcolor=blue}


\usepackage[nameinlink, capitalise, noabbrev]{cleveref}
\bibliographystyle{abbrvnat}
\setcitestyle{authoryear,open={(},close={)}} %Citation-related commands

\normalem

\onehalfspacing
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}{Proposition}
\newtheorem{conj}{Conjecture}

\newenvironment{proof}[1][Proof]{\noindent\textbf{#1.} }{\ \rule{0.5em}{0.5em}}

\newtheorem{hyp}{Hypothesis}
\newtheorem{subhyp}{Hypothesis}[hyp]
\renewcommand{\thesubhyp}{\thehyp\alph{subhyp}}

\newcommand{\red}[1]{{\color{red} #1}}
\newcommand{\blue}[1]{{\color{blue} #1}}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\arraybackslash\hspace{0pt}}m{#1}}

\geometry{left=1.0in,right=1.0in,top=1.0in,bottom=1.0in}


\usetikzlibrary{shapes,arrows,intersections,arrows.meta,shadows,positioning}
\newcommand{\FixedLengthArrow}{2,0}

\begin{document}
\tikzset{
  basic/.style={
    draw,
    text width=3cm, 
    drop shadow, 
    font=\sffamily
  },
  root/.style={
    basic, 
    rounded corners=2pt, 
    thin, 
    align=center,
    fill=red!30
  },
  child node/.style={
    basic, 
    rounded corners=6pt, 
    thin,
    align=center, 
    fill=green!60,
    text width=10em,
    anchor=north
  },
  every child node/.style={child node}
}

\usetikzlibrary{arrows.meta,shadow}


\tikzset{arr/.style = {draw, -{Stealth[length=2mm]}},
  every edge/.style = {arr}
        }

\begin{figure}[H]
\centering
    \begin{forest}
for tree={
% nodes
    draw, semithick, rounded corners,
    fill=green!39, drop shadow,
    text width=10em, text centered,
    font=\sffamily,
% tree
    s sep = 2mm,
    l sep = 5mm,
     edge = {arr},
        }
[Oil Wealth, fill=cyan, sharp corners
    [DD [EE [A1, name=A1]
    
   
    
    [BB, [B1,name=B1 [FF, [CC, [DD,[EE ]]]]]]
    
    [AA, name=A3]]]
    
     [ZZ, no edge]]
    
    
\draw   (A2) edge (B1)
        (A3) edge (B1);  
        
    \end{forest}
\caption{Causal Channels}
\label{fig: Causal}

    \end{figure}
\end{document}

答案1

您提供的代码肯定不是最小工作示例,我建议您确保只加载一次包,并且只加载真正需要的包。

您的错误主要源于两件事:

  1. 您可能不会在forest环境中留下空行。
  2. 如果之前没有定义节点名称,则无法引用该节点。
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{forest}

\usetikzlibrary{shapes, shadows, arrows.meta}

\begin{document}
\tikzset{
  arr/.style={
    draw, -{Stealth[length=2mm]}
  },
}

\begin{forest}
for tree={
% nodes
    draw, semithick, rounded corners,
    fill=green!39, drop shadow,
    text width=10em, text centered,
    font=\sffamily,
% tree
    s sep = 2mm,
    l sep = 5mm,
    edge = {arr},
}
[Oil Wealth, fill=cyan, sharp corners
    [DD 
        [EE 
            [A1, name=A1]
            [BB 
                [B1, name=B1
                    [FF 
                        [CC 
                            [DD
                                [EE]
                            ]
                        ]
                    ]
                ]
            ]
            [AA, name=A3]
        ]
    ]
    [ZZ, no edge]
]
%
\draw[arr] (A1) edge (B1)
           (A3) edge (B1);  
% 
\end{forest}
    
\end{document}

在此处输入图片描述


更新

由于 OP 在他们的问题中添加了如何将根节点直接放在节点“DD”上的问题,我对代码做了一些小调整,在“DD”的左边添加了一个幻影节点:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{forest}

\usetikzlibrary{shapes, shadows, arrows.meta}

\begin{document}
\tikzset{
  arr/.style={
    draw, -{Stealth[length=2mm]}
  },
}

\begin{forest}
for tree={
% nodes
    draw, semithick, rounded corners,
    fill=green!39, drop shadow,
    text width=10em, text centered,
    font=\sffamily,
% tree
    s sep = 2mm,
    l sep = 5mm,
    edge = {arr},
}
[Oil Wealth, fill=cyan, sharp corners
    [, phantom]
    [DD 
        [EE 
            [A1, name=A1]
            [BB 
                [B1, name=B1
                    [FF 
                        [CC 
                            [DD
                                [EE]
                            ]
                        ]
                    ]
                ]
            ]
            [AA, name=A3]
        ]
    ]
    [ZZ, no edge]
]
%
\draw[arr] (A1) edge (B1)
           (A3) edge (B1);  
% 
\end{forest}
    
\end{document}

在此处输入图片描述

我强烈建议看一下快速入门指南forest包中,它包含了很多针对不同应用程序的很好的提示和技巧。

相关内容