1

I have nodes of differing heights next to each other and would like to draw a path (arrows) between them. However, I would like the path to be horizontal all the way. I managed to achieve this between the first and second node in my example, but I don’t know how I could get the line between the second and third to be in the same line as the first arrow (as if all the arrows have been drawn in one long horizontal segmented line).

\documentclass[tikz,border=1mm]{standalone}

\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[every node/.style=draw] \node (a) {(\alpha)}; \node[right=of a,yshift=-2mm,minimum height=1cm] (b) {(\beta)}; \node[right=of b,minimum height=1cm] (c) {(\gamma)};

\draw[->] (a) edge (a -| b.west)  (b) edge (c);
% \draw[->] (a) edge (a -| b.west)  (a -| b.east) edge (b -| c.west);

\end{tikzpicture}

\end{document}

(The commented line is one of my failed attempts.)

output of MWE


As a side question, what is the recommended way to format tikz code when keeping a (eg) 72 haracter line limit? Where should I break the line? Here are a few examples:

\node[something=foo,some other thing] (averylongnodename)
    {Node text};

\node[% something=foo, % <--+ some other thing % <--+--- This arrangement seems to work nice with a lot of keys ] (averylongnodename) {Node text};

\node[something foo,some other thing] (averylongnodename) {Node text};

% etc

(These examples weren’t fitted for 72 characters and are for illustration only.)

bp99
  • 605

2 Answers2

2

You almost done. You only need to add definition of edge and remove -> from draw command:

\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary{arrows.meta, % for arrows heads
                positioning}

\begin{document} \begin{tikzpicture}[ every node/.style=draw, every edge/.style = {draw, -Straight Barb} % <--- ] \node (a) {(\alpha)}; \node (b) [right=of a,yshift=-2mm,minimum height=1cm] {(\beta)}; \node (c) [right=of b,minimum height=1cm] {(\gamma)};

\draw   (a) edge (a -| b.west)  
        (a -| b.east) edge (a -| c.west);
\end{tikzpicture}

\end{document}

output

Edit: With definition every edge/.style = {draw, -Straight Barb} is determined that every edge in picture, i.e. each line between nodes (or given coordinates) is drawn with arrowhead Straight Barb. By this definition options -> are not needed anymore.

Each edge is considered as separate path between nodes (coordinates), therefore command \draw[->] only add extra arrowhead (to not existed path) which confuse tikz:

enter image description here

since for it there is no defined path. Instead of \draw command in above MWE, you can use \path.

Zarko
  • 296,517
  • Thanks, although I am not sure why you changed the arrow heads :) Could you please explain to me why removing the -> from the draw command setting it on the edges works, but using \draw[->] doesn’t? – bp99 Dec 04 '20 at 19:33
  • @bp99, this you can seemple see with test. First you test my answer as it is, then try with \draw[->] and observe difference. With every edge/.style = {draw, -Straight Barb} is determined that every edge has arrowhead Straight Barb, so use of edges with \draw[->] confuse tikz. – Zarko Dec 04 '20 at 20:01
  • @bp99 The short answer is that each edge it treated as a separate path. See https://tex.stackexchange.com/a/314306/24974 for a much more thorough explanation. You could also use \draw[->] (a) edge (a -| b.west) (a -| b.east) -- (a -| c.west); which would apply the arrowhead to the edge part as well as the -- part. – erik Dec 04 '20 at 20:17
1

You've almost done the whole job, is this what you asked for ?

\begin{tikzpicture}[every node/.style=draw]
    \node (a) {\(\alpha\)};
    \node[right=of a,yshift=-2mm,minimum height=1cm] (b)
        {\(\beta\)};
    \node[right=of b,minimum height=1cm] (c) {\(\gamma\)};
    \node[coordinate](next)at(a-|b.north east){};
    \draw (a) edge[->] (a-|b.north west) (next)edge[->](next-|c.north west);
    % or simply (next)--(next-|c.north west)
    % \draw[->] (a) edge (a -| b.west)  (a -| b.east) edge (b -| c.west);
\end{tikzpicture}

enter image description here

When it comes to line limit character, I don't know any rule about that, it should be ok as long as it observes the laws of TeX editing, then you should avoid paragraph or blank lines that might cause compilation errors.

mxnc baud
  • 501
  • Thanks, this works nicely. I am still wondering if there is a way to do this without adding a new coordinate, but I can’t afford the to dwell on this at the moment :-) I also found that your way of writing the path—(a) edge[->] (b) (c) edge[->] (d) works, but when I do \draw[->] (a) edge (b) (c) edge (d) the second edge draws the arrow head twice for some reason. – bp99 Dec 04 '20 at 12:53