2

In using tikz, I often have the following situation:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
  \tikz\draw node (A) {A} node[right=of A] (B) {B} (A) -- (B);
\end{document}

In other words, I draw a node (B) and immediately connect it to another node (A). even if I don't use the positioning syntax but explicit coordinates, the first produces wrong lines:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
    \tikz[nodes={draw}]\draw (0,0) node (A) {A} -- (1,0) node (B) {B};
    \tikz[nodes={draw}]\draw (0,0) node (A) {A} (1,0) node (B) {B} (A) -- (B);
\end{document}

because the first picture connects the coordinates (0,0) and (1,0), but not the nodes' borders.

Is there any convenient way of creating and connecting nodes in one go?

5
  • You could write \draw node (A) at (0,0) {A} node (B) at (1,0) {B} (A) -- (B); but this is probably not what you mean with "one go" ... Commented yesterday
  • you could write \draw (0,0) node (A) {A} -- (1,0) node [anchor=west] (B) {B}; but this will alter the position of B. or you can fill the node. Commented yesterday
  • 1
    Besides reading impatience from such code I don't see its usefulness. To my view you create more obscure code. If you really need such codes frequently I suggest wrapping them into \newcommand or a \pic, depending on the context. Commented yesterday
  • 1
    but the construction of the path is intentionally unaffected by the addition of the node if you use the syntax in your example. in order to invoke the implicit use of node borders, you have to be drawing to/from those nodes. Commented yesterday
  • @cfr Yes, that's precisely what annoys me: I have to write every node's name twice. The typing is one thing, but I also find it obscure to read. Commented yesterday

2 Answers 2

6

You might be looking for the chains library.

enter image description here

You can use absolute coordinates, or the chains direction options going and placed (instead of the positioning library). Many more options can be found in the "chains" chapter of the PGF manual (chapter 48 in version 3.1.11a).

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{chains}

\begin{document}

\begin{tikzpicture}[start chain, node distance=5mm, every join/.style={->, red}]
\node [draw, on chain] (A){A};
\node [draw, on chain, join] (B){B};
\node [draw, on chain, join] (C){C};
\node [draw, on chain=going below, join] (D){D};
\node [draw, on chain, join] at (3,-.5) (E){E};
\end{tikzpicture}

\end{document}

Of course, you could make a style to condense the syntax even more:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{chains}

\tikzset{
    mychain/.style={draw, on chain=#1, join={by ->, red}},
    mychain/.default={}
}

\begin{document}

\begin{tikzpicture}[start chain, node distance=5mm]
\node [mychain] (A){A};
\node [mychain] (B){B};
\node [mychain] (C){C};
\node [mychain=going below] (D){D};
\node [mychain] at (3,-.5) (E){E};
\end{tikzpicture}

\end{document}
4

You might be interested in using the graphs library:

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}

\begin{document}
    \tikz \graph { A -- B -- C };
\end{document}

output of above code

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.