4

Is it possible to put overlay tikz node on the crossing rules of a tabular. I tried, but the remember picture part doesn't work.

\documentclass[a4paper]{article}

\usepackage{array,tabularx,tikz,ragged2e,siunitx,xparse,hhline}
\usetikzlibrary{calc}

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

\begin{document}

\begin{tabular}{cc}\hhline{\tikzmark{A}--}
A&B \\\hhline{-\tikzmark{B}-}
\end{tabular}

\begin{tikzpicture}[overlay,remember picture]
\fill (A) rectangle (B) ;

\end{tikzpicture}

\end{document}
Tarass
  • 16,912
  • https://tex.stackexchange.com/questions/134263/how-to-set-tikzmarks-with-noalign-at-the-edge-of-tabular-rows – Ulrike Fischer Feb 13 '18 at 21:27
  • 1
    To reach the edge is easy, the problem in to reach the corner, that means to know height and depth of the tabular box. Even https://tex.stackexchange.com/questions/349045/add-tikz-node-at-corner-of-a-tabular-cell is depending of what is arround the box on the whole row. – Tarass Feb 13 '18 at 21:32
  • 3
    You can get the y-coordinates (top and bottom) with \noalign{\pgfmark{top}}. The left and right is normally quite easy to get by putting a mark in a cell or before/after the tabular. Then you can calculate the coordinates with intersections. – Ulrike Fischer Feb 13 '18 at 21:36
  • Possibly related: https://tex.stackexchange.com/questions/336782/how-to-align-adjust-colored-frames-with-tables-lists-with-tikz –  Feb 13 '18 at 23:30
  • Looking over the hhline documentation, it says nothing about inserting macros in with the keys =-~|:#tb*. There appears to be no equivalent to @{}. – John Kormylo Feb 14 '18 at 17:20
  • I looked in the hhline package documentation, but I'm not good enought to guess where insert tikzmark in the main loop. – Tarass Feb 14 '18 at 17:33

2 Answers2

3

We know the minimum height and depth of one row, but that does not give us the height and depth of a particular row as it may contain large entries. If we create a row containing nothing but tikzmarks, then we can use a negative vskip to overlay it with the next row.

Note that the @{} generates multiple coordinates with same name. Only the last one counts.

A tikz matrix would have been easier.

\documentclass[a4paper]{article}

\usepackage{array,tabularx,tikz,ragged2e,siunitx,xparse,hhline}% only tikz is needed here
\usetikzlibrary{calc}

\newcommand{\tikzmark}[2][0pt]% #1 = y offset (optional), #2 = coordinate name
{\tikz[overlay,remember picture]\coordinate (#2) at (0pt,#1);}

\newcommand{\tikzrowmark}[2]% #1 = number of columns, #2 = coordinate name
{\multicolumn{#1}{c}{\tikzmark[\arraystretch\ht\strutbox]{#2}}%
\\[\dimexpr -\arraystretch\ht\strutbox-\arraystretch\dp\strutbox]}%

\begin{document}

\begin{tabular}{|@{\tikzmark{A west}\hspace{\tabcolsep}}c@{\hspace{\tabcolsep}\tikzmark{A east}}|c|}
\hline
\tikzrowmark{2}{A north}
A&B \\
\tikzrowmark{2}{A south}
\hline
\end{tabular}

\begin{tikzpicture}[overlay,remember picture]
\fill[yellow] (A west |- A north) rectangle (A east |- A south) ;

\end{tikzpicture}

\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thank you. But I need nodes for another purpose. The rectangle is only a mwe. – Tarass Feb 14 '18 at 13:14
  • If you name the node (A), you have access to (A.south west) and (A.north east). If you want to put tikzmarks inside the tabular, you can use a savebox and [remember picture]. – John Kormylo Feb 14 '18 at 15:22
  • I need a node between A and B, on the vertical line. – Tarass Feb 14 '18 at 16:13
  • @johnkomylo The title of the question was wrong but my example shows what I want. Nodes on the corners of tabular boxes. – Tarass Feb 14 '18 at 16:16
  • @johnkomylo Excellent !!! One can put tikz stuf in \noalign placed after the second \rowskip then use the @{} of the next line. Or naming the tikzmark using two counters and then have an unique xy reference to each one. – Tarass Feb 14 '18 at 19:33
  • @johnkomylo Still a little problem, if one put | between column, they go out of the tabular at the last row. – Tarass Feb 14 '18 at 19:59
  • Note that when you use |@{} you only need to add the \hspace{\tabcolsep} on the side away from the |. – John Kormylo Feb 14 '18 at 22:18
2

The package nicematrix does that job (in fact, nicematrix creates those nodes and uses them to provide further functionnalities).

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix,tikz}

\begin{document}

\setlength{\arrayrulewidth}{1pt} \begin{NiceTabular}{cc}[hvlines,name=MyTable] A & B \end{NiceTabular}

\begin{tikzpicture}[overlay,remember picture,name prefix = MyTable-] \fill [yellow] (1-|1) rectangle (2-|2) ; \end{tikzpicture}

\end{document}

The PGF/Tikz nodes are at the middle of the rules (by design).

Output of the above code

F. Pantigny
  • 40,250