TransWikia.com

Fix table cells width and alignment

TeX - LaTeX Asked by Mubeen on December 21, 2020

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}

2 Answers

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
newlengthmylen
settowidthmylen{textbf{Ranges and Coded Levels}}
addtolengthmylen{-4tabcolsep}
setlength{mylen}{dimexprmylen/3relax}

begin{document}
begin{table}[t]
    centering
    caption{Ranges and levels on independent variablesstrut}
    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}

Correct answer by Mico on December 21, 2020

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

Answered by Bernard on December 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP