3

The goal is to obtain a table with fixed height rows, and to place the material inside -certain- cells vertically centered.

Looking for a robust and simple way, I have worked on this idea:

\documentclass{article}
\usepackage{tabularx}
\begin{document}

\def\text{This is a text example inside a cell}

\newcommand{\cell}[2]{\parbox[t][#1][c]{4cm}{#2}}    % <- '4cm' fixed!
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{tabularx}{\textwidth}{|X|C|}
\hline
\text & \cell{2cm}{\text} \\
\hline
\end{tabularx}

\end{document}

(Similar to this approach using minipage.)

At this point, I would like to use the width of each column into my \cell command, in order to replace the fixed "4cm" definition by the generic column width values.

What I am looking for, if possible, is to get the "current" witdh column register used by tabularx, tabular or array, and to use it like an \parbox argument. This will also be necessary to use \includegraphics [width = x] inside the cells.

For example, you can do it successfully with the paracol package:

\documentclass{article}
\usepackage{paracol}
\begin{document}

\def\text{This is a text example inside a cell}

\columnratio{0.8}
\begin{paracol}{2}
\parbox[t][2cm][c]{\csname pcol@columnwidth\number1\endcsname}{\text}
\end{paracol}

\end{document}
e_moro
  • 888
  • What are you trying to achieve with \hsize=\hsize in \newcolumntype{C}{>{\hsize=\hsize\centering\arraybackslash}X}? – Mico Sep 27 '19 at 10:58
  • Thanks @Mico. It was a bug in the MWE. It is not relevant for the main question. – e_moro Sep 27 '19 at 12:36

2 Answers2

4

Thinking outside of the box...

Hat tip to Phelype for clueing me in to both \@wholewidth and \linethickness.

\documentclass{article}
\usepackage{stackengine}
\newcount\cellwd
\newcount\cellsep
\cellwd=80
\cellsep=5
\makeatletter
%\linethickness{.1pt}
\newcommand\acell[1]{\noindent%
\framebox(\cellwd,\cellwd){\parbox{\the\numexpr\cellwd-\cellsep\relax pt}%
  {\centering#1}}\kern\@wholewidth\ignorespaces}
\newcommand\cellrowskip{\\[\dimexpr-1pt+\@wholewidth\relax]}
\makeatother
\begin{document}
What precedes.

\acell{Here is a test of my parbox}
\acell{xyz}
\acell{Not sure if this is what is desired}
\cellrowskip
\acell{Ha-ha!}
\acell{We are almost there!}
\acell{Finis!}

What follows.
\end{document}

enter image description here

If you want a more packaged implementation, I introduce the syntax

\begin{celltable}[<linethickness>]{<cell width>}{<cell height>}{<2X cell sep>}
|{<cell data 1,1>} |{<cell-data 1,2>}
\cellrowskip
|{<cell data 2,1>} |{<cell-data 2,2>}
\end{celltable}

Here is the MWE:

\documentclass{article}
\usepackage{stackengine}
\newcount\cellwd
\newcount\cellht
\newcount\cellsep
\makeatletter
{\catcode`|=\active \gdef|{\acell}}
\newcommand\acell[1]{\noindent%
\framebox(\cellwd,\cellht){\parbox{\the\numexpr\cellwd-\cellsep\relax pt}%
  {\centering#1}}\kern\@wholewidth\ignorespaces}
\newcommand\cellrowskip{\\[\dimexpr-1pt+\@wholewidth\relax]}
\newenvironment{celltable}[4][\@wholewidth]
{\cellwd=#2\relax
\cellht=#3\relax
\cellsep=#4\relax
\linethickness{#1}%
\vspace{#1}%
\par\catcode`\| \active 
}{\par}
\makeatother

\begin{document}
What precedes.

\begin{celltable}[3pt]{70}{50}{5}
|{Here is a test of my parbox}
|{xyz}
|{Not sure if this is what is desired}
\cellrowskip
|{Ha-ha!}
|{We are almost there!}
|{Finis!}
\end{celltable}

What follows.
\end{document}

enter image description here

2

You can use \linewidth:

\documentclass{article}
\usepackage{tabularx}
\begin{document}

\def\text{This is a text example inside a cell}

\newcommand{\cell}[2]{\parbox[t][#1][c]{\linewidth}{#2}}    % 
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{tabularx}{\textwidth}{|X|C|}
\hline
\text & \cell{0.5\linewidth}{\text} \\
\hline
\end{tabularx}

\end{document}
Ulrike Fischer
  • 327,261