One often needs to alter the alignment of a tabular p
('paragraph')
cell, but problems at the end of a table row are common. With a
p
cell that looks like:
one is liable to encounter errors that complain about a "misplaced... & \centering blah ... \\
\
noalign
" or "extra alignment tab", or the like.
The problem is that the command \\
means different things in
different circumstances: the tabular
environment
switches the meaning to a value for use in the table, and
\
centering
, \
raggedright
and \
raggedleft
all change the
meaning to something incompatible. Note that the problem only
arises in the last cell of a row: since each cell is set into a box,
its settings are lost at the &
(or \\
) that
terminates it.
The simple (old) solution is to preserve the meaning of \\
:
which one uses as:\newcommand\PBS[1]{\let\temp=\\% #1% \let\\=\temp }
(for example).... & \PBS\centering blah ... \\
The technique using \
PBS
was developed in the days of LaTeX 2.09
because the actual value of \\
that the tabular
environment uses was only available as an internal command. Nowadays,
the value is a public command, and you can in principle use it
explicitly:
which may be incorporated into a simple macro as:... & \centering blah ... \tabularnewline
and used as\newcommand{\RBS}{\let\\=\tabularnewline} ... ... & \centering\RBS blah ... \\
(note, you Preserve backslash with... & \centering\RBS blah ... \\
\
PBS
before the command
that changes it, and Restore it with \
RBS
after the
command; in fact, \
RBS
is marginally preferable, but the old
trick lingers on).
The \
PBS
and \
RBS
tricks also serve well in array
package "field format" preamble specifications:
or\begin{tabular}{... >{\centering\RBS}p{50mm}} ...
In the tabularx and tabulary packages, there's a command\begin{tabular}{... >{\PBS\centering}p{50mm}} ...
\
tabularnewline
that does the same as \
RBS
(above);
so in those packages, one might say:
in place of the example above; in fact, the very latest (2003/12/01) release of array.sty also provides the\begin{tabular}{... >{\centering\tabularnewline}p{50mm}} ...
\
tabularnewline
command.
The command does rather lack brevity, but at least you don't have to
define it for yourself.
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=tabcellalign