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

Don Hosek don.hosek at
Wed Jun 2 15:46:36 CEST 2021

As noted earlier by Vladimir Lomov, \centering is a bit incompatible with tables (although not because \centering expects the original value of \\, but because it does \let\\\@centercr which redefines \\ and messes up the definition). 

The non-package solution to this is to define
\NewDocumentCommand{\headercell}{ m } {%
  {\centering #1\par}%

then enclose each header with \headercell{…}. By enclosing the \centering in a group, we keep its redefinition of \\ from affecting the \\ at the end of the line (each cell of a tabular is implicitly in a group which means that if you had \centering in every cell but the last one, you would never have encountered an issue). We also put a \par at the end after #1 so that we end the paragraph inside the group and have the settings of \centering still in effect. If you take that out, you’ll see that your header doesn’t get centered as you’d expect.

If you want to allow paragraphs in your \headercell, you will need to change the m to +m in the argument specification, but that’s probably not necessary for your use case and is primarily useful for educational purposes to do something like:

\NewDocumentCommand{\headercell}{ +m } {%
  {\centering #1}% ☜ Note that I removed the \par here
\headercell{First paragraph which will be centered.

Second paragraph which won’t have centering applied.}


More information about the texhax mailing list.