# Strange problem with centring headers in "p{wd}" columns in "tabular".

Herbert Voss Herbert.Voss at fu-berlin.de
Wed Jun 2 08:30:40 CEST 2021


Am 02.06.21 um 04:45 schrieb Rolf Turner:
> I wanted to create a form with columns 2 through 5 all of width 2 cm.
> I tried using "p{2cm}" as the "cols" specifier, and that worked
> fine, but then I decided that I'd like to have the column headers
> *centred* in the columns as well.
>
> So I preceded each column header by "\centering".  This produces (in
> my minimal reproducible example) an error:
>
>> ! Misplaced \noalign.
>> \hline ->\noalign
>>                    {\ifnum 0=}\fi \hrule \@height \arrayrulewidth
>> \futurelet... l.8           \centering Yak \\ \hline
> ("Yak" being the header of the last column).
>
> I cannot parse this error message.
>
> I then removed the "\centering" command that preceded "Yak" (but
> left the other "\centering" commands as they were).  The *.tex file
> then compiled just fine and the headings of columns 2, 3 and 4 were
> duly centred, but the heading of the last column, "Yak", was of course
> not centred.
>
> Can anyone explain to me what is going on, and how (if it is indeed
> possible) to get the headings of all of the "p{wd}" columns centred?

use package ragged2e and \Centering

\documentclass{article}

\usepackage{ragged2e}
\newcommand{\joist}{\rule[-1.2cm]{0cm}{1.2cm}}

\begin{document}
\begin{center}
{\Large \textbf{A Load of Dingoes' Kidneys}} \\[0.8cm]
\begin{tabular}{|l|p{2cm}|p{2cm}|p{2cm}|p{2cm}|} \hline
Student & \Centering Yadda & \Centering Blather & \Centering Yakkity &
\Centering Yak \\ \hline
\joist Melvin & & & & \\ \hline
\joist Clyde & & & & \\ \hline
\joist Irving & & & & \\ \hline
\joist Fred & & & & \\ \hline
\end{tabular}
\end{center}
\end{document}

Herbert
`