3

hope you all feeling well and safe

please help with the code below, the issue is with the cell width of last 3 column (3rd row onward), I want them to have similar width. please find attached code and picture

the problem

how I want it to look like

\begin{table}[t]
    \centering
    \caption{Ranges and levels on independent variables}
    \label{tab:activation ranges}
    \begin{tabular}{|c|c|c|c|}
        \hline
        \multirow{2}{*}{\textbf{Independent Variable}} & \multicolumn{3}{c}{\textbf{Ranges and Coded Levels}} \\ \cline{2-4} 
        & \textbf{-1}      & \textbf{0}      & \textbf{+1}     \\ \hline
        Acid concentration (\%)                        &25               & 55              & 85              \\ \hline
        Temperature (°C)                               & 45               & 80              & 115             \\ \hline
        Residence time (min)                           & 60               & 120             & 180             \\ \hline
    \end{tabular}
\end{table}
1
  • 1
    Welcome to TeX.SE.
    – Mico
    Aug 3, 2020 at 23:49

2 Answers 2

3

One can measure the width of the string \textbf{Ranges and Coded Levels} and set the widths of columns 2 thru 4 to take up (roughly) one third of that width.

A separate recommendation: Do get rid of all vertical rules, and do replace the basic \hline and \cline directives with fewer but better-spaced lines using the macros of the booktabs package: \toprule, \midrule, \cmidrule, and \bottomrule.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{array} % for 'w' column type
\newlength\mylen
\settowidth\mylen{\textbf{Ranges and Coded Levels}}
\addtolength\mylen{-4\tabcolsep}
\setlength{\mylen}{\dimexpr\mylen/3\relax}

\begin{document}
\begin{table}[t]
    \centering
    \caption{Ranges and levels on independent variables\strut}
    \label{tab:activation ranges}
    
    \begin{tabular}{@{} l *{3}{w{c}{\mylen}} @{}}
    \toprule
    \textbf{Independent Variable} & 
    \multicolumn{3}{c@{}}{\textbf{Ranges and Coded Levels}} \\    
    \cmidrule(l){2-4} 
    & $\mathbf{-1}$ & \textbf{0} & $\mathbf{+1}$ \\ 
    \midrule
    Acid concentration (\%) & 25 &  55 &  85 \\ %\hline
    Temperature (°C)        & 45 &  80 & 115 \\ %\hline
    Residence time (min)    & 60 & 120 & 180 \\ 
    \bottomrule %\hline
    \end{tabular}
\end{table}
\end{document}
2
  • 1
    Thank you for help, would you please elaborate more on these three lines, I want to further understand them \addtolength\mylen{-4\tabcolsep} \setlength{\mylen}{\dimexpr\mylen/3\relax} and the line \begin{tabular}{@{} l *{3}{w{c}{\mylen}} @{}}
    – Mubeen
    Aug 4, 2020 at 0:37
  • 2
    @Mubeen - Let x be the sought-after length. In a LaTeX tabular environment, whitespace in the amount of \tabcolsep is insert on either side of a column. The total usable width, taken up by the string "Ranges and Coded Levels", consists of 3x+4\tabcolsep, as there are 2 interior whitespace areas in a three-column setting. The instruction \addtolength\mylen{-4\tabcolsep} subtracts 4\tabcolsep from \mylen. Since the object of the calculations is a length, not a scalar, \dimexpr is needed to assign the right variable type to \mylen/3.
    – Mico
    Aug 4, 2020 at 2:28
3

I propose this layout with siunitx, so the numbers be right-aligned, and a well-chosen column-width. I added some improvements, such as loading caption to have a more sensible verical space between caption and table, and the first column left-aligned, which looks better in my opinion:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{tabularx, multirow, caption, siunitx}
\usepackage{makecell}

\begin{document}

\begin{table}[t]
\setlength{\extrarowheight}{2pt}
\sisetup{table-format=3.0, table-number-alignment=center, table-column-width=12mm}
\captionsetup{skip=4pt}
\centering
\caption{Ranges and levels on independent variables}
\label{tab:activation ranges}
\begin{tabular}{|l|S|S|S|}
    \hline
    \multirow{2}{*}{\textbf{Independent Variable}} & \multicolumn{3}{@{}c@{}|}{\textbf{Ranges and Coded Levels}} \\ \cline{2-4}
    & {$\mathbf{-1}$} & {\textbf{0}} & {$ \mathbf{+1} $} \\ \hline
    Acid concentration (\%) &25 & 55 & 85 \\ \hline
    Temperature (°C) & 45 & 80 & 115 \\ \hline
    Residence time (min) & 60 & 120 & 180 \\ \hline
\end{tabular}
\end{table}

\end{document} 

enter image description here

2
  • 1
    Thank you for your help, I did use your caption line to adjust the caption spacing would you please elabroate more on this Line \sisetup{table-format=3.0, table-number-alignment=center, table-column-width=12mm} and where did you use xcolor package, as new user of latex, am affriad of using alot of packages since it might conflict with elsevier class.
    – Mubeen
    Aug 4, 2020 at 0:52
  • 2
    It's quite simple: the numbers have 3 digits before the decimal dot & none after, the second argument stipulates the numbers are centred in their columns (the default makes the decimal dot centred) and the third argument fixes the column width (found by trial and error, until there's enough space for the text in \multicolumn above).
    – Bernard
    Aug 4, 2020 at 1:31

You must log in to answer this question.

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