初中树状图tree中如何将各种组合结果以(A,B)形式简洁实现?

发布于 2021-06-13 17:46:48

10.png
如图,如何简单实现?用child太麻烦了。用tree我不会列结果的坐标形式和小红小明那种。刚学几天,用实例改的,还不怎么懂,请各位大神指教。代码如下:

\documentclass[UTF8]{ctexart}
%\usepackage{amsmath}
%\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=5pt},edge from parent/.style={draw,->,red,very thick},level 1/.style={sibling distance=25mm},
    level 2/.style={sibling distance=5mm}] % 为所有分支设置层间距
\node {开始} [grow'=right]
child {node[yshift=-15mm]{小红} edge from parent[draw=none]}
child {node {A}
    child {node{小明} edge from parent[draw=none]  child {node[xshift=1mm] {结果}}}
    child {node {A} child {node {(A,A)}}}
    child {node {B}  child {node {(A,B)}}}
    child {node {C} child {node {(A,C)}}}
    child {node {D} child {node {(A,D)}}}
}
child {node {B}
    child {node {A}}
    child {node {B}}
    child {node {C}}
    child {node {D}}
}
child {node {C}
    child {node {A}}
    child {node {B}}
    child {node {C}}
    child {node {D}}
}
child {node {D}
    child {node {A}}
    child {node {B}}
    child {node {C}}
    child {node {D}}
};
\end{tikzpicture}    
\end{document}
\documentclass[UTF8]{ctexart}
\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing,arrows,graphs.standard}
\usegdlibrary{trees,layered}
\begin{document}
    
\tikz \graph [tree layout,grow'=right,level distance=2.5cm,sibling distance=1mm,sibling sep=2pt
,nodes={red,inner sep=5pt,minimum size=5mm}] { { [number nodes, number nodes sep=*]
    开始->[red] {
            A -> {A--这里写结果,B,C,D},
            B -> {A,B,C,D},
            C -> {A,B,C,D},
            D-> {A,B,C,D}, } },
};
\end{document}

查看更多

关注者
0
被浏览
2k
3 个回答
LaTeXStudio
LaTeXStudio 2021-06-13
这家伙很懒,什么也没写!

贴代码,编辑器提供了快捷键了,点一下,代码放进去,方便别人也方便自己。

越来越少
越来越少 2021-06-14
这家伙很懒,什么也没写!
\begin{tikzpicture}
\graph [
tree layout,
grow'=right,
level distance=2.5cm,
sibling distance=1mm,
sibling sep=2pt,
nodes={red,inner sep=5pt,minimum size=5mm}
] 
{[number nodes, number nodes sep=*]
    开始-> {
            A-> {A->Ac/"(A,A)",
                  B,C,D},
            B -> {A,B,C,D},
            C -> {A,B,C,D},
            D-> {A,B,C,D}, 
            小王[>{draw=none}]--小李[>{draw=none}]->那啥,
            }, 
};
\node [above]at(A*2 |- A*3.north){小红};
\node [above](小明)at(A*3.north){小明};
\node [above](结果)at(Ac*4 |- A*3.north){结果};
\draw [->](小明)--(结果);
\end{tikzpicture}

2d2da10ccc9a16b604d1e1fc8925a5a4.png
——————————————————————————————————————
补充,参考选项 /tikz/graph/parse

\def\mystateA{}
\foreach \i[count=\ci,remember=\i as \li] in {A,B,C,D}
{
  \ifnum\ci=1\relax
  \else
    \xdef\mystateA{\mystateA\li->{\mystateB},}
  \fi
  \xdef\mystateB{}
  \foreach \j[count=\cj] in {A,B,C,D}
  {
    \xdef\mystateB{\mystateB\j->\i\j/"(\i,\j)",}
    \ifnum\ci=4\relax
      \ifnum\cj=4\relax
        \xdef\mystateA{\mystateA\i->{\mystateB},}
      \fi
    \fi
  }
}

\begin{tikzpicture}
  \graph [
    tree layout,
    grow'=right,
    level distance=2.5cm,
    sibling distance=1mm,
    sibling sep=2pt,
    nodes={red,inner sep=5pt,minimum size=5mm}
  ] 
  {
    [number nodes, number nodes sep=*]
    开始-> {
      [parse/.expanded=\mystateA]
    }, 
  };
  \node (结果)[above] at(AA*4.north){结果};
  \node (小明)[above] at(A*3.north |- AA*4.north){小明};
  \node (小红)[above] at(A*2.north |- AA*4.north){小红};
\end{tikzpicture}
海之魂
海之魂 2021-06-14
这家伙很懒,什么也没写!

247.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览