1

Suppose that I want to create 3 tikz nodes named a, b and c.

The nodes should be positioned in the following relational manner:

First of all, a's east anchor should be connected to b's west anchor. Furthermore, b's east anchor should be connected to c's west anchor.

What is the correct way to do this?

I tried the following code (which doesn't work). I'm not sure what is wrong with my logic here.

\begin{tikzpicture}
\node[rectangle , draw](a){A};
\node[rectangle, draw , right of = a , anchor = west](b){B};
\node[rectangle , draw , left of = a , anchor = east](c){C};    
\end{tikzpicture}
4
  • You have to draw the connections: \draw[->] (a) to (b);
    – Sigur
    Commented Jan 24, 2023 at 16:55
  • 1
    Don't use right of=… but right=of … from the positioning library. Set either node distance=0 or use right=0 of a then (same is true for left of course) then you can drop the anchor key. Commented Jan 24, 2023 at 18:41
  • The older syntax doesn't select the reference's anchor which lead to a.center to be the reference and not a.west or a.east. And you can't change that behaviour without extra work. → Use positioning with a zero node distance (either explicitly before of or implicitly via node distsance). Commented Jan 24, 2023 at 18:44
  • You could also manually do [anchor=west] at (a.east) which is what right=0 of a will do automatically for you. Commented Jan 24, 2023 at 20:04

1 Answer 1

1

Here are two ways. Both produce the same output.

enter image description here

Using chains:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{chains}

\begin{document}

\begin{tikzpicture}[start chain, node distance=5mm]
\node[on chain, draw](a){A};
\node[on chain=going right, draw](b){B};
\node[on chain=going right, draw](c){C};
\end{tikzpicture}

\end{document}

Using positioning:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[node distance=5mm]
\node[draw](a){A};
\node[right=of a, draw](b){B};
\node[right=of b, draw](c){C};
\end{tikzpicture}

\end{document}

If you want to connect the nodes, you can add \draw(a)--(b)--(c); before the end of the tikzpicture. With chains, there is also the join option: \node[on chain=going right, draw, join]

enter image description here

You must log in to answer this question.

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