将树的方向改为水平

将树的方向改为水平

我正在使用我找到的以下代码。我添加了颜色和字母大小。但我需要更改以下我无法做到的内容:

  1. 消除边缘处的 c 字母。(红色圆圈)
  2. 创建相同风格的树,但现在是水平的(下面的示例)。

妇女权利委员会:

\documentclass[tikz,border=10pt,multi] {standalone}
\usepackage{forest}
\standaloneenv{forest,tikzpicture}

\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered, font=\sffamily, top color=white, bottom color=blue!20},
  arn/.style = {treenode, circle, font=\sffamily\bfseries, draw=black, fill=white, text width=6.5ex},
  arnrec/.style = {treenode, rectangle, font=\sffamily\bfseries, draw=black,  fill=white, text width=ex,minimum width=4.0ex, minimum height=4.0ex},
  arnsmall/.style = {treenode, circle, font=\sffamily\bfseries, draw=black, fill=white, text width=1.5ex}, root/.style = {treenode, font=\Large, bottom color=red!30}
}

\begin{document}
\begin{forest}
  make f/.style={
    append={[f, arnsmall, tier=prez]},
    l sep+=-5mm,
  },
  make empty/.style={
    coordinate,
    no edge,
    tier=prez,
    for children={
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!uu.parent anchor) -- (.child anchor)\forestoption{edge label};
      }
    },
  },
  my x/.style n args=2{
    edge label={node [midway, #2, font=\footnotesize, text=black] {$c_{#1}$}},
  },
  for tree={
    arn,
    l sep+=5mm,
  } 
     [6, my x={61} {left}
        [1, my x={6,1}{left}
        [5, my x={1,5}{fill=white}
        ]
]
      [2, my x={6,2}{fill=white}
        ]
      [3, my x={6,3}{fill=white}
        ]
      [4, my x={6,4}{fill=white}
        ]
      [8, my x={6,8}{fill=white}
        ]
      [7, my x={6,7}{fill=white}
        ]
      [9, my x={9,6}{fill=white}
                ]
      ]
      ]
\end{forest}


\end{document}

enter image description here

答案1

像这样?

enter image description here

使用forest超出了我暂时的能力,但使用 pure 的解决方案tikz非常简单:

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{chains, positioning, quotes, shapes}
\makeatletter
\tikzset{
  off chain/.code={\def\tikz@lib@on@chain{}}%
}
\makeatother

\begin{document}
\begin{tikzpicture}[auto,
node distance = 11mm and 22mm,
  start chain = A going right,
N/.style = {% Node
            ellipse, draw, minimum size=5mm,
            inner sep=1mm, outer sep=0mm,
            on chain=A},
every chain label/.style={inner sep=1mm, font=\footnotesize}
                                 ]
\node [N] {1,2};
\node [N] {2,3};
\node [N] {3,6};
\node [N] {6,7};
%
\node (n1) [N, off chain, below=of A-2] {2,5};
\node (n2) [N, off chain, below=of A-3] {3,4};
%
\draw   (A-1) to ["${1,3|2}$"] (A-2)
        (A-2) to ["${2,6|3}$"] (A-3)
        (A-3) to ["${3,7|6}$"] (A-4)
%
        (A-2) to ["${3,5|2}$"] (n1)
        (A-2) to ["${2,4|3}$"] (n2);
\end{tikzpicture}
\end{document}

附录(1): 如果您喜欢使用带有粗体字母的彩色节点,如下所示:

enter image description here

那么您唯一需要做的就是在节点样式的定义中添加两行:

N/.style = {% Node
            ellipse, draw, minimum size=5mm,
            top color=white, bottom color=blue!20, % <-- added color to nodes
            font=\bfseries,                        % <-- added boldface to node content
            inner sep=1mm, outer sep=0mm,
            on chain=A},

附录(2): 如果你喜欢你定义的样式(在其他forest情况下使用),那么你只需要更改tikzpicture节点的选项和名称。在这种情况下,完整的姆韦是:

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{chains, positioning, quotes, shapes}
\makeatletter
\tikzset{
  off chain/.code={\def\tikz@lib@on@chain{}}%
}
\makeatother

\tikzset{
  treenode/.style = {align=center, inner sep=0pt,
                     text centered, font=\sffamily,
                     top color=white, bottom color=blue!20},
       arn/.style = {treenode, circle, font=\sffamily\bfseries,
                    draw=black, fill=white, text width=6.5ex},
        }

\begin{document}
\begin{tikzpicture}[auto,
node distance = 11mm and 22mm,
  start chain = A going right,
every chain label/.style={inner sep=1mm, font=\footnotesize},
arn/.append style = {on chain=A}
                                 ]
\node [arn] {1,2};
\node [arn] {2,3};
\node [arn] {3,6};
\node [arn] {6,7};
%
\node (n1) [arn, off chain, below=of A-2] {2,5};
\node (n2) [arn, off chain, below=of A-3] {3,4};
%
\draw   (A-1) to ["${1,3|2}$"] (A-2)
        (A-2) to ["${2,6|3}$"] (A-3)
        (A-3) to ["${3,7|6}$"] (A-4)
%
        (A-2) to ["${3,5|2}$"] (n1)
        (A-2) to ["${2,4|3}$"] (n2);
\end{tikzpicture}
\end{document}

enter image description here

答案2

这是一个使用 Ti 的方法Z 的图形语法,但不依赖于需要 LuaTeX 的图形绘制算法。如果您乐于使用 LuaTeX,您可以做更多花哨(和更自动化)的事情。此外,真正理解这一点的人可能比我更自动地做到这一点,即使坚持使用 pdfTeX。(我无法弄清楚如何从放置规则中排除一个节点,所以最终手动放置它们。即便如此,语法也使代码非常简洁。)

\documentclass[border=11pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,shapes.geometric,quotes}
\begin{document}
  \begin{tikzpicture}
  \graph [nodes={draw, ellipse}]%, grow right sep=10mm, branch down sep=10mm]
  { a/"1,2" --["$1, 3|2$"] b/"2,3"[x=1] -- {c/"3,6"[>"$2, 6|3$",x=2] --["$3, 7|6$"] f/"6,7"[x=3], d/"3,4"[>"$2, 4|3$", x=2, y=-1], e/"2,5"[>"$3, 5|2$", y=-1] } };
