使用 tikz 的树和宏
我正在尝试使用宏构建我的树,但没有得到我想要的结果。这是一个最小的例子:
\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是对您问题的直接答案,但您可以查看 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.
看起来 LaTeX 隐式地对 \newcommand 的输出进行分组。因此,结果
与此相同:
TikZ 扫描显式“子”关键字,但当它隐藏在命令或块中时找不到它。
我不知道有什么办法解决这个问题,但我没有看到你的宏使语法变得更容易。
It seems that LaTeX implicitly groups the output of a \newcommand. So, the result of
is the same as this:
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.