# Draw arrows between elements inside math matrix

I want to make this,

without having to do the entire thing in TikZ. I tried this (pst-node), which sort of worked, but then I had to use the Basic Builder and I want to avoid that.

Also I tried to make the arrows with TikZ, but I wasn't able to shift the tikzpicture into the equation environment.

\documentclass[12pt,norsk, fleqn]{article}
\usepackage{amssymb, amsmath}
\begin{document}
\begin{equation*}
\begin{matrix}
x_1&y_1&z_1&x_1&y_1&z_1\\
x_2&y_2&z_2&x_2&y_2&z_2
\end{matrix}
\end{equation*}
\end{document}


Any ideas?

• What's the Basic Builder? – Bernard Aug 22 '17 at 19:05
• I'm using sublime text to develop the document. There's several ways to compile, one, that gives me at the time several compile-warnings and does not support makeindex, is the Basic Builder which simply runs xelatex and bibtex. – E. l4d3 Aug 23 '17 at 18:39

The question reads “without having to do the entire thing in TikZ”, so here is a solution that keeps the original matrix:

\documentclass[12pt,norsk, fleqn]{article}
\usepackage{amssymb, amsmath}

\usepackage{tikz}
\newcommand{\rn}[2]{%% "rn": "remember node"
\tikz[remember picture,baseline=(#1.base)]\node [inner sep=0] (#1) {$#2$};%
}

\begin{document}
%% The original matrix, but with commands to remember the nodes:
\begin{equation*}
\begin{matrix}
x_1 & \rn{11}{y_1} & \rn{12}{z_1} & \rn{13}{x_1} & \rn{14}{y_1} & z_1
\\
x_2 & \rn{21}{y_2} & \rn{22}{z_2} & \rn{23}{x_2} & \rn{24}{y_2} & z_2
\end{matrix}
\end{equation*}

%% Draw the arrows:
\begin{tikzpicture}[overlay,remember picture]
\draw [->] (11) -- (22);
\draw [->] (12) -- (23);
\draw [->] (13) -- (24);
\draw [->] (21) -- (12);
\draw [->] (22) -- (13);
\draw [->] (23) -- (14);
\end{tikzpicture}
\end{document}


Explanation: The command \rn puts its second argument in a tikz node. The option remember picture makes these nodes available even after the mini tikzpicture, i.e., after the equation. Later, another tikzpicture draws the arrows. The option overlay ensures that it takes no space and can draw on top of the matrix above.

It looks like before, but with the desired arrows:

tikz-cd can be used for this. Up-right arrows are drawn by \arrow[ur] and Down-right arrows are drawn by \arrow[dr]. Normal tikz options apply here. You can control row sep and column sep by specifying any lengths for them, or simply use [cramped, sep=small] as a fast key.

\documentclass[12pt,norsk, fleqn]{article}
\usepackage{amssymb, amsmath, tikz-cd}
\begin{document}

\begin{tikzcd}[cramped, sep=small]
x_1 & y_1\arrow[dr] & z_1\arrow[dr] & x_1\arrow[dr] & y_1 & z_1 \\
x_2 & y_2\arrow[ur] & z_2\arrow[ur] & x_2\arrow[ur] & y_2 & z_2
\end{tikzcd}

\end{document}


• More symmetric arrows would be nicer. – egreg Aug 23 '17 at 8:08

Without tikz.

\documentclass{article}
\usepackage{amssymb, amsmath,graphicx,tabstackengine}
\newcommand\crisscross{\ooalign{%
\rotatebox[origin=c]{30}{${-}\mkern-4mu{-}\mkern-4mu{\rightarrow}$}\cr
\rotatebox[origin=c]{-30}{${-}\mkern-4mu{-}\mkern-4mu{\rightarrow}$}}
}
\TABstackMath
\begin{document}
${}={}\tabbedCenterstack{ x_1&&y_1&&z_1&&x_1&&y_1&&z_1\\ &\phantom{\crisscross}&&\crisscross&&\crisscross&&\crisscross&&\phantom{\crisscross}&\\ x_2&&y_2&&z_2&&x_2&&y_2&&z_2}$
\end{document}


with pure tikz

\documentclass[12pt,norsk, fleqn]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
$\begin{tikzpicture} \matrix (m) [matrix of math nodes, nodes={inner sep=1pt}, column sep=2ex, row sep=2ex] { x_1&y_1&z_1&x_1&y_1&z_1\\ x_2&y_2&z_2&x_2&y_2&z_2\\ }; \draw[->] (m-1-2) edge (m-2-3) (m-1-3) edge (m-2-4) (m-1-4) edge (m-2-5) (m-2-2) edge (m-1-3) (m-2-3) edge (m-1-4) (m-2-4) edge (m-1-5); \end{tikzpicture}$
\end{document}


With pstricks:

\documentclass[12pt,norsk, fleqn]{article}
\usepackage{amssymb, amsmath, pgffor}
\usepackage{pst-node}%, auto-pst-pdf

\begin{document}

\begin{pspicture}
\begin{equation*}
\setlength{\arraycolsep}{8pt}
\begin{matrix}
x_1&\Rnode{A1}{y_1}&\Rnode{B1}{z_1}&\Rnode{C1}{x_1}&\Rnode{D1}{y_1}&z_1\\
x_2&\Rnode{A2}{y_2}&\Rnode{B2}{z_2}&\Rnode{C2}{x_2}&\Rnode{D2}{y_2}&z_2
\end{matrix}
\psset{arrowinset=0.12, arrows=->, linewidth=0.6pt, nodesepB=0.8pt}
\foreach \L/\M in {A1/B2, B1/C2, C1/D2}{\ncline{\L}{\M}}
\psset{nodesepB=-0.2pt}
\foreach \L/\M in {A2/B1,B2/C1, C2/D1}{\ncline{\L}{\M}}
\end{equation*}
\end{pspicture}

\end{document}