3

I'm trying to draw a tree with tikz

I've tried this:

\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}

I get ! Package pgf Error: No shape named `2a' is known. How can I fix it?

I would also like to tweak the links.

If I omit the \draw part, only the first node is drawn. How can I draw 2 nodes without any link?

Is there any way to draw with a thicker line?

1
  • Try {5}, i.e. without the semicolon: you describe one (longer) path, not two.
    – MS-SPO
    Commented Jun 10, 2023 at 17:16

2 Answers 2

5

You are terminating the \path with a semicolon after making the 1a node but not starting a new path, so the second node invocation does nothing.

If you check the log the error is preceded by

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.

indicating the (-2, -1) node (2a) {12}; line is trying to be printed rather than parsed.

You also need a semicolon to terminate the \draw command.

\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}

Depending on your use case though

\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}

might be easier to write.

3

You have a semicolon after the first line of your \path command, so the next line doesn't belong to the path at all. So remove that semicolon and you get what you want. However, given the title of the question, if you are drawing trees, you really should use one of the tree drawing packages. The forest package is by far the best for this. I've added an example of how to draw a tree of the sort you seem to be constructing with 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}

output of code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .