5

Do you know how I can specify the size of a cell in points (pt) within a tabular, that is the row height and the column witdh? In text mode there are commands like \textwidth or \baselineskip but this does not work within a tabular environment. I do not want to adjust these parameters, I only want to know their values.

\documentclass{scrreprt}

\begin{document}
\centering
\begin{tabular}[t]{rl}
  Row Height:   & "\textbackslash{}the \textbackslash{}getRowHeight" \\
  Column Width: & "\textbackslash{}the \textbackslash{}getColWidth"
\end{tabular}

\end{document}

Thank you! :)

1
  • 2
    they are not in general known to the macro layer you need to do a two pass system that either boxes and measures the entries by hand not relying on the table mechanism to give the size, or record coordinates in the aux file (\pdfsavepos or a wrapper such as \tikzmark) and then pick them up on the next run of latex. Mar 28 at 13:07
5

In the environment {NiceTabular} of nicematrix, you have PGF/Tikz nodes which are created under the cells, rows and columns. It's possible to use those nodes to compute the dimensions of the current cell and use Tikz back again to put the result in the cell.

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

\makeatletter
\ExplSyntaxOn
\cs_new_protected:Nn \__pantigny_SizeOfCell:nn 
  {
    \begin{tikzpicture}
    \path let \p1 = (#1-|#2) ,
              \p2 = (\int_eval:n{#1+1}-|\int_eval:n{#2+1}) 
          in node at (#1-#2) 
             { \dim_eval:n { \x2 - \x1 } $\times$ \dim_eval:n{\y1-\y2} } ;
    \end{tikzpicture}
  }

\NewDocumentCommand \SizeOfCell {}
  {
    \NotEmpty
    \tl_gput_right:Nx \g_nicematrix_code_after_tl
      { \__pantigny_SizeOfCell:nn { \arabic { iRow } } { \arabic { jCol } } }
  }
\ExplSyntaxOff
\makeatother

\begin{document}
\begin{NiceTabular}{ccc}[hvlines]
AAAA & AAAAAAAAAAAAAA AAAAAA & BB \\
BB & \SizeOfCell  & CCC \\
CC & BBBBB  & DD
\end{NiceTabular}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

3
  • 2
    +1. You may want to point out explicitly that 12.4pt is the sum of \baselineskip and \arrayrulewidth (which is 0.4pt by default). Sure enough, if one were to run \fontsize{10}{20}\selectfont and \setlength{\arrayrulewidth}{2.5pt}, the cell height would come to 22.5pt.
    – Mico
    Mar 28 at 14:37
  • @Mico: You are right. Mar 28 at 14:54
  • Maybe it would be nice, if NiceTabular (and NiceMatrix) elements do offer some functions to determine these values? :D
    – Max16hr
    Mar 28 at 22:19
2

tabstackengine shares some of the features that are available through tabular. One additional thing it does is provide \TABwd{} to provide column width of the most recently created TABstack. Likewise, \TABht{} and \TABdp{} can be used for row heights/depths.

As seen in the MWE, these quantities are available inside the TABstack and afterwards, as well.

These quantities provide the height & width of the row/column not accounting for border. In order to account for the additional border of the stack, there is \tabular@gap separation between inner columns (but not around the outer edges), and (in the case of a short stack such as this, \Sstackgap space between rows.

\documentclass{scrreprt}
\usepackage{tabstackengine}
\begin{document}
\centering
\begin{tabular}[t]{rl}
  Row Height:    & "\textbackslash{}the \textbackslash{}getRowHeight" \\
  Columdn Width: & "\textbackslash{}the \textbackslash{}getColWidth"
\end{tabular}

\the\tabcolsep

\setstacktabulargap{12pt}% COMPARABLE TO 2\tabcolsep
\tabularShortstack{rl}{
  Row Height:    & "\textbackslash{}the \textbackslash{}getRowHeight" \\
  Columdn Width: & "\textbackslash{}the \textbackslash{}getColWidth"\\
  \rule{\TABwd{1}}{\dimexpr\TABht{2}+\TABdp{1}}& 
  \rule{\TABwd{2}}{\dimexpr\TABht{2}+\TABdp{1}}
}

\bigskip
Column widths:
\TABwd{1}, \TABwd{2}

\medskip
Row heights/depths:
\TABht{1}/\TABdp{1}, \TABht{2}/\TABdp{2}
\end{document}

enter image description here

2
  • +1. You may want to point out that 8.2125pt+2.7375pt=10.95pt. (Unlike the "basic" LaTeX document classes -- article, report, and book, which use 10pt as the default font size, the KOMA-Script classes use 10.95pt as the default font size. 10.95 is the geometric average of 10 and 12. Don't ask...) As such, your method for determining the cell height may not be factoring in (a) the value of \baselineskip and (b) the width parameter \arrayrulewidth.
    – Mico
    Mar 28 at 14:59
  • 1
    @Mico If one wants \baselineskip to be accounted for, one must use Longstacks instead of Shortstacks. As to \arrayrulewidth, tabstacks do not intrinsically support \hlines. The TABcline package option provides something akin to \cline, except that the lines are actually created as their own row in the stack. Mar 28 at 15:15

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.