TransWikia.com

How can I make these table columns adaptive so that they remain centered?

TeX - LaTeX Asked on June 16, 2021

I’m trying to create a table that can adapt its column height so that when the text length increases, column-width stays the same but column height adapts. Also, I’d like this table to have the text remain centered.

Here’s what I’ve got so far. The problem is that when there is more text in the middle column in doesn’t translate to the next line or remain centered as I’d like it to.

documentclass{article}

usepackage{array}
    newcolumntype{P}[1]{>{centeringarraybackslash}p{#1}}
    newcolumntype{M}[1]{>{centeringarraybackslash}m{#1}}
usepackage{multirow}


begin{table}[h]
    centering
    begin{tabular}[t]{|M{5cm}|P{5cm}|M{5cm}|}
    hline
    multirow{2}{*}{textbf{Control Variables}}   &   multirow{2}{*}{textbf{Description}}   & multirow{2}{*}{textbf{Value}} 
    && hline % End of Header
    multirow{3}{*}{text{Humidity}} & multirow{3}{*}{text{Humidity}} & multirow{3}{*}{text{Humidity}} 
    &&
    && hline 
    multirow{3}{*}{text{Temperature inside Freezer}} & multirow{3}{*}{text{Humidity}} & multirow{3}{*}{text{Humidity}} 
    &&
    && hline
    multirow{3}{*}{text{Room Temperature}} & multirow{3}{*}{text{Nothing.}} & multirow{3}{*}{text{Humidity}} 
    &&
    && hline
    end{tabular}
    caption{Caption}
    label{tab:my_label}
end{table}

Any suggestions are really appreciated.

2 Answers

enter image description here

documentclass[]{article}

usepackage{booktabs,lipsum} 

newcolumntype{P}[1]{>{centeringarraybackslash}p{#1}}
newcolumntype{M}[1]{>{centeringarraybackslash}m{#1}}
begin{document}
    
    begin{table}
        caption{Scenario}label{tab3}
        begin{tabular}{  M{3cm}P{5cm}M{3cm}}
            toprule
            Col 1& Col 2 &Col3 
            cmidrule(l){1-1}cmidrule(l){2-2}cmidrule(l){3-3}
            addlinespace
            addlinespace
            1  &lipsum[1]& 3  
            addlinespace
            4 &5 & 6
            bottomrule
        end{tabular}
    end{table}
    
end{document}

Answered by js bibra on June 16, 2021

"column-width stays the same but column height adapts" and "want the rows to be equally tall" seem to be somewhat contradicting requests.

In the following MWE, cell's contents are vertically and horizontally centered, while the row height adapts to the length of the text inside of them:

enter image description here

documentclass{article}

usepackage{array}
    newcolumntype{P}[1]{>{centeringarraybackslash}p{#1}}
    newcolumntype{M}[1]{>{centeringarraybackslash}m{#1}}
usepackage{multirow}

begin{document}
begin{table}[h]
setlengthextrarowheight{3pt}
    centering
    begin{tabular}[t]{|M{3.5cm}|P{3.5cm}|M{3.5cm}|}
    hline
    textbf{Control Variables}   &   textbf{Description}   & textbf{Value}[3pt] hline % End of Header
    Humidity & Humidity & Humidity [3pt]hline 
    Temperature inside Freezer & Humidity & Humidity [3pt] hline
    Room Temperature & Nothing. & Humidity [3pt]hline
    end{tabular}
    caption{Caption}
    label{tab:my_label}
end{table}

end{document}

Answered by leandriis on June 16, 2021

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