0

I would like to center the header in the table below:

\documentclass[10pt]{article}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{l|Wr{2cm}|Wr{2cm}}
% \RowStyle{\centering}
& column 1 & column 2 \\
row 1 & 0\% & 1\% \\
row 2 & 2\% & 3\%
\end{NiceTabular}
\end{document}

If I uncomment the line, however

! LaTeX Error: Something's wrong--perhaps a missing \item.

I think this is because of the duplicate alignment specification, but I don't know how to fix it.

1 Answer 1

1

With multicolumn syntax

\multicolumn{1}{c}{column 1}

where

{1} is the number of column to be merged - in this case one column

{c} is the positioning of the text - in this case centering denoted by `c`

{column1} is the text to occupy the centered column  

MWE

\documentclass[10pt]{article}
\usepackage{nicematrix}
\begin{document}
    \begin{NiceTabular}{l|Wr{2cm}|Wr{2cm}}
        % \RowStyle{\centering}
        & \multicolumn{1}{c}{column 1} & \multicolumn{1}{c}{column 2}\\
        row 1 & 0\% & 1\% \\
        row 2 & 2\% & 3\%
    \end{NiceTabular}
\end{document}

enter image description here

To bold the header use the makecell package and redefine the theadfont as below

MWE

\documentclass[10pt]{article}
\usepackage{nicematrix}
\usepackage{makecell}

\renewcommand\theadfont{\bfseries}

\begin{document}
    \begin{NiceTabular}{l|Wr{2cm}|Wr{2cm}}
        % \RowStyle{\centering}
        & \multicolumn{1}{c}{\thead{column 1}} & \multicolumn{1}{c}{\thead{column 2}}\\
        row 1 & 0\% & 1\% \\
        row 2 & 2\% & 3\%
    \end{NiceTabular}
\end{document}

enter image description here

A dash of color will emphasise the header and remove the need for having vertical lines

To contrast with the dark background of the header a blue font can be added with the help of redefining theadfont as below

MWE

\documentclass[10pt]{article}
\usepackage{nicematrix}
\usepackage{makecell}

\renewcommand\theadfont{\color{blue!20!white}\bfseries}

\begin{document}
    \begin{NiceTabular}{lWr{2cm}Wr{2cm}}[%
        code-before = \rowcolor{black!60}{1}]
        
        % \RowStyle{\centering}
        & \multicolumn{1}{c}{\thead{column 1}} & \multicolumn{1}{c}{\thead{column 2}}\\
        row 1 & 0\% & 1\% \\
        row 2 & 2\% & 3\%
    \end{NiceTabular}
\end{document}

enter image description here

2
  • 1
    Thank you! It's really surprising that multicolumn is needed for this. And thank you for the extra examples, I will definitely use them
    – user357269
    Nov 29, 2022 at 21:34
  • 1
    btw, it might be easier to do \RowStyle{\leavevmode \color{blue!20!white} \bfseries} than bring in makecell
    – user357269
    Nov 29, 2022 at 22:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .