How to draw such arrows in the table?
3 Answers
Next time, please provide a small example document people can use as the basis for an answer. In this case, for example, since your question is about the arrows, you should provide the code for the table so that people can concentrate on answering the question rather than having to construct a suitable table first.
One way is to use tikzmark to mark coordinates within the table which you can then use to overlay a tikzpicture containing the arrows.
For example, the following code illustrates the two kinds of arrows you need: curved arrows from one row to another within a column and straight arrows between cells in consecutive columns:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{tabular}{cc}
14\tikzmark{a} & 78 \
71\tikzmark{b} & \tikzmark{c}93 \
\end{tabular}
\begin{tikzpicture}[overlay, remember picture, shorten >=.5pt, shorten <=.5pt, transform canvas={yshift=.25\baselineskip}]
\draw [->] ({pic cs:a}) [bend left] to ({pic cs:b});
\draw [->] ([yshift=.75pt]{pic cs:a}) -- ({pic cs:c});
\end{tikzpicture}
\end{document}

-
I just ran this exact code, but it appears the tikzpicture yshift is not applied (the one for the second arrow is): output -- has something changed since this answer was posted? – meide Sep 19 '17 at 04:51
-
Do you compile at least twice? – Zarko Apr 05 '21 at 12:21
If you want to use a matrix instead of tabular, you can get inspiration from the following code:
% Code by Anonymous User from https://www.latex4technics.com/?note=1w9f
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={rectangle,text width=3em,align=center},
text depth=1.25ex,
text height=2.5ex,
nodes in empty cells
},
row 1/.style={nodes={fill=green!10,text depth=0.4ex,text height=2ex}},
row 6/.style={nodes={text depth=0.4ex,text height=2ex}},
column 1/.style={nodes={fill=green!10}},
}
\begin{document}
\begin{tikzpicture}
% the matrix entries
\matrix (mat) [table]
{
& $D_1$ & $D_2$ & $D_3$ & $D_4$ & foo \\
$O_1$ & 50 & 0 & & & 50 \\
$O_2$ & & 60 & & & 60 \\
$O_3$ & & 10 & 30 & 10 & 50 \\
$O_4$ & & & & 50 & 50 \\
bar & 50 & 70 & 30 & 60 & 210 \\
};
% the matrix rules
\foreach \x in {1,...,5}
{
\draw
([xshift=-.5\pgflinewidth]mat-\x-1.south west) --
([xshift=-.5\pgflinewidth]mat-\x-6.south east);
}
\foreach \x in {1,...,5}
{
\draw
([yshift=.5\pgflinewidth]mat-1-\x.north east) --
([yshift=.5\pgflinewidth]mat-6-\x.south east);
}
% the arrows
\begin{scope}[shorten >=7pt,shorten <= 7pt]
\draw[->] (mat-2-2.center) -- (mat-2-3.center);
\draw[->] (mat-2-3.center) -- (mat-3-3.center);
\draw[->] (mat-3-3.center) -- (mat-4-3.center);
\draw[->] (mat-4-3.center) -- (mat-4-4.center);
\draw[->] (mat-4-4.center) -- (mat-4-5.center);
\draw[->] (mat-4-5.center) -- (mat-5-5.center);
\draw[->] (mat-5-5.center) -- (mat-6-6.center);
\end{scope}
\end{tikzpicture}
\end{document}
You can experiment with the code here: https://www.latex4technics.com/?note=1w9f
- 2,040
You can do that with {NiceArray} of nicematrix. This environment is similar to the classical environment {array} (of array) but constructs PGF/Tikz nodes under the cells. Then, it's possible to use these nodes with Tikz.
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\renewcommand{\arraystretch}{1.5}
$\begin{NiceArray}{*{9}{c}}
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \
& 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 \
& 19 & & 21 & 22 & 23 & 24 & 25 & 26 \
& 28 & & 30\rlap{${}=s$} & & 32 & 33 & 34 & 35 \
& 37 & & 39 & & 41 & & 43 & 44 \
& 46 & & 48 & & 50 & & 52 & \
& 55 & & 57 & & 59 & & 61 & \
& & & 66 & & 68 & & 70 & \
& & & & & 77 & & 79\rlap{${}=t=30+3a+2b$} \
& & & & & & & 88 & \
\CodeAfter
\begin{tikzpicture}
\draw (1-1) circle (2mm)
(2-3) circle (2mm)
(3-5) circle (2mm)
(4-7) circle (2mm)
(5-9) circle (2mm)
(7-2) circle (2mm)
(8-4) circle (2mm)
(9-6) circle (2mm)
(10-8) circle (2mm) ;
\begin{scope} [->]
\draw (4-4.east) to [bend left=45] (5-4.east) ;
\draw (5-4.east) to [bend left=45] (6-4.east) ;
\draw (6-4.east) to [bend left=45] (7-4.east) ;
\draw (7-4) -- (8-6) ;
\draw (8-6) -- (9-8) ;
\end{scope}
\end{tikzpicture}
\end{NiceArray}$
\end{document}
You need several compilations (because nicematrix uses PGF/Tikz nodes).
- 40,250

