4

I have used code from the answer discussed here. The resulting arrows are not aligned with each other. For example the arrow from "12 buckets" to "5" and from "5" to "7" should be collinear. Similarly "7" to "5" and "5" to "10". Is there any way to align them so that final output looks neater?

enter image description here

MWE

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usetikzlibrary{arrows}
\usepackage{booktabs}

\begin{document}



\begin{tabular}{p{2cm}lllllllll}
\toprule
9 Buckets & 0 & 7\tikzmark{a3} & 7\tikzmark{a6} & 2 & 2\tikzmark{a9} & 0 & 9\tikzmark{a11} & 6 & 6 \\
5 Buckets & 5\tikzmark{a2} & 5\tikzmark{a4} & 0 & 5\tikzmark{a7} & 0 & 2\tikzmark{a10} & 2 & 5\tikzmark{a12} & 0 \\
12 Buckets\tikzmark{a1} & 7 & 0 & 5\tikzmark{a5} & 5 & 10\tikzmark{a8} & 10 & 1 & 1 & 6\tikzmark{a13} \\ 
\bottomrule
\end{tabular}

\begin{tikzpicture}[overlay, remember picture, 
          shorten >=8pt, shorten <=3pt, 
          transform canvas={yshift=.25\baselineskip}]
    \draw [-stealth, red, thick] ({pic cs:a1}) -- ({pic cs:a2});
    \draw [-stealth, red, thick] ({pic cs:a2}) -- ({pic cs:a3});
    \draw [-stealth, red, thick] ({pic cs:a4}) -- ({pic cs:a5});
    \draw [-stealth, red, thick] ({pic cs:a6}) -- ({pic cs:a7});
    \draw [-stealth, red, thick] ({pic cs:a7}) -- ({pic cs:a8});
    \draw [-stealth, red, thick] ({pic cs:a9}) -- ({pic cs:a10});
    \draw [-stealth, red, thick] ({pic cs:a10}) -- ({pic cs:a11});
    \draw [-stealth, red, thick] ({pic cs:a11}) -- ({pic cs:a12});
    \draw []-stealth, red, thick] ({pic cs:a12}) -- ({pic cs:a13}); 
\end{tikzpicture}
  

\end{document}
1
  • 2
    I've taken the liberty of replacing all instances of \[ and \] in your code with [ and ], resp.
    – Mico
    Commented May 21 at 11:42

1 Answer 1

7

I suggest you (a) change the column type of the first column from p to l, (b) change the column type of the nine data columns from l to c, and (c) use the \makebox device (see below) to reduce the amount of space taken up by the numbers 10 in the third row from their normal, i.e., two-digit width to the width of a single digit.

The effect of these changes is to make the width of each intercolumn space equal to 2\tabcolsep, or 12pt by default in the article document class. This, in turn, makes the arrows line up as you desire.

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{tikzmark,arrows}
\usepackage{booktabs}
\usepackage{calc} % for '\widthof' macro
\begin{document}

\begin{tabular}{@{}r *{9}{c}@{}}
\toprule
9 Buckets & 0 & 7\tikzmark{a3} & 7\tikzmark{a6} & 2 & 2\tikzmark{a9} & 0 & 9\tikzmark{a11} & 6 & 6 \\
5 Buckets & 5\tikzmark{a2} & 5\tikzmark{a4} & 0 & 5\tikzmark{a7} & 0 & 2\tikzmark{a10} & 2 & 5\tikzmark{a12} & 0 \\
12 Buckets\tikzmark{a1} & 7 & 0 & 5\tikzmark{a5} & 5 & 
   \makebox[\widthof{0}][c]{10}\tikzmark{a8} & 
   \makebox[\widthof{0}][c]{10} & 
   1 & 1 & 6\tikzmark{a13} \\ 
\bottomrule
\end{tabular}

\begin{tikzpicture}[overlay, remember picture, 
  shorten >=8pt, 
  shorten <=3pt, 
  transform canvas={yshift=.33\baselineskip}]
    \draw [-stealth, red, thick] ({pic cs:a1}) -- ({pic cs:a2});
    \draw [-stealth, red, thick] ({pic cs:a2}) -- ({pic cs:a3});
    \draw [-stealth, red, thick] ({pic cs:a4}) -- ({pic cs:a5});
    \draw [-stealth, red, thick] ({pic cs:a6}) -- ({pic cs:a7});
    \draw [-stealth, red, thick] ({pic cs:a7}) -- ({pic cs:a8});
    \draw [-stealth, red, thick] ({pic cs:a9}) -- ({pic cs:a10});
    \draw [-stealth, red, thick] ({pic cs:a10}) -- ({pic cs:a11});
    \draw [-stealth, red, thick] ({pic cs:a11}) -- ({pic cs:a12});
    \draw [-stealth, red, thick] ({pic cs:a12}) -- ({pic cs:a13}); 
\end{tikzpicture}
  
\end{document}
1
  • 1
    Thanks for the suggestions on making the intercolumn space equal in the table.
    – Damitr
    Commented May 21 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .