How to alter the alignment of tabular cells

  • tables
  • figures

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:

snippet.latex
... & \centering blah ... \\

one is liable to encounter errors that complain about a “misplaced \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.

In the old days, 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:

snippet.latex
... & \centering blah ... \tabularnewline

(but that's a rather verbose way of doing things).

The array package provides a command \arraybackslash which restores \\ to its correct (within table) meaning; the command may be used in array's “field format” preamble specifications:

snippet.latex
\begin{tabular}{... >{\centering\arraybackslash}p{50mm}}
...

The \tabularnewline and \arraybackslash commands are (somewhat) modern additions to LaTeX and the array package, respectively. In the unlikely event that neither is available, the user may try the (old) solution which preserves the meaning of \\: <!– {% raw %} –

>

snippet.latex
\newcommand\PBS[1]{\let\temp=\\%
#1%
\let\\=\temp
}

<

!– {% endraw %} –

>

w

hich one uses within a table as:

snippet.latex
... & \PBS\centering blah ... \\

or in the preamble as:

snippet.latex
\begin{tabular}{...>{\PBS\centering}p{5cm}}
This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

Creative Commons Lizenzvertrag Edit this page Old revisions Sitemap Backlinks RSS feed Impressum Flattr this