\end{tikzpicture}
\end{document}

graph

如果你希望只改变图形的结构,并让其使用与森林树相同的样式,你可以应用 TiZ 样式与图表类似。例如,

\documentclass[border=11pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,shapes.geometric,quotes}
\begin{document}
\tikzset{% cwestiwn Wilmer Rojas: https://tex.stackexchange.com/q/387249/
  treenode/.style = {align=center, inner sep=0pt, text centered, font=\sffamily, top color=white, bottom color=blue!20},
  arn/.style = {treenode, circle, font=\sffamily\bfseries, draw=black, fill=white, text width=6.5ex},
}
\begin{tikzpicture}[x=15mm]
  \graph [nodes={arn}, edge quotes={midway, anchor=center, font=\footnotesize, fill=white}]%, grow right sep=10mm, branch down sep=10mm]
  { a/"1,2" --["$1, 3|2$"] b/"2,3"[x=1] -- {c/"3,6"[>"$2, 6|3$",x=2] --["$3, 7|6$"] f/"6,7"[x=3], d/"3,4"[>"$2, 4|3$", x=2, y=-1], e/"2,5"[>"$3, 5|2$", y=-1] } };
\end{tikzpicture}
\end{document}

style-alike

在这种情况下,我会分离出边缘的样式,并在森林定义和图表中使用该样式。

\tikzset{%
  arn edges/.style={midway, #1, font=\footnotesize, text=black},
}%
\forestset{%
  my x/.style n args=2{
    edge label={node [arn edges={#2}] {$c_{#1}$}},
  },
}%
...
\graph [nodes={arn}, edge quotes={arn edges={fill=white}}] ...

等等。

由于这里没有加载 Forest 设置,因此我们将其忽略,这样我们

\documentclass[border=11pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,shapes.geometric,quotes}
\begin{document}
\tikzset{% cwestiwn Wilmer Rojas: https://tex.stackexchange.com/q/387249/
  treenode/.style = {align=center, inner sep=0pt, text centered, font=\sffamily, top color=white, bottom color=blue!20},
  arn/.style = {treenode, circle, font=\sffamily\bfseries, draw=black, fill=white, text width=6.5ex},
  arn edges/.style={midway, #1, font=\footnotesize, text=black},
}%
\begin{tikzpicture}[x=15mm]
  \graph [nodes={arn}, edge quotes={arn edges={fill=white}}]%, grow right sep=10mm, branch down sep=10mm]
  { a/"1,2" --["$1, 3|2$"] b/"2,3"[x=1] -- {c/"3,6"[>"$2, 6|3$",x=2] --["$3, 7|6$"] f/"6,7"[x=3], d/"3,4"[>"$2, 4|3$", x=2, y=-1], e/"2,5"[>"$3, 5|2$", y=-1] } };
\end{tikzpicture}
\end{document}

生成上面的第二张图像。

相关内容