Drawing vertical arrow to a table column

Here is the obligatory tikz solution which then gives you all the flexibility inherent in tikz in terms of adjusting the arrow tips, colors, line styles, etc.

Note:

  • As with all \tikzmark solutions, this does require two runs. First one to determine the locations, and the second to do the drawing.

  • The \tikzmark is from Adding a large brace next to a body of text.


Automated Solution:

In the earlier manual solution below, each column for which the arrow was to be drawn needed to be marked with an individual \tikzmark. This makes the table a bit harder to read.

In this solution, I have used the the collcell package to define a new column type C to replace the c columns (as well as R, L columns). When this column type is used the column is automatically marked with a \tikzmark numbered 1,...,<numberof columns>. So, the table just need to look like:

\begin{tabular}{|CCCCCCLR|}\hline
  1 & 2 & 3 & 4 & wide text & 6 & 7 & 008 \\ \hline
  1 & 2 & 3 & 4 & 5 & 6 & wide text & 8 \\ \hline
\end{tabular}

After the table to you can use the \DrawArrow macro to draw the arrow to any of the columns in the table by using the column number as the paramater to \DrawArrow. For example the \DrawArrow[-stealth]{3} produces the third black arrow:

enter image description here

Notes:

  • This automated solution assumes that you will draw at least one arrow whenever the C, R, or L column types are used. Without this the counter which numbers the columns would not be reset. Otherwise you could just manually call \ResetAddTikzMark before the start of each table, and this would be a non-issue.
  • Only the special columns types are numbered, so if you use CrC, then the first C column would be numbered 1, and the third column would be numbered 2 (since it is the 2nd column that gets the automatic \tikzmark.

Further Enhancements:

  • Note that column 7 is using the L column type. Since the first column's text is used to compute the width (to center the arrow), the arrow points to the center of the first column's text, which may not be the desired outcome. If this is not clear, see column 8 which is using the R column type.

    One way (untested) to adjust his would be to ensure that that columns text was wrapped in a \makebox[\widthof{<widest-text-in-column>}]{} type of macro.


Manual Solution:

In this version each of the top rows where you want to add the arrows needs to be marked with a \tikzmark:

enter image description here

References:

  • Solution is adapted from Draw a line through one column of a matrix

Code: Automated Solution

\documentclass{article}
\usepackage{collcell}
\usepackage{tikz}
\usetikzlibrary{calc}

\newlength{\TextWidth}
\newcommand{\tikzmark}[2]{%
    \settowidth{\TextWidth}{#2}%
    \makebox[\TextWidth]{%
        \tikz[overlay,remember picture,baseline] 
            \node [anchor=base] (#1) {#2};
    }%
}

\newcommand{\DrawArrow}[2][]{%
  \ResetAddTikzMark% Reset counter for next table
  \begin{tikzpicture}[overlay,remember picture]
    \draw[-latex, thick,#1] ($(#2.north)+(0,1.0cm)$) -- (#2.north);
  \end{tikzpicture}
}

\newcounter{ColumnCounter}
\newcommand*{\ResetAddTikzMark}{\setcounter{ColumnCounter}{0}}
\newcommand*{\AddTikzmark}[1]{%
    \stepcounter{ColumnCounter}%
    \tikzmark{\arabic{ColumnCounter}}{#1}%
}%

\newcolumntype{C}{>{\collectcell\AddTikzmark}c<{\endcollectcell}}%
\newcolumntype{R}{>{\collectcell\AddTikzmark}r<{\endcollectcell}}%
\newcolumntype{L}{>{\collectcell\AddTikzmark}l<{\endcollectcell}}%

\begin{document}
\vspace{2.0cm}
\begin{tabular}{|CCCCCCLR|}\hline
  1 & 2 & 3 & 4 & wide text & 6 & 7 & 008 \\ \hline
  1 & 2 & 3 & 4 & 5 & 6 & wide text & 8 \\ \hline
\end{tabular}
%
% Now we can draw the arrows to each of the `C` columns
\DrawArrow{1}
\DrawArrow[->]{2}
\DrawArrow[-stealth]{3}
\DrawArrow[red]{4}
\DrawArrow[blue, densely dotted]{5}
\DrawArrow[brown, thin, densely dashed]{6}
\DrawArrow[violet, ultra thick, dashed]{7}
\DrawArrow[magenta, thick, -stealth]{8}
\end{document}

Code: Manual Solution

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}

\newcommand{\DrawArrow}[2][]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[-latex, thick,#1] ($(#2.north)+(0,1.0cm)$) -- (#2.north);
  \end{tikzpicture}
}

\begin{document}
\begin{tabular}{|*{8}{c|}}\hline
  \tikzmark{A}{1} & \tikzmark{B}{2} & \tikzmark{C}{3} & \tikzmark{D}{4} & 5 & 6 & 7 & 8 \\ \hline
  1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline
\end{tabular}
\DrawArrow{A}
\DrawArrow[->]{B}
\DrawArrow[red]{C}
\DrawArrow[blue, dotted]{D}
\end{document}

You can use \multicolumn:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{tabular}{*{4}{|c}|}
\multicolumn{1}{c}{$\downarrow$} \\
\hline
A & B & C & D \\
\hline
\end{tabular}

\end{document}

enter image description here


You need to place the arrow in a \multicolumn to remove any of the tabular vertical rules.

enter image description here

\documentclass{article}
\begin{document}
\begin{tabular}{|*{8}{c|}}
  \multicolumn{1}{c}{$\downarrow$} \\ \hline
  1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \hline
  %...
\end{tabular}
\end{document}

Tags:

Arrows

Tables