3

I am trying two build a table, with the following code:

\begin{table}[]
\begin{tabular}{@{}lllll@{}}
\toprule
\multicolumn{3}{c}{gamma function} & \multicolumn{2}{c}{\begin{tabular}[c]{@{}c@{}}zeta and gamma \\ function\end{tabular}} \\ \midrule
1          & 1         & 1         & 1                                          & 1                                         \\
2          & 3         & 4         & 5                                          & 6                                         \\
4          & 7         & 8         & 43                                         & 21                                        \\ \bottomrule
\end{tabular}
\end{table}

But the third and fourth columns have different widths. I tried to set equal width for all columns, but doesn't work. How can I fix this problem?

2
  • Do you want all five data columns to have the same width, or just columns 3 & 4?
    – Mico
    Commented Dec 15, 2019 at 20:12
  • 1
    I am trying to fix the same width for all columns.
    – sane
    Commented Dec 15, 2019 at 20:14

3 Answers 3

4

Here's a solution which sets all five columns to have equal width. It employs the w column type that's provided by the array package. The column contents are center-set via the c option in the first argument of the w columns.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}  % for "w" column type
\begin{document}
\begin{table}
\begin{tabular}{@{} *{5}{w{c}{11mm} }@{}}
\toprule
\multicolumn{3}{@{}c}{gamma function} & 
\multicolumn{2}{c}{\begin{tabular}[c]{@{}c@{}}
   zeta and gamma \\ function\end{tabular}} \\ 
\midrule
1 & 1 & 1 & 1  & 1  \\
2 & 3 & 4 & 5  & 6  \\
4 & 7 & 8 & 43 & 21 \\ 
\bottomrule
\end{tabular}
\end{table}
\end{document}
5
  • Thanks, very useful! How I can add 6th column with different width?
    – sane
    Commented Dec 15, 2019 at 20:30
  • @sane - change *{5}{w{c}{11mm}} to *{5}{w{c}{11mm}}w{<some other width>}.
    – Mico
    Commented Dec 15, 2019 at 20:44
  • Should I keep *{5}?
    – sane
    Commented Dec 15, 2019 at 20:48
  • Ok, I tried the following *{5}{w{c}{11mm}}*{1}{w{c}{25mm}}. Itworks!
    – sane
    Commented Dec 15, 2019 at 20:55
  • 1
    @sane - *{1}{w{c}{25mm}} and w{c}{25mm} have the exact same effect. The second expression avoids code clutter, though.
    – Mico
    Commented Jan 21, 2020 at 11:00
5

I propose this, with an empty column to separate the groups, siunitx and makecell for the column heads:

\documentclass{article}
\usepackage{makecell} 
\usepackage{booktabs}
\usepackage{siunitx} 

\begin{document}

\begin{table}
\sisetup{table-format=2.0, table-number-alignment=center}
\begin{tabular}{@{}SSSc@{\enspace}SS}
\toprule
\multicolumn{3}{@{}c}{\thead{gamma \\ function}} & & \multicolumn{2}{@{\enspace}c}{\makebox[0pt]{\thead{zeta and\\ gamma \\ functions}}} \\
\midrule
1 & 1 & 1 & & 1 & 1 \\
2 & 3 & 4 & & 5 & 6 \\
4 & 7 & 8 & & 43 & 21 \\
\bottomrule
\end{tabular}
\end{table}

\end{document} 

enter image description here

2
  • 1
    Many many wishes for your reputation of 200k: auguriiiiiiiiiiiiiii !
    – Sebastiano
    Commented Dec 15, 2019 at 21:57
  • 2
    Mille grazie!!!
    – Bernard
    Commented Dec 15, 2019 at 22:00
0

The environment {NiceTabular} of nicematrix has a key columns-width=auto to enforce the same width to all the columns.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{NiceTabular}{@{}SSSSS@{}}[columns-width=auto]
\toprule
\Block{1-3}{gamma\\ function} &&& 
\Block{1-2}{zeta and\\ gamma\\ functions} \\
\midrule
1 & 1 & 1 & 1 & 1 \\
2 & 3 & 4 & 5 & 6 \\
4 & 7 & 8 & 43 & 21 \\
\bottomrule
\end{NiceTabular}

\end{document}

Output of the above code

You must log in to answer this question.

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