5

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.

1
  • 2
    I'd just like to note that I'd consider it better style to not have rules, and to left align everything.
    – morbusg
    Commented Feb 17, 2015 at 9:17

2 Answers 2

5

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
}}
6
  • thanks! got what I was looking for after three tweaks to your solution -- have updated my original question with the current approach based on your solution. Is there a way to replace the numbers used (e.g., kern-<number>pt, \halign{\vrule height<number>ex depth<number>ex) by measurements, perhaps of the font being used? Commented Feb 18, 2015 at 2:53
  • @user1823664 ad 1) Simply don't use \barb && \cr(l) in the data area. And replace \hfil\vrule\cr by \hfil\vrule\crl. The re-definition of \barb is another solution, but more curious. Ad 2), 3) the ex unit depends on the font being used. So, only use this unit. For example \def\low#1{\vbox{\hbox{\rm#1}\kern-3ex}}.
    – wipet
    Commented Feb 18, 2015 at 8:50
  • How Seroul and Levy address (2): start with \nointerlineskip and with \strut inside \halign: this will make all rows of uniform height but with no white space above and below -- so add white space above and below entries using \mr of \def\mr{...2pt...} after and/or before each \hrule Commented Feb 18, 2015 at 14:26
  • @user1823664 \mr is not simple solution. More simple is to use bigger strut, i.e. the \vrule height3ex depth2ex in my solution. Then you needn't to write the \mr sequence in data area.
    – wipet
    Commented Feb 18, 2015 at 15:19
  • 1
    @user1823664 If you are studying books about tables in plain TeX, you can compare this with my book math.feld.cvut.cz/pub/olsak/tbn/tbn.pdf pages 129 to 143, section "tables by \halign". Especially very complex table is at the page 142.
    – wipet
    Commented Feb 18, 2015 at 17:58
3

You could do it using \valign:

\newskip\colskip \colskip=2em
\newtoks\cellformat \cellformat={
  \parindent=0pt
  \raggedright
  \hsize=\dimexpr\hsize/4 % the width of a cell
}

{
\def\cr{\crcr\noalign{\hskip\colskip}} % gets inserted between columns

\valign{& \vfill\the\cellformat #\vfill\tabskip=\baselineskip\crcr
  \span Two rows& 3rd row& 4th row\cr
  1st row 2nd col& 2nd row \& col\cr
  \multispan3\vfil\the\cellformat cell spanning 3 rows\vfil\cr
}
}
\bye

enter image description here

3
  • 1
    +1, "People usually work with TeX at least a year before they find their first application for \valign" --- personally I'm still waiting!
    – Thruston
    Commented Feb 7, 2015 at 12:09
  • Thanks; but it looks like \valign makes it hard to get entries that span multiple columns -- have added image to original question to illustrate need for entries that span multiple rows and entries that span multiple columns; also need for vertical and horizontal lines. Commented Feb 7, 2015 at 14:55
  • @user1823664: yes, you can easily span multiple columns with \halign, or multiple rows with \valign, but having both spans in the same table can't be done with the aforementioned primitives (easily, anyway).
    – morbusg
    Commented Feb 7, 2015 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .