[Tex/LaTex] Plain Tex: Get cell in table to span multiple rows, and contents to be centered

plain-textablesvertical alignment

For PlainTeX (I did find similar questions for latex):

  • Q 1: How to ensure upper left cell is two rows tall?
  • Q 2: How to ensure entry in upper left cell is vertically and horizontally centered?

In following image, table on left is as per code below; want guidance on developing code for table on right:

Table on left is as per code below, want guidance on developing code for table on right

Code (for table on left):

% plain TeX with pdftex  

\parskip3mm  

Attempt at adapting example from Seroul and Levy's {\it A Beginner's Book of $TeX$}:  

\leftskip10mm  
\item{Q 1}How to ensure upper left cell is two rows tall?  
\item{Q 2}How to ensure entry in upper left cell is vertically and horizontlly centered?  

\leftskip0mm  

$$\vbox{\offinterlineskip  
\catcode`\*=\active \def*{\hphantom{0}}  
\def\vr{\vrule height 12pt depth 5pt} \def\vrq{\vr\quad}  
\settabs  
\+\vr\quad A \quad&\vr\quad Col A\quad&\vr\quad Col B\quad&\cr  
\hrule  
\+\vrq &\vrq Spanning 2 Cols&&\vr\cr  
\hrule  
\+\vrq \# &\vr\hfill Col A\hfill&\vr\hfill Col B\hfill&\vr\cr  
\hrule  
\+\vrq 1 &\vr\hfill R1C1\hfill&\vr\hfill R1C2\hfill&\vr\cr  
\hrule  
\+\vrq 2 &\vr\hfill R2C1\hfill&\vr\hfill R2C2\hfill&\vr\cr  
\hrule  
}$$  

\bye  

Update:

Used the library and looked at about 10 books on plain TeX — only one, Malcolm Clark's A Plain TeX Primer address creation of tables which contain not only horizontally combined cells but also vertically combined cells (refer Figures 15.3 and 16.3 of that book).

I tried to modify the table from Section 9.10 of Seroul and Levy's A Beginnner's Book of TeX by using Clark's ideas to add both horizontally and vertically combined cells — but as can be seen from the figure below, there is the issue of vertical centering, excess vertical space, and vertical "barbs":

illustrates vertical issues:  centering, excess space, and barbs

Here's the code:

% plain TeX with pdftex

 $$\vbox{\offinterlineskip
      \def\foovrule{\vrule}
     %\def\foovrule{\vrule height 0pt depth 0pt}
     %\def\foovrule{}

      \def\foohrulefill{\hrulefill}
     %\def\foohrulefill{\leaders\hrule height 0pt depth 0pt \hfill}

      \def\mr{\omit&height 2pt&&&&&&\cr}
      \def\vspan#1#2{\parindent0pt\setbox0
          \vbox to#1\normalbaselineskip
                {\null\vfill#2\vfill\null}%
                \ht0\ht\strutbox
                \dp0\dp\strutbox
                \setbox1\hbox{#2}\wd0\wd1\box0}
      \halign{\strut#&\vrule#\quad&
              \hfil$#$\hfil&
              \quad\vrule#\quad&
              \it#\hfil&\quad\vrule#\quad&
              \hfil#&\quad\vrule#\cr
              \noalign{\hrule} \mr
              &&\omit\bf name&&
                     \bf type&&
                     \bf value&\cr
              \mr \noalign{\hrule} \mr
              &&x&& integer&&   1987&     \cr
              \mr \noalign{\hrule} \mr
              &&y&& real&&      3.14159&  \cr
              \mr \noalign{\hrule} \mr
              &&\vspan2{entry needs lowering}&& excess space below&&      excess space below&  \cr
              &&&\omit\quad\foovrule\foohrulefill&
                 \omit\foohrulefill&\omit\foohrulefill\foovrule\foohrulefill&
                 \omit\foohrulefill&
                 \omit\foohrulefill\foovrule  \cr
              &&&&        \multispan3  small vertical barbs from column divider&  \cr
              \mr \noalign{\hrule} \mr
              &&z&& boolean&&   false&    \cr
              \mr \noalign{\hrule}
      }}$$

\bye

Attempts to fix the vertical issues (centering, excess space, and barbs) by playing with height and depth of \vrule and \hrulefill (see \foovrule and \foohrulefill) did not help.

Yet Another Update:

Was able to tweak @wipet 's solution to get the following

enter image description here

Here's the tweaks:

1) Removed barbs by making their height 0:

\def\barb{\omit \vrule height 0pt \hfil\vrule}

2) Made spaces above and below entries equal by increasing depth in \halign:

\halign{\vrule height3ex depth2ex

3) Readjusted centering by modifying \def\low#1:

\def\low#1{\vbox{\hbox{\rm#1}\kern-12pt}}

I suspect @wipet can come up with a better way to implement the tweaks; will await for this possibility happening before accepting his solution.

Best Answer

I don't understand why your code is such complicated. Here is more simple code which creates exactly the same table as the table mentioned in your question.

\vbox{\offinterlineskip
   \def\crl{\cr\noalign{\hrule}}
   \def\barb{\omit \vrule height 3pt \hfil\vrule}
   \def\low#1{\vbox{\hbox{\rm#1}\kern-9pt}}
   \halign{\vrule height3ex depth1ex
           \quad\hfil\it #\unskip\hfil\quad\vrule&% 
           \quad\it#\unskip\hfil\quad\vrule&\hfil\quad#\unskip\quad\vrule\span\crl
           % data:
           \bf name\hfill\ & \bf type & \bf value \crl
           x     & integer & 1987 \crl
           y     & real    & 3.14159 \crl
           \low{entry needs lowering}  & excess space below & exess space below \cr
           \omit &\omit\hrulefill &\omit\hrulefill \cr
           \barb && \cr
                 & \multispan2 \hfil small vertical barbs from column divider \hfil\vrule\cr
           \barb &&\crl
           z     & boolean & false \crl
}}
Related Question