2

Is it possible to retrieve the height of a row, which has a p{}-column in an tabularx environment?

Ultimately I want to be able to get the width and height of the X column cells. The width is available by \TX@col@width, but how can I get the height?

Usecase: I want to fit a \TextField of the hyperref package perfectly in a table cell using tabularx as environment. To size the field, I need a width and height.

\documentclass{scrartcl}

\usepackage[table,xcdraw]{xcolor}
\definecolor{hyperlinkblue}{RGB}{204,215,251}

\usepackage{tabularx}
\usepackage{hyperref}
\hypersetup{pdflinkmargin=-.5pt}
\def\LayoutTextField#1#2{#2} % ignore label of \TextField

\makeatletter
% Determine the width of a cell including the tabcolsep
% First argument: number of columns
% TODO \TX@col@width does not calculate the width correctly. Sometimes it includes \tabcolsep sometimes not
\newcommand{\tabularxCellWidth}[1]{\dimexpr #1\TX@col@width+ 2\tabcolsep\relax}
\makeatother

% TODO missing: get dynamic height of a cell to resize the TextField always correctly
\newcommand{\tabularxCellHeight}{1em}

\newcommand{\myTextField}[1]{\TextField[name={#1}, width=\tabularxCellWidth{1}, height=\tabularxCellHeight, default={#1}]{}}

% Use multicolumn to ignore tabcolsep @{} for cells with a TextField
% TODO Problem: multicolumn removes the outer borders from the table
\newcommand{\TabularTextField}[1]{\multicolumn{1}{@{}@{}X@{}@{}}{\myTextField{#1}}}

\begin{document}

    \begin{tabularx}{\textwidth}{|p{3cm}|X|}
        \hline
        text which may have line breaks & Get width and height of this cell \\
        \hline
        but this has none & \TabularTextField{this text field has perfect size} \\
        \hline
        text which may have many many many many many many line breaks & \TabularTextField{resize me} \\
        \TabularTextField{resize me too} & text which may have many many many many many many line breaks\\
        \TabularTextField{resize me tooo} & text which may have many many many many many many line breaks\\
        \hline
    \end{tabularx}

\end{document}

Another problem I face is the calculation of the width and the removal of the tabcolsep for only a specific cell. I can remove the tabcol with a \multicolumn, but this will also remove the borders. Adding them back might duplicate some borders, which looks bad. Is there another solution to this?

Furthermore \TX@col@width does not always return the correct width of a cell. Sometimes it includes the \tabcolsep sometimes it does not.

Here is a snipped of my pdf, which shows the problems I face.

  • red: error in width calculation (overshoots by 2*\tabcolsep
  • blue: missing borders (multicolumn removes borders, but adding them to multicolumn duplicates some borders)
  • green: wrong field height

my pdf

Carsten
  • 55
  • Welcome to TeX.SE! Height o cells in some row is determined by cell with the longer text. To it are accommodated all cells in this row. For inserting hyper links in cells you not need to define height of cell. Of course, rows usually have different heights. If you like to have equal height of all rows, you can obtain this by inserting invisible rule with prescribed width and zero length. SInce your question is not very clear, please show us an example/sketch, how the table should looks. – Zarko Mar 02 '20 at 12:36
  • Do you like something like this? `\documentclass{scrartcl} \usepackage{tabularx} \usepackage{hyperref}

    \begin{document} \begin{tabularx}{\textwidth}{|p{3cm}|X|@{}c@{\rule[-4em]{0pt}{4em}}} \hline text which may have line breaks & Get width and height of this & \ \hline text which may have many many many many many many line breaks & \href{https://tex.stackexchange.com/questions/530775}{resize me} & \ \hline but this has none & \href{https://tex.stackexchange.com/questions/530775}{resize me too} & \\hline \end{tabularx} \end{document}˙`.

    – Zarko Mar 02 '20 at 12:53
  • Hey Zarko. Thank you for your reply. I do not want to resize all cells to the same height, but the \TextFields to fill the respective cell perfectly. I'll edit my MWE. – Carsten Mar 02 '20 at 13:17
  • Now I guess that you have problem with lenght of text in \TabularTextField. In X column type you shouldn't change /adopt column width with to its content. It is prescribed with column type. For test use code from my previous comment and delete in it last column and then see, if result is close to what you like to have. – Zarko Mar 02 '20 at 14:11
  • Ok, I removed the last column. How do you resize the hyperlinks a.k.a. the \TextField (the blue area in which you can enter text) to fill the respective cell? – Carsten Mar 02 '20 at 14:24
  • With your code I can't reproduce showed table (text in \TabularTextField not show up). Does text in \TabularTextField can have more than one line? Unfortunately I'm lost in your code ... – Zarko Mar 02 '20 at 15:31
  • I am not sure why the MWE does not show the blue area in my TexStudio, but my pdf viewer (Adobe) does. This might work for you too. The text only shows when you click on a field.. another bug I do not understand in the MWE. I dont have these problems in my original code. – Carsten Mar 02 '20 at 15:51
  • Ah, I look in sumatra. I'm sorry but I will give up. I can't help you. – Zarko Mar 02 '20 at 15:54
  • I recently found my unanswered question again, while googling for the same error. I found this question, which covers the same problem and might help. https://tex.stackexchange.com/questions/467497/form-how-to-put-form-fields-perfectly-into-table-cells – Carsten Dec 17 '20 at 11:21
  • The question in the title is covered by tables - How to get the row height and column width within a tabular? - TeX - LaTeX Stack Exchange but they require switching to another environment... – user202729 Mar 29 '23 at 01:47

0 Answers0