4

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}
2

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

| improve this answer | |
4

In your code, the lines just connect your nodes (x,y). To connect the lines, you have to draw them center to center:

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

The result would be:enter image description here

It's not that nice aligned. If you don't have a specific reason to use tabular, i would recommend array here:

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


\newcommand {\tikzmark}[2]{%
    \tikz[overlay,remember picture] \node (#1) {#2};}
\begin{document}
\[
\begin{array}{*6{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{array}
\]


\[
\begin{array}{*6{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)} & \tikzmark{2-2}{(2,2)} & \tikzmark{2-3}{(2,3)} & (2,4) & (2,5) & \cdots \\
\tikzmark{3-1}{(3,1)} & \tikzmark{3-2}{(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{array}
\]

\tikz[overlay,remember picture]
\draw[->,overlay,remember picture] (1-1.center) -- (2-1.center) -- (1-2.center) -- (1-3.center) -- (2-2.center) -- (3-1.center) -- (4-1.center) -- (3-2.center) -- (2-3.center) -- (1-4.center) -- (1-5.center);

\end{document}

The result is much nicer: enter image description here

Sadly it does not solve the problem of the unequally spaced entries completly. I Think that has something todo with making some entries nodes while some are not, but I'm not sure of that.

| improve this answer | |
  • 1
    \tikzmark inserts it's argument in text mode so you should switch to math mode again: \tikzmark{1-1}{$(1,1)$} etc. – Ulrike Fischer Jul 12 '16 at 7:00
  • Oh thanks! i didn't knew that! The lines are aligned now, but there is still the unequal spacing between the columns... – audiophiel Jul 12 '16 at 7:31
  • 1
    The misalignment is probably caused by the overlay in \tikzmark. It essentially means the node should take no space, and therefore the first column is more narrow. – StefanH Jul 12 '16 at 8:45
  • Thanks, I went with the other answer because of the alignment issues, but this one helped me understand some things, like I didn't know about .center. – user109923 Jul 12 '16 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.