错误绘制树 tikz

错误绘制树 tikz

我正在尝试画一棵树tikz

我尝试过这个:

\documentclass{report}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[nodes={draw, circle}]
\path
(0, 0)          node        (1a)        {5};
(-2, -1)        node        (2a)        {12};

\draw
(1a)--(2a)
\end{tikzpicture}
\end{document}

我明白了! Package pgf Error: No shape named `2a' is known.如何修复它?

我还想调整链接。

如果我省略该\draw部分,则只会绘制第一个节点。我如何绘制没有任何链接的 2 个节点?

有没有什么办法可以画得更粗一些的线条呢?

答案1

在创建节点后但未开始新路径时,您\path以分号终止,因此第二次调用不执行任何操作。1anode

如果你检查日志,错误前面会有

Missing character: There is no ( in font nullfont!
Missing character: There is no - in font nullfont!
Missing character: There is no 2 in font nullfont!
Missing character: There is no , in font nullfont!
Missing character: There is no - in font nullfont!
Missing character: There is no 1 in font nullfont!
Missing character: There is no ) in font nullfont!
Missing character: There is no n in font nullfont!
Missing character: There is no o in font nullfont!
Missing character: There is no d in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no ( in font nullfont!
Missing character: There is no 2 in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no ) in font nullfont!
Missing character: There is no 1 in font nullfont!
Missing character: There is no 2 in font nullfont!
Missing character: There is no ; in font nullfont!

! Package pgf Error: No shape named `2a' is known.

表示该(-2, -1) node (2a) {12};行正试图被打印而不是被解析。

您还需要一个分号来终止\draw命令。

\documentclass{report}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[nodes={draw, circle}]
\path
(0, 0)          node        (1a)        {5}
(-2, -1)        node        (2a)        {12};

\draw
(1a)--(2a);
\end{tikzpicture}
\end{document}

但取决于你的使用情况

\documentclass{report}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[nodes={draw, circle}]
\node (1a) at (0, 0) {5};
\node (2a) at (-2, -1) {12};

\draw (1a)--(2a);
\end{tikzpicture}
\end{document}

可能更容易写。

答案2

您的命令第一行后面有一个分号\path,因此下一行根本不属于路径。因此删除该分号,您就会得到想要的结果。但是,鉴于问题的标题,如果您要绘制树,您确实应该使用树绘制包之一。该forest包是迄今为止最适合此用途的。我添加了一个示例,说明如何绘制您似乎正在构建的那种树forest

\documentclass{report}
\usepackage{tikz}
\usepackage{forest}
\begin{document}
\begin{tikzpicture}[nodes={draw, circle}]
\path
(0, 0) node (1a) {5}
(-2, -1) node (2a) {12};
\draw[very thick] (1a)--(2a);
\end{tikzpicture}

\forestset{my tree/.style={for tree={draw,circle,align=center,minimum size=2em,inner sep=1pt,s sep=1cm,edge={very thick}}}}
\begin{forest}my tree
[5
    [12 
        [6 ]
        [7 ]
    ]
    [13 ]
]
\end{forest}
\end{document}

代码输出

相关内容