[Tex/LaTex] Drawing an arrow over a tabular with tikzmark

tikzmark

I'm trying to get lines to trace diagonally along the entries in a tabular using tikz and tikzmark but tikz isn't drawing on top of the text in the tabular. How can I fix this? I would like to keep using an appropriate mathy structure like tabular, not change to something pgf-specific, just draw a line on top, like the one that's used to prove countability of the rationals like here: https://plus.maths.org/issue47/features/macgregor/diagram3.gif but preferrably with an arrow head at the end. What's a good way to do that?

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{comment}
\usepackage{tikz}

\begin{document}

\begin{tabular}{c c c c c c}
$(1,1)$ & $(1,2)$ & $(1,3)$ & $(1,4)$ & $(1,5)$ & $\cdots$ \\
$(2,1)$ & $(2,2)$ & $(2,3)$ & $(2,4)$ & $(2,5)$ & $\cdots$ \\
$(3,1)$ & $(3,2)$ & $(3,3)$ & $(3,4)$ & $(3,5)$ & $\cdots$ \\
$(4,1)$ & $(4,2)$ & $(4,3)$ & $(4,4)$ & $(4,5)$ & $\cdots$ \\
$(5,1)$ & $(5,2)$ & $(5,3)$ & $(5,4)$ & $(5,5)$ & $\cdots$ \\
$\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ \\
\end{tabular}

\newcommand {\tikzmark}[2]{%
    \tikz[overlay,remember picture] \node (#1) {#2};
}

\begin{tabular}{c c c c c c}
\tikzmark{1-1}{$(1,1)$} & \tikzmark{1-2}{$(1,2)$} & \tikzmark{1-3}{$(1,3)$} & \tikzmark{1-4}{$(1,4)$} & \tikzmark{1-5}{$(1,5)$} & $\cdots$ \\
\tikzmark{2-1}{$(2,1)$} & $(2,2)$ & $(2,3)$ & $(2,4)$ & $(2,5)$ & $\cdots$ \\
\tikzmark{3-1}{$(3,1)$} & $(3,2)$ & $(3,3)$ & $(3,4)$ & $(3,5)$ & $\cdots$ \\
\tikzmark{4-1}{$(4,1)$} & $(4,2)$ & $(4,3)$ & $(4,4)$ & $(4,5)$ & $\cdots$ \\
\tikzmark{5-1}{$(5,1)$} & $(5,2)$ & $(5,3)$ & $(5,4)$ & $(5,5)$ & $\cdots$ \\
$\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ \\
\end{tabular}

\tikz[overlay,remember picture]
\draw [->,overlay,remember picture,xshift=20ex] (1-1) -- (2-1) -- (1-2) -- (1-3) -- (3-1) -- (4-1) -- (1-4) -- (1-5);

\end{document}

Best Answer

Since you want some math environment I would recommend array or matrix (from amsmath). The line can be drawn as a plot between the coordinates. I also did a slight change in \tikzmark command to get a better overview when writing the matrix.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{comment}
\usepackage{tikz}

\newcommand{\tikzmark}[2]{\tikz[remember picture] \node (#1-#2) {$(#1,#2)$};}
%%
\begin{document}

\begin{displaymath}
  \begin{matrix}
    \tikzmark{1}{1} & \tikzmark{1}{2} & \tikzmark{1}{3} & \tikzmark{1}{4} & \tikzmark{1}{5} & \cdots\\
    \tikzmark{2}{1} & \tikzmark{2}{2} & \tikzmark{2}{3} & \tikzmark{2}{4} & \tikzmark{2}{5} & \cdots\\
    \tikzmark{3}{1} & \tikzmark{3}{2} & \tikzmark{3}{3} & \tikzmark{3}{4} & \tikzmark{3}{5} & \cdots\\
    \tikzmark{4}{1} & \tikzmark{4}{2} & \tikzmark{4}{3} & \tikzmark{4}{4} & \tikzmark{4}{5} & \cdots\\
    \tikzmark{5}{1} & \tikzmark{5}{2} & \tikzmark{5}{3} & \tikzmark{5}{4} & \tikzmark{5}{5} & \cdots\\
    \vdots & \vdots & \vdots & \vdots & \vdots & \ddots
  \end{matrix}
\end{displaymath}

\begin{tikzpicture}[overlay,remember picture]
  \draw[->] plot coordinates {(1-1) (2-1) (1-2) (1-3) (3-1) (4-1) (1-4) (1-5) (5-1)};
\end{tikzpicture}

\end{document}

enter image description here

Related Question