1

So, I've followed this example to highlight cells in my tabularx-table. This works well for single-"item"-cells and with some \hfill it works well enough for ragged-multiline-cells too.

However, with multiline, centering cells, this approach doesn't work at all, as the reference points are set by the first and last word thus drawing a frame cutting throught the text:

bad outcome

some playing around with newlines yields this result, which is ok, however the "full" line is not really nice:

okish outcome

So, how could I improve on this? I've tried to get the cell-extents (or at least \TX@col@width, but that didn't work for me); \hphantom should nominally do, what I want, but I think it doesn't really work in tables.

MWE (the newline/hfill-comments give the "ok" solution. :

\documentclass{standalone}
\usepackage{tabularx}
\usepackage{multirow}

\usepackage{tikz} \usetikzlibrary{fit} \usetikzlibrary{shapes}

\newcommand{\tabxmulticol}[3] {\multicolumn{#1} {>{\centering\hsize=\dimexpr#1\hsize+#1\tabcolsep+\arrayrulewidth\relax}#2} {#3}}

\newcommand\marktopleft[1]{% \tikz[overlay,remember picture]% \coordinate (marker-#1-a) at (0,1.5ex) {};% \hspace*{\fill}\newline% }

\newcommand\markbottomright[1]{% \newline \hspace*{\fill} \pgfsetcornersarced{\pgfpoint{3mm}{3mm}} \tikz[overlay,remember picture] \coordinate (marker-#1-b) at (0,-0.5ex) {};% \tikz[overlay,remember picture,thick,dashed,inner sep=0pt] \node[draw,rectangle,fit=(marker-#1-a.center) (marker-#1-b.center)] {};% }

\begin{document} \renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}} \begin{tabularx}{\textwidth}{p{0.24\textwidth}XX} First row & & \marktopleft{cella} bla bla bla bla bla bla bla bla bla \markbottomright{cella} \ Second row & 2a & \ Third row & & 3b \ Fourth row & \tabxmulticol{2}{X}{\marktopleft{cellb} he he he he he he he he he he he he he he he he he he he he he he he he he he he he \markbottomright{cellb}} \ \end{tabularx} \end{document}

flxmmr
  • 11
  • Welcome to TeX.SX! It might be easier if you use a matrix of TikZ nodes, as proposed in the other answer to the question you link to. For a possible solution using this approach see: https://tex.stackexchange.com/q/83323/47927 . – Jasper Habicht Feb 09 '22 at 21:49
  • Thanks, I've looked into this, but the problem there is, that the multirow/column-cells are not that easy there then. The nicematrix by Imran does it nicely though! – flxmmr Feb 10 '22 at 10:31

1 Answers1

2

It's possible to achieve what you want using the nicematrix package.

enter image description here

\documentclass{article}

\usepackage{nicematrix} \usepackage{tikz} \usetikzlibrary{fit}

\begin{document} \begin{NiceTabularX}{\textwidth}{p{0.24\textwidth}X[c]X[c]}[name=MyTbl] \RowStyle[cell-space-limits=3mm]{} First row & & bla bla bla bla bla bla bla bla bla \ Second row & 2a & \ Third row & & 3b \ \RowStyle[cell-space-limits=3mm]{} Fourth row & \Block{1-2}{\parbox{8.5cm}{\centering he he he he he he he he he he he he he he he he he he he he he he he he he he he he}} &\ \CodeAfter \tikz \node[draw, dashed, rounded corners, inner ysep=-1mm,rectangle, fit=(MyTbl-1-3)] {}; \tikz \node[draw, dashed, rounded corners, inner ysep=-1 mm,rectangle, fit=(MyTbl-4-3) (MyTbl-4-2)] {}; \end{NiceTabularX} \end{document}

nicematrix associates a tikz node for every cell of the table and it's possible to use these nodes with tikz. This makes what you want easier. The nodes are accessible with the name name-i-j where name is the name given to the table (with the optional argument of the environment) and i and j are the numbers of row and column.

The only downside is that nicematrix doesn't yet support automatic linebreak in multi-cell blocks. So, to do that, one needs to insert a parbox in a block.

Edit:

It looks better to me if more space is added around every row.

\documentclass{article}

\usepackage{nicematrix} \usepackage{tikz} \usetikzlibrary{fit}

\begin{document} {\NiceMatrixOptions{cell-space-limits = 2mm} \begin{NiceTabularX}{\textwidth}{p{0.24\textwidth}X[c]X[c]}[name=MyTbl2] % \RowStyle[cell-space-limits=3mm]{} First row & & bla bla bla bla bla bla bla bla bla \ Second row & 2a & \ Third row & & 3b \ % \RowStyle[cell-space-limits=3mm]{} Fourth row & \Block{1-2}{\parbox{8.5cm}{\centering he he he he he he he he he he he he he he he he he he he he he he he he he he he he}} &\ \CodeAfter \tikz \node[draw, dashed, rounded corners, inner ysep=0mm, rectangle, fit=(MyTbl2-1-3)] {}; \tikz \node[draw, dashed, rounded corners, inner ysep=0mm, rectangle, fit=(MyTbl2-4-3) (MyTbl2-4-2)] {}; \end{NiceTabularX} } \end{document}

enter image description here

By the way, it is necessary to compile the code more than once to get the correct result.

Imran
  • 3,096
  • Thank you, that's a great solution! (unfortunately I can't mark it solved) – flxmmr Feb 10 '22 at 10:29
  • Usually, it's good to wait for some time (maybe a day) before accepting, as a better solution may be given. However, I will like to know why you can't accept it as that might help me improve the answer. – Imran Feb 10 '22 at 10:47
  • I am a fresh user and accepting so fast is a rookie mistake, SX wants to prevent. On that note: do you know, how to query the width of the X-columns so that you can set up the parbox programatically? – flxmmr Feb 10 '22 at 11:34
  • Regular X column width depends on other columns. I don't know how to get exactly the width of the X column if there is an l, c, or r column. But for your case, \dimexpr \texwidth - 0.24\textwidth - \tabcolsep should give you the combined width of your two X columns. But sadly it doesn't work as expected in the parbox (I don't know why). – Imran Feb 10 '22 at 12:29