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

Vladimir Lomov lomov.vl at yandex.ru
Wed Jun 2 07:50:35 CEST 2021


Hello
** Rolf Turner <r.turner at auckland.ac.nz> [2021-06-02 14:45:42 +1200]:

> 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.

It is collision between \centering and tabular. In tabular environment the \\
is redefined but \centering expects it's original definition, see, e.g.
https://texblog.net/latex-archive/floats/center-centering/

> 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?

I would suggest to use array package and use it's \arraybackslash command.
Just use this line

  \centering Yak \arraybackslash \\ \hline

in the header.

After I added the change into mre01 I got result you may expect but then I
thought that first column of table a bit "visually unpleasant". I played a bit
with the example and came to this one (see attach file). It is a bit ugly but
cells material in first column is vertically centred.

[...]

> Rolf Turner

---
WBR, Vladimir Lomov

-- 
Common sense is the collection of prejudices acquired by age eighteen.
		-- Albert Einstein
-------------- next part --------------
\documentclass{article}
\usepackage{array}
\newcommand{\joist}{\raisebox{-0.2cm}{\vphantom{V}}\raisebox{1.2cm}{\vphantom{V}}}
\newcolumntype{S}{>{\centering\arraybackslash}m{2cm}}
\begin{document}
\setlength{\extrarowheight}{1pt}
\begin{center}
  {\Large \textbf{A Load of Dingoes' Kidneys}} \\[0.8cm]
  \begin{tabular}{|S|S|S|S|S|} \hline
    Student & Yadda & Blather & Yakkity & Yak \\[10pt] \hline
    Melvin & & & & \joist \\ \hline
    Clyde & & & & \joist \\ \hline
    Irving & & & & \joist \\ \hline
    Fred & & & & \joist \\ \hline
  \end{tabular}
\end{center}
\end{document}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <https://tug.org/pipermail/texhax/attachments/20210602/bb7fcdf6/attachment.sig>


More information about the texhax mailing list.