Below image shows part of a document prepared with a word processor. Gray lines mark table borders and are only visible in edit mode (they will not be visible when document is printed but have been preserved as a hint at text layout). My question is how I can prepare a similar layout--text in two columns--with TeX (preferrably plain TeX).
-
This is off-topic, but you've said before that you're not very familiar with TeX and its various engines (latex, pdf(la)tex, xe(la)tex, lua(la)tex), so why do you insist on using plain TeX rather than LaTeX? There's a reason why LaTeX was created and that most people use that rather than plain TeX.– SverreMay 29, 2014 at 11:48
-
4@Sverre "Many people swear by Plain TeX, and produce highly respectable documents using it (Knuth is an example of this, of course). But equally, many people are happy to let someone else take the design decisions for them, accepting a small loss of flexibility in exchange for a saving of (mental) effort." --tex.ac.uk/cgi-bin/texfaq2html?label=plainvltx. I just wanna be the former type of person. Sorry if this is off-topic. I'd be glad to ask for help in a more appropriate place, if there is one.– SJUMay 29, 2014 at 12:08
-
1No, no, I only meant that my comment was off-topic. Questions about plain TeX are very welcome here.– SverreMay 29, 2014 at 12:17
4 Answers
If you want it centered,
$$
\vbox{\tabskip=0pt
\halign{\tabskip=4cm #\hfil&#\hfil\tabskip=0pt \cr
Date:\cr
Gr.& Declarant\cr}}
$$
Just to see what happens without the $$
for the centering:
$$
\vbox{\tabskip=0pt
\halign{\tabskip=4cm #\hfil&#\hfil\tabskip=0pt \cr
Date:\cr
Gr.& Declarant\cr}}
$$
\bigskip
\noindent X\vbox{\tabskip=0pt
\halign{\tabskip=4cm #\hfil&#\hfil\tabskip=0pt \cr
Date:\cr
Gr.& Declarant\cr}}X
\bye
If you want the columns to occupy half of the line width, here's how to do it:
$$
\vbox{\tabskip=0pt
\halign{\hbox to .5\hsize{#\hfil}&\hbox to .5\hsize{#\hfil}\cr
Date:\cr
Gr.& Declarant\cr}}
$$
If you omit the $$
, add \noindent
in front of \vbox
; but this will not leave space above and below the table.
If you want line breaking, change \hbox to .5\hsize{#\hfil}
into
\vbox{\hsize=.5\hsize\strut#\strut}
for bottom alignment or \vtop
for to alignment. Of course this won't leave space between the two boxes. This can be obtained by reducing the \hsize
inside the two boxes and adding some \tabskip
glue between them.
For more information about tables in Plain TeX, see TeX by Topic (texdoc texbytopic
on TeX Live) or this link
-
Is it possible the table be as wide as the page (excluding page margins) with each column taking up 50% of it?– SJUMay 29, 2014 at 14:18
-
-
Do we need to put the table in math mode, then? Also, why do we need the vertical box and
\tabskip=0pt
?– SJUMay 29, 2014 at 14:33 -
The
\vbox
is not strictly necessary, but you should remember that\halign
is a<vertical command>
, so it switches to vertical mode. The\vbox
allows to place\halign
in a paragraph; it also keeps the setting to\tabskip
local. It's better to set\tabskip
, because it might have some different value remaining from a preceding table.– egregMay 29, 2014 at 14:37 -
When cell contents exceed
.5\hsize
I guess I need a multi-line cell. How is this done? Actually, why does not line-breaking work within cells?– SJUMay 29, 2014 at 18:38
Short crash course on \halign
Used commands and their meanings
- Category code 4 character (default: '
&
') - column separator
- but also, if used inside the preamble, a repeater marker
- Category code 6 character (default: '
#
') - marks the place in the preamble where to put input
\strut
- a box having the height of 8.5pt and depth of 3.5pt and width of 0pt
\tabskip
- glue added between columns
\hfil
- glue with 'first order of infinity' stretching (
\hskip 0pt plus 1fil
) \cr
- mark the end-of-row (short for 'Carriage Return')
\crcr
- same as above, but if there was a
\cr
just before, it does nothing
With an horizontal alignment such as
\halign{#\hfil\strut\tabskip=2em && #\hfil\crcr
Some& short& text\cr
going& on& for\cr
some& rows\cr
}
whose 'expansion' is like (simplified):
\hbox to<widest_cell_in_column>{Some\hfil\strut}\hskip 2em
\hbox to<widest_cell_in_column>{short\hfil\strut}\hskip 2em
% ...
But to have longer runs of text inside a cell, you could put the cell contents inside a \vtop
(or \vbox
, or \vcenter
, depending on how you want them to align [note that \vcenter
expects to be in mathmode]):
\halign{& \vtop{\hsize=.3\hsize\noindent\strut #\strut}\tabskip=2em \crcr
Now for some quite much longer text inside the table cell.&
Aand, another table cell to show how these vboxes are inside the horizontal
alignment command's preamble.\cr
}
whose 'expansion' would be:
\hbox to.3\hsize{\vtop{\hsize=.3\hsize\noindent\strut ...\strut}}\hskip 2em
\hbox to.3\hsize{\vtop{\hsize=.3\hsize\noindent\strut ...\strut}}\hskip 2em
Lies!
Actually I lied; given an alignment like:
\halign to.5\hsize{& \strut#\hfil\tabskip=2em plus\hsize \crcr
Some& short& text\cr
going& on& for\cr
some& rows\cr
}
its 'expansion' is more like:
\hbox to.5\hsize{\hbox to<widest_cell_in_column>{\strut Some\hfil}\hskip 2em plus\hsize
\hbox to<widest_cell_in_column>{\strut short\hfil}\hskip 2em plus\hsize
\hbox to<widest_cell_in_column>{\strut text\hfil}\hskip 2em plus\hsize
}
\hbox to.5\hsize{\hbox to<widest_cell_in_column>{\strut going\hfil}\hskip 2em plus\hsize
\hbox to<widest_cell_in_column>{\strut on\hfil}\hskip 2em plus\hsize
% ...
}
% ...
Conclusion
So, as you see, there are boxes, boxes everywhere. Nothing but boxes and glues.
Here is a simple table in plain TeX:
\halign{\hfil # & # \hfil \cr
{\it Aa} & {\it Bb} \cr
bb & cc \cr
ff & ggg \cr}
\bye
Read this for more detailed information.
I suspect (from the picture in the question) that \vtop
behavior is expected for long data. So my solution adds this feature. Note that the \unskip
before second \strut
is used because user can write space before &
or \cr
. The column declaration in the \halign
is given only once because TeX is able to return to the &
mark when more columns are written in data area.
Moreover I want to show that macro code needn't be mixed with the data even though plain TeX is used. The macro \Table {2c,.5w}
is defined and it is used in the document afterwards. You can define Table {3c,.33w}
by similar way.
\def\Table #1{\csname Table:#1\endcsname}
\def\sdef#1{\expandafter\def\csname#1\endcsname}
\sdef{Table:2c,.5w}{\par
\halign\bgroup &\vtop{\hsize=.5\hsize\settingpar\strut##\unskip\strut}\cr
}
\def\settingpar{\raggedright \leftskip=.5em \advance\rightskip by.5em
\parindent=0pt \emergencystretch=2em }
\def\eTable{\crcr\egroup}
\Table {2c,.5w}
ahu ssgh dh da duia dxue ei eyerr dcew eyr ruiebr cui ueir & uha \cr
gsum bym & ryu duewb \cr
\eTable
\bye
The main feature of \halign
is not used here because the width of all items in the table is constant. So, there is another solution which doesn't use \halign
.
\def\settingpar{\raggedright \leftskip=.5em \advance\rightskip by.5em
\parindent=0pt \emergencystretch=2em }
\def\twocline #1|#2|{\par\line{%
\vtop{\hsize=.5\hsize\settingpar\strut\ignorespaces#1\unskip\strut}\hss
\vtop{\hsize=.5\hsize\settingpar\strut\ignorespaces#2\unskip\strut}}
}
\twocline ahu ssgh dh da duia dxue ei eyerr dcew eyr ruiebr cui ueir | uha |
\twocline gsum bym | ryu duewb |
\bye