1

I have the following example code, and I want all the columns to have the same width:

enter image description here

\documentclass[xcolor=table]{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{tabularx,booktabs}

\title{dd} \author{david.moldes } \date{June 2023}

\begin{document}

\end{table}

\maketitle

\section{Introduction}

\begin{table}\centering \begin{tabular}{@{}lccccccc@{}}\toprule & \multicolumn{7}{c}{\textbf{Levels}}\ \cmidrule{2-8}
\textbf{Factors} & \textit{1} & \textit{2} & \textit{3} & \textit{4} & \textit{5} & \textit{6} & \textit{7}\ \midrule \textit{A} & Water & ChCl:2Gly & ChCl:2Urea & --- & --- & --- & --- \ \textit{B}& 0 & 0.40 & 0.57 &0.69 & 0.82 & 0.86 & 0.91 \ \bottomrule \end{tabular}

\end{document}

I tried to use the explanation given here, How to spread columns evenly without fixing table width?, but in a beamer document it does not work.

1 Answers1

1

Uncentered columns

The tabularx package provides a column type X to automatically adjust the width of the columns to a suitable one (ultimately, equating the width of the columns of this type).

Code

\begin{tabularx}{1.5\textwidth}{@{} l *7{X} @{}}  % I've had to do `1.5\textwidth` as a the width to suit my specific renderer. Adjust it according to your needs.
    \toprule
                        &   \multicolumn{7}{c}{\textbf{Levels}}                                                                         \\
    \cmidrule{2-8}
    \textbf{Factors}    &   \textit{1}  &   \textit{2}  &   \textit{3}  &   \textit{4}  &   \textit{5}  &   \textit{6}  &   \textit{7}  \\
    \midrule
    \textit{A}          &   Water       &   ChCl:2Gly   &   ChCl:2Urea  &   ---         &   ---         &   ---         &   ---         \\
    \textit{B}          &   0           &   0.40        &   0.57        &   0.69        &   0.82        &   0.86        &   0.91        \\
    \bottomrule
\end{tabularx}

Image

Table with columns of type X (uncentered)

Centered columns

Although, every column of type X is automatically left-aligned. To keep them centered, use this answer (from another tex stackexchange post).

New code

% \newcolumntype{Y}{>{\centering\arraybackslash}X}  % in your preamble

\begin{tabularx}{1.5\textwidth}{@{} l *7{Y} @{}} % I've had to do 1.5\textwidth as a the width to suit my specific renderer. Adjust it according to your needs. \toprule & \multicolumn{7}{c}{\textbf{Levels}} \ \cmidrule{2-8} \textbf{Factors} & \textit{1} & \textit{2} & \textit{3} & \textit{4} & \textit{5} & \textit{6} & \textit{7} \ \midrule \textit{A} & Water & ChCl:2Gly & ChCl:2Urea & --- & --- & --- & --- \ \textit{B} & 0 & 0.40 & 0.57 & 0.69 & 0.82 & 0.86 & 0.91 \ \bottomrule \end{tabularx}

New image

Table with columns of type X (centered)

Hope this is what you were looking for!