1

I am creating a table that contains more than one multicolumn command. How can I specify the width of the columns for the whole table collectively?

\begin{tabular}{rrrrr} \toprule 
 \multicolumn{1}{l}{\bfseries Data}  & {\textbf{data1}} & {\textbf{data2}} & {\textbf{data3}} &{\textbf{data4}}   \bigstrut\\ 
  \midrule 
  \\ \midrule \multicolumn{5}{l}{\bfseries Panel A}  \\ \midrule\\ 
 \multicolumn{1}{l}{\textbf{Sample1}}& 0.1503&0.1789&0.1929&0.2026&\\ 
 \multicolumn{1}{l}{\textbf{Sample2}} & 0.1217&0.1503&0.1640&0.1723&\\ 
 \\ \midrule \multicolumn{5}{l}{\bfseries Panel B}  \\ \midrule\\ 
 \multicolumn{1}{l}{\textbf{Sample1}}& 0.1492&0.1729&0.1831&0.1894&\\ 
 \multicolumn{1}{l}{\textbf{Sample2}} & 0.1034&0.1305&0.1434&0.1504&\\ 
 \\ \bottomrule \\ 
 \end{tabular}

The current output look like this. What I would like to achieve is fixing the width for the first column, then using the same width for the rest of columns. I know this is something to do with tabularx, but so far keep getting errors and haven't managed to compile the file.

enter image description here

2
  • Which multicolumn command do you refer to? The o e that spans 5 columns? If so, why would you need a specific width there? Or so you refer to the multicolumns on the first column? Simply remove them and use an l instead of an r type column. Also, why would you want to specify a width here?
    – leandriis
    Commented Jul 8, 2020 at 16:31
  • Yes, I meant the multicolumn command which spens 5 columns.The reason for fixing the specific width is that I am creating the same tables 5 times, each time with different entries. If I use r-type column instead, the width changes depending on the entries that looks not very good when I compile the file into one document.
    – yufiP
    Commented Jul 8, 2020 at 16:41

2 Answers 2

1
  • You not provide any information about your document.
  • Your code fragment contain errors. You define 5 columns, but in the table you use 6 columns.
  • Most of \multicolumn{...}{...}{...} in table are superfluous
  • A possible MWE with your table can be:
\documentclass{article}
\usepackage{booktabs, makecell, tabularx}
\renewcommand\theadfont{\normalsize\bfseries}
\usepackage{siunitx}

\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c}{#2}}


\begin{document}
\begin{tabularx}{0.65\linewidth}{X *{4}{S[table-format=1.4]} } 
    \toprule
\thead{Data}    
    & {\thead{data1}}   & {\thead{data2}}   
    & {\thead{data3}}   & {\thead{data4}}           \\
  \midrule
\mcc[5]{\thead{Panel A}}                            \\ 
    \midrule
\thead{Sample1}
    & 0.1503    & 0.1789    & 0.1929    & 0.2026    \\
\multicolumn{1}{l}{\thead{Sample2}}
    & 0.1217    & 0.1503    & 0.1640    & 0.1723    \\
    \midrule
\mcc[5]{\thead{Panel B}}                            \\
    \midrule
\thead{Sample1}
    & 0.1492    & 0.1729    & 0.1831    & 0.1894    \\
\thead{Sample2}
    & 0.1034    & 0.1305    & 0.1434    & 0.1504    \\
    \bottomrule     
 \end{tabularx}
\end{document}

Which gives:

enter image description here

1
  • Thank you very much for pointing our the errors and providing the example. I managed to create the table that I wanted by arranging your code.
    – yufiP
    Commented Jul 8, 2020 at 17:11
1

I propose to use a usual tabular environnment, and to load siunitx and eqparbox. I define the last four columns as S type, and set the table-column-width key to the width of the longest cell in the first column.

Also I reorganised the rules and vertical spacings in the table.

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{eqparbox}
\usepackage{bigstrut}

\begin{document}

\begin{tabular}{>{\bfseries}l *{4}{S[table-format=1.4,table-column-width=\eqboxwidth{S}]}}%
 \toprule
 Data & {\textbf{data1}} & {\textbf{data2}} & {\textbf{data3}} &{\textbf{data4}}\\%\bigstrut
  \midrule
  \addlinespace[2ex]
 \multicolumn{5}{l}{\bfseries Panel A} \\ \midrule\addlinespace
 Sample1 & 0.1503 & 0.1789 & 0.1929 & 0.2026 \\
 \eqparbox{S}{\textbf{Sample2}} & 0.1217 & 0.1503 & 0.1640 & 0.1723 \\
 \addlinespace[2ex]
 \multicolumn{5}{l}{\bfseries Panel B} \\ \midrule\addlinespace
Sample1 & 0.1492 & 0.1729 & 0.1831 & 0.1894 \\
{Sample2} & 0.1034 & 0.1305 & 0.1434 & 0.1504 \\
 \\ \bottomrule
 \end{tabular}

\end{document}

enter image description here

You must log in to answer this question.

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