# How to draw these images with TikZ?

I had once the need to use these graphics:

It seemed to be a simple table, but I was not able to

• draw randomly while crossing table lines, but with respect to the cell's coordinates (like in the first picture)
• set an offset to distinguish the continuous line from the dotted line (second picture)

I do not have to use these graphics now, but I found that it annoyed me not knowing how to do this. I am sure one could draw them completely within TikZ, but perhaps it is possible to marry TikZ with a table to not being bothered with a complicated calculation of coordinates.

Could anyone give a hint or a solution? My TikZ knowledge is rather small and merely of theoretical nature.

• Doing everything in TikZ is probably less fuzz than mixing in a tabular, a TikZ \matrix might be useful. As an addendum, I know the following isn't that helpful, but at a basic level the diagrams are quite simple. If you know how to draw a line between points and how to place text at a specified point, you can do the bulk of them, so a lot of TikZ knowledge isn't needed. (Of course, some knowledge could make it easier to do.) Commented Sep 28, 2019 at 10:57
• The first picture can be obtained e.g. with \documentclass[tikz,border=3mm]{standalone} \begin{document} \begin{tikzpicture} \draw (-2,1) -- (4.5,1) (-1,2) -- (-1,-4.5); \foreach \X [count=\Y starting from 0] in {\mu=0,1,2,3,\cdots} {\node[above=0.5ex] at (\Y,1) {$\X$};} \foreach \X [count=\Y starting from 0] in {\nu=0,1,2,3,\cdots} {\node[left=0.5ex] at (-1,-\Y) {$\X$};} \draw[mark=*] plot coordinates {(0,0) (1,0) (2,0) (2,-1) (3,-1) (3,-2) (4,-2) (4,-3)}; \end{tikzpicture} \end{document} ...
– user194703
Commented Sep 28, 2019 at 11:22
• It's wonderful to now have two different approaches. Thanks! Commented Sep 29, 2019 at 15:14

Here is one possibility using a \matrix. There are some comments in the code, ask if anything is unclear.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

% hack for style of empty cells from https://tex.stackexchange.com/a/386805/
\tikzset{empty node/.style={coordinate}}
\makeatletter
\def\tikz@lib@matrix@empty@cell{\iftikz@lib@matrix@empty\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\begin{document}
\begin{tikzpicture}

\matrix [
% with the following option each cell becomes a node, set in math mode
% the nodes are named automatically as <matrix name>-<row number>-<column number>
matrix of math nodes,
nodes in empty cells, % add cells also to empty nodes
nodes={minimum width=1cm, minimum height=1cm, font=\strut} % set style of nodes
] (m) % give the matrix the name m
{
& \mu = 0 & 1 & 2 & 3 & \dots \\
\nu = 0 & & & & & \\
1 & & & & & \\
2 & & & & & \\
3 & & & & & \\
\vdots & & & & &  \\
};

% for explanation of |- syntax: https://tex.stackexchange.com/a/401429/
% used to ensure horizontal/vertical lines
\draw (m-1-2.north west) -- (m-1-2.north west |- m-6-1.south east);
\draw (m-2-1.north west) -- (m-2-1.north west -| m-1-6.south east);

% due to the automatic node naming mentioned above, m-1-2 is the
% node in the first row, second column of the matrix

% a plot is an easy way of drawing a line with dots
\draw plot[mark=*] coordinates {(m-2-2)(m-2-3)(m-2-4)(m-3-4)(m-3-5)};

% for explanation of |- syntax: https://tex.stackexchange.com/a/401429/
% using that you only need to specify every other corner on the path
\draw (m-3-2) |- (m-4-3) |- (m-6-6);

% draw shifted, dotted line
% transform canvas idea from https://tex.stackexchange.com/a/180669/
\draw [dotted, transform canvas={shift={(5pt,5pt)}}] (m-3-2) |- (m-4-3) |- (m-6-6);

\end{tikzpicture}
\end{document}


• Very interesting. I did not even know that a matrix could have this kind of arguments. Thanks! Commented Sep 29, 2019 at 15:14

With {NiceArray} of nicematrix and Tikz to draw the broken lines with the Tikz nodes created by nicematrix.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

$\begin{NiceArray}{c|ccccc} & \mu = 0 & 1 & 2 & 3 & \cdots \\ \hline \nu = 0 \\ 1 \\ 2 \\ 3 \\ \vdots \CodeAfter \tikz [radius=2pt,every circle/.style = fill] \draw plot [mark = *] coordinates {(2.5-|2.5)(2.5-|3.5)(2.5-|4.5)(3.5-|4.5)(3.5-|5.5)(4.5-|5.5)(4.5-|6.5)(5.5-|6.5)} ; \end{NiceArray}$

$\begin{NiceArray}{c|ccccccc} & \mu = 0 & 1 & 2 & 3 & \cdot & \cdot & \cdot \\ \hline \nu = 0 \\ 1 \\ 2 \\ \cdot \\ \cdot \\ \cdot \\ \CodeAfter \begin{tikzpicture} \draw (2.5-|2.5) -- (2.5-|4.5) |- (3.5-|5.5) |- (4.5-|6.5) |- (5.5-|7.5) ; \draw (2.5-|2.5) -- (5.5-|2.5) -| (6.5-|3.5) -| (7.5-|4.5) ; \draw [dotted] ([xshift=1mm]2.5-|2.5) |- (3.5-|3.5) |- (4.5-|4.5) |- (5.5-|5.5) |- (6.5-|6.5) ; \end{tikzpicture} \end{NiceArray}$

\end{document}