2

Dear experienced friends,

I met a strange bug when I tried to draw a table. As shown in the picture, I wish I can make all the columns 4,5,6,1,2,3 evenly distributed. However, the width of 6 is always larger than others. May I ask why this bug happens, and how can I fix it? Thank you!

enter image description here

\documentclass{article}

\usepackage[utf8]{inputenc} \usepackage[table]{xcolor} \usepackage{array} \usepackage{multirow} \usepackage{graphicx} \usepackage{booktabs} \usepackage{tabularx}

\begin{document}

\setlength{\heavyrulewidth}{1.2pt} \setlength{\abovetopsep}{2pt} \setlength\extrarowheight{2pt}

\begin{table}[htbp!] \centering \scalebox{1}{

\begin{tabular}{c c c c @{\extracolsep{0.2cm}} c c c} \toprule \multirow{2}{4em}{} & \multicolumn{3}{c}{Cubic Height Height} & \multicolumn{3}{c}{Width Width}\[1ex]

\cmidrule{2-4} \cmidrule{5-7}

Model & 4 & 5 & 6 & 1 & 2 & 3 \ [1ex]

\midrule A & 0 & 0 & 0 & 0 & 0 & 0 \

B & 0 & 0 & 0 & 0 & 0 & 0 \

C & 0 & 0 & 0 & 0 & 0 & 0 \

\bottomrule \hline \end{tabular}}

\caption{Comparison} \label{table 2} \end{table}

\end{document}

( I also tried \begin{tabular}{p{3.2cm} p{0.8cm} p{0.8cm} p{0.8cm} @{\extracolsep{0.15cm}} p{0.8cm} p{0.8cm} p{0.8cm}}, but it didn't work. )

  • 1
    Welcome to TeX.SE. – Mico Jul 19 '21 at 01:24
  • One approach that does not require manually specifying column width is https://tex.stackexchange.com/a/515973/250119 or the linked question there (basically such a solution must measure things. There's also a solution there using the new package tabularray) – user202729 Dec 18 '21 at 16:20

1 Answers1

4

You need to set a fixed column width -- the c column type does not have an ex ante fixed width. Since you're using the array package, you could use its w column type, with a width of 8mm or more, for this purpose. (Aside: I came up with 8mm via trial and error.)

enter image description here

\documentclass{article}
%\usepackage[utf8]{inputenc} % that's the default nowadays
\usepackage{array,booktabs}

\begin{document} \begin{table}[htbp!] \centering \begin{tabular}{l *{6}{w{c}{8mm}} } \toprule & \multicolumn{3}{c}{Cubic Height Height} & \multicolumn{3}{c}{Width Width}\ \cmidrule(lr){2-4} \cmidrule(l){5-7} Model & 4 & 5 & 6 & 1 & 2 & 3 \ \midrule A & 0 & 0 & 0 & 0 & 0 & 0 \ B & 0 & 0 & 0 & 0 & 0 & 0 \ C & 0 & 0 & 0 & 0 & 0 & 0 \ \bottomrule \end{tabular} \caption{Comparison}\label{table 2} \end{table} \end{document}

Mico
  • 506,678
  • Your answer perfectly solved my problem! Thank you so much, Mico! May I ask a follow-up question? Does {l *{6}{w{c}{8mm}} } means that you set 6 left-aligned cells, and all of them are 8mm width? What if I want to set the first one as a 5mm center-aligned one? Should I use {c *{1}{w{c}{5mm}} | l *{5}{w{c}{8mm}} }? Thank you! – Nick Nick Nick Jul 19 '21 at 01:49
  • 1
    @NickChen - You're most welcome. The table has 7 columns in total. l denotes "variable-width left-aligned", *{6}{...} means "6 times {...}, and w{c}{8mm} denotes "fixed-width (8mm wide) centered". If you want the first column to be exactly 5mm wide and center-aligned, you'd replace l with w{c}{5mm}. "Variable-width" means "as wide as the widest cell in that column"; in the present case, that's the width of the word "Model". More information about the w column type may be found in the user guide of the [array])https://www.ctan.org/pkg/array) package. – Mico Jul 19 '21 at 05:17
  • 1
    Your answer is very helpful. I just tried it on my script and it works perfectly. Thanks a lot, Mico! – Nick Nick Nick Jul 19 '21 at 14:09