[Tex/LaTex] table with many columns

tablestabularx

I have some questions about the below table:

  1. If I omit the margins, the table will completely get disordered.
  2. I want to have the two 4 columns in the middle to have a slight distance with each other but the \cmidrule is not working properly
  3. as you can see at the right side of the table the data have come out of the table.

I will be more than grateful if you kindly help me solve the problems.

\documentclass{article} [8pt]
\usepackage[margin=1cm]{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{mhchem} 
\usepackage{siunitx}
\usepackage{bm}
\begin{document}

\begin{table*}[htb]
  \label{tbl:stats-and-correlations}

  \begin{tabularx}{\linewidth}{l*{8}{c}}
    \toprule
    & \multicolumn{4}{c}{\textbf{Panel A:}} 
     & \multicolumn{4}{c}{\textbf{Panel B:}}  \\ \cmidrule{2-5} \cmidrule{6-9}

               & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $}
               & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} \\ [0.5ex]
               & (\si{\angstrom})
               & (\si{\angstrom})
               & (\si{\angstrom})
               & (\si{\angstrom})
               & (\si{\angstrom})
               & (\si{\angstrom})
               & (\si{\angstrom})
               & (\si{\angstrom}) \\
               \hline
    Variable~1 &    0.78 &    0.37 &    0.48 &    0.10 &    0.13 &    0.58 &    0.41 &    0.10\\
    Variable~2 &    0.46 &    0.86 &    0.96 &    0.44 &    0.15 &    0.56 &    0.31 &    0.10\\
    Variable~3 &    0.03 &    0.75 &    0.11 &    0.44 &    0.71 &    0.06 &    0.26 &    0.10\\
    Variable~4 &    0.21 &    0.25 &    0.38 &    0.88 &    0.24 &    0.52 &    0.46 &    0.10\\
    Variable~5 &    0.20 &    0.93 &    0.54 &    0.96 &    0.55 &    0.82 &    0.62 &    0.10\\
    Variable~6 &    0.67 &    0.85 &    0.74 &    0.99 &    0.27 &    0.48 &    0.85 &    0.10\\
    Variable~7 &    0.82 &    0.89 &    0.68 &    0.06 &    0.02 &    0.30 &    0.10 &    0.10\\
    \bottomrule
  \end{tabularx}


\end{table*}

\end{document}

Best Answer

Your table is wider then the available \linewidth and the tabularx is used without any X column.

One possibility to get the table right is using \small and defining the first column as an X column:

enter image description here

Code:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage[version=3]{mhchem} 
\usepackage{siunitx}
%\usepackage{bm}
\begin{document}
\begin{table*}[htb]
  \label{tbl:stats-and-correlations}
  \small\begin{tabularx}{\linewidth}{X*{8}{c}}
    \toprule
    & \multicolumn{4}{c}{\textbf{Panel A:}} & \multicolumn{4}{c}{\textbf{Panel B:}}\\ \cmidrule(r){2-5} \cmidrule(l){6-9}
    & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} 
    & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} \\ [0.5ex]
    & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) \\
    \midrule
    Variable~1 &    0.78 &    0.37 &    0.48 &    0.10 &    0.13 &    0.58 &    0.41 &    0.10\\
    Variable~2 &    0.46 &    0.86 &    0.96 &    0.44 &    0.15 &    0.56 &    0.31 &    0.10\\
    Variable~3 &    0.03 &    0.75 &    0.11 &    0.44 &    0.71 &    0.06 &    0.26 &    0.10\\
    Variable~4 &    0.21 &    0.25 &    0.38 &    0.88 &    0.24 &    0.52 &    0.46 &    0.10\\
    Variable~5 &    0.20 &    0.93 &    0.54 &    0.96 &    0.55 &    0.82 &    0.62 &    0.10\\
    Variable~6 &    0.67 &    0.85 &    0.74 &    0.99 &    0.27 &    0.48 &    0.85 &    0.10\\
    Variable~7 &    0.82 &    0.89 &    0.68 &    0.06 &    0.02 &    0.30 &    0.10 &    0.10\\
    \bottomrule
  \end{tabularx}
\end{table*}
\end{document}

Note that I have changed \hline to \midrule.


Another possibility is using \small together with tabular* and @{\extracolsep{\fill}} behind the first column definition.

\small\begin{tabular*}{\linewidth}{l@{\extracolsep{\fill}}*{8}{c}}
...
\end{tabular*}

will stretch all columns so that the width of the table fits \linewidth.

enter image description here


Or you can use \resizebox from the graphics bundle if your table width differs not so much from line width.

enter image description here

Code

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage[version=3]{mhchem}% loads also graphicx
\usepackage{siunitx}
%\usepackage{bm}
\begin{document}
\begin{table*}[htb]
  \label{tbl:stats-and-correlations}
  \resizebox{\linewidth}{!}{\begin{tabular}{l*{8}{c}}
    \toprule
    & \multicolumn{4}{c}{\textbf{Panel A:}} & \multicolumn{4}{c}{\textbf{Panel B:}}\\ \cmidrule(r){2-5} \cmidrule(l){6-9}
    & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} 
    & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} & {$d_{(\ce{O1}-\ce{Mg_{(Zn)}})} $} \\ [0.5ex]
    & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) & (\si{\angstrom}) \\
    \midrule
    Variable~1 &    0.78 &    0.37 &    0.48 &    0.10 &    0.13 &    0.58 &    0.41 &    0.10\\
    Variable~2 &    0.46 &    0.86 &    0.96 &    0.44 &    0.15 &    0.56 &    0.31 &    0.10\\
    Variable~3 &    0.03 &    0.75 &    0.11 &    0.44 &    0.71 &    0.06 &    0.26 &    0.10\\
    Variable~4 &    0.21 &    0.25 &    0.38 &    0.88 &    0.24 &    0.52 &    0.46 &    0.10\\
    Variable~5 &    0.20 &    0.93 &    0.54 &    0.96 &    0.55 &    0.82 &    0.62 &    0.10\\
    Variable~6 &    0.67 &    0.85 &    0.74 &    0.99 &    0.27 &    0.48 &    0.85 &    0.10\\
    Variable~7 &    0.82 &    0.89 &    0.68 &    0.06 &    0.02 &    0.30 &    0.10 &    0.10\\
    \bottomrule
  \end{tabular}}
\end{table*}
\end{document}

Additionally you can save space by removing the left \tabcolsep of the first column and the right \tablcolsep of the last column: add @{} at the begin and the end of the column definitions. That means for the last example

\resizebox{\linewidth}{!}{\begin{tabular}{@{}l*{8}{c}@{}}
...
\end{tabular}}

enter image description here

Related Question