使用 tikz 的树和宏

发布于 2024-08-17 01:58:14 字数 917 浏览 4 评论 0原文

我正在尝试使用宏构建我的树,但没有得到我想要的结果。这是一个最小的例子:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\newcommand{\LeafNode}[1]{%
  child {node  {#1}}
}

\newcommand{\InnerNode}[3]{%
  child {node {#3}
           #1
           #2
        }
}

\begin{document}

\begin{tikzpicture}
 \node (A) {A}
    \LeafNode{B}
    \LeafNode{C}
 ;
\end{tikzpicture}%
\hspace{2cm}%
\begin{tikzpicture}
 \node (A) {A}
    \InnerNode{\LeafNode{D}}{\LeafNode{E}}{B}
    \LeafNode{C}
 ;
\end{tikzpicture}

\end{document}

我期望这会产生两棵树:

    A             A
   / \           / \
  B   C         B   C
               / \
              D   E

但我得到:

           A
           |
   A       B
   |       |
   B       D
   |       |
   C       C

我错过了什么或者没有办法做到这一点?

顺便说一句,如果我省略根节点上的标签,我会收到 PGF 错误:

! Package pgf Error: No shape named  is known.

-- Tsf

I am trying to build my trees using macros but I don't get the result I want. Here is a minimal example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\newcommand{\LeafNode}[1]{%
  child {node  {#1}}
}

\newcommand{\InnerNode}[3]{%
  child {node {#3}
           #1
           #2
        }
}

\begin{document}

\begin{tikzpicture}
 \node (A) {A}
    \LeafNode{B}
    \LeafNode{C}
 ;
\end{tikzpicture}%
\hspace{2cm}%
\begin{tikzpicture}
 \node (A) {A}
    \InnerNode{\LeafNode{D}}{\LeafNode{E}}{B}
    \LeafNode{C}
 ;
\end{tikzpicture}

\end{document}

I expected this to produce two trees:

    A             A
   / \           / \
  B   C         B   C
               / \
              D   E

but I am getting:

           A
           |
   A       B
   |       |
   B       D
   |       |
   C       C

Am I missing something or there is no way to do it?

BTW, if I omit the label on my root node, I get a PGF error:

! Package pgf Error: No shape named  is known.

-- Tsf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2024-08-24 01:58:14

这不是对您问题的直接答案,但您可以查看 tikz-qtree 包。它提供了更简单的创建树的语法。

This is not a direct answer to you question, but you could check out the tikz-qtree package. It provides a simpler syntax for creating trees.

莳間冲淡了誓言ζ 2024-08-24 01:58:14

看起来 LaTeX 隐式地对 \newcommand 的输出进行分组。因此,结果

\begin{tikzpicture}
 \node (A) {A}
    \LeafNode{B}
    \LeafNode{C}
 ;
\end{tikzpicture}

与此相同:

 \begin{tikzpicture}
 \node (A) {A}
   {child {node {B}}}
   {child {node {C}}}
 ;
\end{tikzpicture}

TikZ 扫描显式“子”关键字,但当它隐藏在命令或块中时找不到它。

我不知道有什么办法解决这个问题,但我没有看到你的宏使语法变得更容易。

It seems that LaTeX implicitly groups the output of a \newcommand. So, the result of

\begin{tikzpicture}
 \node (A) {A}
    \LeafNode{B}
    \LeafNode{C}
 ;
\end{tikzpicture}

is the same as this:

 \begin{tikzpicture}
 \node (A) {A}
   {child {node {B}}}
   {child {node {C}}}
 ;
\end{tikzpicture}

TikZ scans for explicit "child" keywords and does not find it when it is hidden in a command or block.

I don't know any way around this, but I don't see that your macros make the syntax any easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文