4

In the following tikz matrix of nodes, the red line isn't drawn on the (magic-2-2) node. How could I make it continuous from (magic-1-1) to (magic-3-3) nodes, drawn through the (“step” on) node (magic-2-2)?

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary {matrix}

\begin{document} \begin{tikzpicture} \matrix (magic) [matrix of nodes] { 1-1 & 1-2 & 1-3 \ 2-1 & 2-2 & 2-3 \ 3-1 & 3-2 & 3-3 \ }; \draw[red,->] (magic-1-1) |- (magic-2-2) -| (magic-3-3); \end{tikzpicture} \end{document}

enter image description here

Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85

2 Answers2

6

If you use just the name of a node as a coordinate specification TikZ will automatically choose a point on the node's border that is in the direction of the connecting line viewed from the node's center. If you want to have the line actually go through the node's center, you need to use the center anchor.

\draw[red,->] (magic-1-1) |- (magic-2-2.center) -| (magic-3-3);

You can use any anchor here since it will stop TikZ from automatically choosing a point on the border. (In this case, both west and east will lead to the same result.)

Code

\documentclass[tikz, convert={density=150}]{standalone}
\usetikzlibrary {matrix}
\begin{document}
\begin{tikzpicture}
\matrix (magic) [matrix of nodes]{
  1-1 & 1-2 & 1-3 \\
  2-1 & 2-2 & 2-3 \\
  3-1 & 3-2 & 3-3 \\
};
\draw[red,->] (magic-1-1) |- (magic-2-2.center) -| (magic-3-3);
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
4

Using @Qrrbrbirlbel's fantastic ext.paths.ortho library, you can draw the line in one go without the intermediate stop:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{matrix} \usetikzlibrary{ext.paths.ortho}

\begin{document} \begin{tikzpicture} \matrix (magic) [matrix of nodes] { 1-1 & 1-2 & 1-3 \ 2-1 & 2-2 & 2-3 \ 3-1 & 3-2 & 3-3 \ }; \draw[red,->] (magic-1-1) |-| (magic-3-3); \end{tikzpicture} \end{document}

enter image description here