15

I'm trying to place nodes in a tabular to obtain image like this

desired

I have a command to create an overlay TikZ node like this

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

My question is: how can I reach corners of cell and add my node without manual adjustment every time I change cell?

MWE

\documentclass[border=5pt]{standalone}
\usepackage{tikz}

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

\begin{document}

\begin{tabular}{c|c|c}
12  &                            13                           &  32\\
\hline
24  &\tikzmark[xshift=-\tabcolsep]{A}\hfill 14 \hfill\tikzmark[xshift=\tabcolsep]{B}&  45\\
12  &                            13                           &  32\\
\hline
12  &                 very long long long text                &  32
\end{tabular}

\begin{tikzpicture}[remember picture,overlay]
\draw[red](A) to[out=5,in=175](B);
\end{tikzpicture}

\end{document}

output

  • 1
    Have you considered using the tikz matrix feature to set the table? This will make it easy to get the right coordinates. – Artemis Jan 16 '17 at 21:13
  • I need to add nodes in a tabular environment not in a tikz matrix – Salim Bou Jan 16 '17 at 21:17
13

The following example fixes some issues:

  • \coordinate instead of \node for \tikzmark avoids additional spacing of a node.
  • The default depth of a row in a tabular is set to 30 % of the baseline skip and multiplied with \arraystretch.

    The example defines the \RowDepth outside of the tabular, because inside the tabular, \baselineskip is reset to zero.

  • Package array is loaded to get a more reliable position of the rules. In a tabular, the vertical rules have its own space, if package array is loaded. Without the package, the vertical rules are sticking to the left and right cell and might even overprint the cell contents (for ridiculous large rule widths).

Example code:

\documentclass[border=5pt]{standalone}
\usepackage{array}
\usepackage{tikz}

\newcommand{\tikzmark}[2][]{%
  \tikz[remember picture,overlay]\coordinate[#1](#2);%
}

\begin{document}

\edef\RowDepth{\the\dimexpr\arraystretch\dimexpr.3\baselineskip\relax\relax}
\begin{tabular}{c|c|c}
12  &  13  &  32  \\
\hline
24  &
  \tikzmark[xshift=-\tabcolsep, yshift=-\RowDepth]{A}%
  \hfill 14\hfill
  \tikzmark[xshift=\tabcolsep, yshift=-\RowDepth]{B}&
  45\\
\hline
12  &  13 &  32\\
\hline
12  &  very long long long text &  32
\end{tabular}

\begin{tikzpicture}[remember picture,overlay]
\draw[red](A) to[out=5,in=175](B);
\end{tikzpicture}

\end{document}

Result

An alternative approach

The vertical position can be retrieved from a coordinate right below the row:

\\% end of the row
\noalign{\tikzmark{C}}% out of alignment, but correct vertical position

Then the perpendicular coordinate system can be used to address the start and end points: (A|-C) and (B|-C)

Full example:

\documentclass[border=5pt]{standalone}
\usepackage{array}
\usepackage{tikz}

\newcommand{\tikzmark}[2][]{%
  \tikz[remember picture,overlay]\coordinate[#1](#2);%
}

\begin{document}

\begin{tabular}{c|c|c}
12  &  13  &  32  \\
\hline
24  &
  \kern-\tabcolsep\tikzmark{A}%
  \hfill 14\hfill
  \tikzmark{B}\kern-\tabcolsep &
  45\\
\noalign{\hbox{\tikzmark{C}}}%
\hline
12  &  13 &  32\\
\hline
12  &  very long long long text &  32
\end{tabular}

\begin{tikzpicture}[remember picture,overlay]
\draw[red](A|-C) to[out=5,in=175](B|-C);
\end{tikzpicture}

\end{document}

This method deals well with rows, where the depth is not easily known and would have to be measured otherwise.

  • Awesome idea \noalign{\tikzmark{C}} but how can avoid out of alignement ? why this big space on the right appear ? – Salim Bou Jan 17 '17 at 6:01
  • Thanks Heiko Oberdiek the problem can be solved with \noalign{\hbox to 0pt{\tikzmark{C}}}. – Salim Bou Jan 17 '17 at 6:09
  • @SalimBou Thanks.\tikzmark would start a new paragraph with full line width. Answer updated. – Heiko Oberdiek Jan 17 '17 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.