[Tex/LaTex] Table with arrows between cells

arrowspositioningtablestabularxtikz-pgf

I want to create a table with arrows between some cells, for which I've used tabularx and TkiZ. I have the following problems:

  • Text in cells aligned vertically to the center and at the same heigh (TikZ nodes are not inline with normal cells text in a same row).
  • Inability to align to the center with \begin{center}•\end{center}.
  • I don't know how to fix the size of the cells.

This is what I currently have:

Code

\documentclass{article}
\usepackage{tabularx}
\usepackage{tikz}
\tikzset{every picture/.style={remember picture}}
\begin{document}
\begin{tabularx}{\textwidth}{r|c|c|c|c|l}
    &$D_1$&$D_2$&$D_3$&$D_4$&foo\\ \cline{1-6}
    $O_1$&\tikz[baseline]{\node (a11) {50};}&\tikz[baseline]{\node (a12) {0};}&&&50\\[2em] \cline{1-6}
    $O_2$&&\tikz[baseline]{\node (a22) {60};}&&&60\\[2em] \cline{1-6}
    $O_3$&&\tikz[baseline]{\node (a32) {10};}&\tikz[baseline]{\node (a33) {30};}&\tikz[baseline]{\node (a34) {10};}&50\\[2em] \cline{1-6}
    $O_4$&&&&\tikz[baseline]{\node (a44) {50};}&50\\[2em] \cline{1-6}
    bar&50&70&30&60&210\\
\end{tabularx}
\begin{tikzpicture}[overlay]
    \path[thick,->] (a11) edge (a12);
    \path[thick,->] (a12) edge (a22);
    \path[thick,->] (a22) edge (a32);
    \path[thick,->] (a32) edge (a33);
    \path[thick,->] (a33) edge (a34);
    \path[thick,->] (a34) edge (a44);
\end{tikzpicture}
\end{document}

Output

Besides, I get the following warnings:

Underfull \hbox (badness 10000) in alignment at lines 13–13

Overfull \hbox (15.0pt too wide) in paragraph at lines 13–22

Best Answer

Another possible solution where the OP code is used but slightly modified. The underfull problem is taken care by adding an X column in the last column so that \textwidth is satisfied.

The OP's tikz struture in the cell is redefined as tikzmark taking two arguments. and connected by \link macro.

Create a new column type M with horizontal and vertical centering:

\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{M}{>{\centering\arraybackslash}m{1cm}}

enter image description here

Code

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tabularx}
\usepackage{tikz}
%\tikzset{every picture/.style={remember picture}}

\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{M}{>{\centering\arraybackslash}m{1cm}}

\newcommand\tikzmark[2]{%
\tikz[remember picture,baseline] \node[inner sep=2pt,outer sep=0] (#1){#2};%
}

\newcommand\link[2]{%
\begin{tikzpicture}[remember picture, overlay, >=stealth, shift={(0,0)}]
  \draw[->] (#1) to (#2);
\end{tikzpicture}%
}

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{M|M|M|M|M|MX}
         &$D_1$           &$D_2$           &$D_3$    &$D_4$& foo  &\\ \cline{1-6}
    $O_1$&\tikzmark{a}{50}&\tikzmark{b}{0} &         &     & 50   &\\[2em] \cline{1-6}
    $O_2$&                &\tikzmark{c}{60}&         &     &60 &\\[2em] \cline{1-6}
    $O_3$&                &\tikzmark{d}{10}&\tikzmark{e}{30}&\tikzmark{f}{10}&50 &\\[2em] \cline{1-6}
    $O_4$&                &                &         &\tikzmark{g}{50}&{50} &\\[2em] \cline{1-6}
    bar&50&70&30&60&210  \\ 
\end{tabularx}

\link{a}{b}
\link{b}{c} 
\link{c}{d}
\link{d}{e} 
\link{e}{f} 
\link{f}{g}
\end{document}