TransWikia.com

Linebreak in multirow using tabularx

TeX - LaTeX Asked on May 15, 2021

I try to place the three first description in a multirow enviroment and include a linebreak.

But I did not succed in doing this without creating an error. Do you have an idea how I could do this?

% page setup 
documentclass[a4paper, 11pt]{article}
usepackage[margin=2cm]{geometry}

% language
usepackage[utf8]{inputenc} 

% graphics
usepackage{array,multirow,graphicx} 

% units 
usepackage{siunitx}

% chemistry 
usepackage{ghsystem}
usepackage[version=4]{mhchem} 

% tables
usepackage{ragged2e} 
usepackage{booktabs, tabularx}
%____________________________________________________________________________________________________________
newcolumntype{B}[1]{>{scriptsizeRaggedRighthspace{0pt}}p{#1}}
newcolumntype{L}{>{scriptsizeRaggedRighthspace{0pt}}X}
newlengthcolwidth
%____________________________________________________________________________________________________________
% figures 
usepackage{float}
%____________________________________________________________________________________________________________

begin{document}

begin{table}[ht!]
settowidthcolwidth{textbf{Solution}} % <-- need to use textbf to get correct measurement
begin{tabularx}{textwidth}{@{} B{colwidth} *{8}{L} @{}} 
toprule 
multirow{2}{*}{textbf{Solution}}   & multirow{2}{*}{textbf{shortstack{Molar mass  [$si{grampermol}$]}}}   & multirow{2}{*}{textbf{ V stock  [$si{milliliter}$] }}&      multicolumn{3}{c}{textbf{Amount of substance [$si{millimol}$]}  }     &       multicolumn{3}{c}{textbf{Mass [$si{gram}$]}  }           
cmidrule(lr){4-6} cmidrule(lr){7-9}
 & & & $ce{KH2PO4}$ & $ce{Na2HPO4}$ & $ce{NaCl}$ & $ce{KH2PO4}$ & $ce{Na2HPO4}$ & $ce{NaCl}$
midrule
$ce{KH2PO4}$ stock     &  num{150}  & $num{1.5}$  & -                 & $num{}$ &  $num{}$ & -         & $num{}$ 
$ce{Na2HPO4}$ stock    &  num{100}  & -             & $num{1}$      & $num{}$ & -            &num{}     & $num{}$ 
bottomrule
end{tabularx}
endtabularx
end{table}

end{document}

here

3 Answers

Here are two alternative suggestions:

enter image description here

documentclass[a4paper, 11pt]{article}
usepackage[margin=2cm]{geometry}

usepackage{array,multirow} 
usepackage{siunitx}

usepackage[version=4]{mhchem} 
usepackage{booktabs, tabularx}

usepackage{makecell}
renewcommand{theadfont}{bfseries}

begin{document}

begin{table}[ht!]
setlength{tabcolsep}{4.5pt}
begin{tabularx}{linewidth}{@{}XS[table-format=3] S[table-format=1.1] *{6}{c} @{}} 
toprule 
thead{Solution}   & {thead{Molar mass{}  [si{grampermol}]}}   & {thead{ V stock{}  [si{milliliter}]}} &      multicolumn{3}{c}{thead{Amount of substance{} [si{millimol}]}  }     &       multicolumn{3}{c}{thead{Mass{} [si{gram}]}  }           
cmidrule(r){4-6} cmidrule(l){7-9}
                     &       &      & ce{KH2PO4} & ce{Na2HPO4} & ce{NaCl} & ce{KH2PO4} & ce{Na2HPO4} & ce{NaCl}
midrule
ce{KH2PO4} stock    &  150  & 1.5  & -           &              &           & -           &              &     
ce{Na2HPO4} stock   &  10   & {-}  & 1           &              & -         &             &              &     
bottomrule         
end{tabularx}
end{table}


begin{table}[ht!]
small
begin{tabular}{@{}l S[table-format=3] S[table-format=1.1] *{6}{c} @{}} 
toprule 
multirow{2.5}{*}{thead{Solution}}   & {multirow{2.5}{*}{thead{Molar mass{}  [si{grampermol}]}}}   & {multirow{2.5}{*}{thead{ V stock{}  [si{milliliter}]}}} &      multicolumn{3}{c}{thead{Amount of substance [si{millimol}]}  }     &       multicolumn{3}{c}{thead{Mass [si{gram}]}  }           
cmidrule(r){4-6} cmidrule(l){7-9}
                     &       &      & ce{KH2PO4} & ce{Na2HPO4} & ce{NaCl} & ce{KH2PO4} & ce{Na2HPO4} & ce{NaCl}
midrule
ce{KH2PO4} stock    &  150  & 1.5  & -           &              &           & -           &              &     
ce{Na2HPO4} stock   &  10   & {-}  & 1           &              & -         &             &              &     
bottomrule         
end{tabular}
end{table}


end{document}

Correct answer by leandriis on May 15, 2021

Here is a solution. I have used multirowcell from the makecell package for the multiline multirows and left the units outside of textbf because units won't be done bold. The second column is fit to the "Molar mass" width.

EDIT: In fact multirow{2}={textbf{Molar mass} {}[$si{grampermol}$] } works as well as multirowcell, and for the other one similar.

documentclass[a4paper, 11pt]{article}
usepackage[margin=2cm]{geometry}

% language
usepackage[utf8]{inputenc} 

% graphics
usepackage{array,multirow,graphicx} 

% units 
usepackage{siunitx}

% chemistry 
usepackage{ghsystem}
usepackage[version=4]{mhchem} 

% tables
usepackage{ragged2e} 
usepackage{booktabs, tabularx}
%____________________________________________________________________________________________________________
newcolumntype{B}[1]{>{scriptsizeRaggedRighthspace{0pt}}p{#1}}
newcolumntype{L}{>{scriptsizeRaggedRighthspace{0pt}}X}
newlengthcolwidth
newlengthcolwidthb
%____________________________________________________________________________________________________________
% figures 
usepackage{float}
%____________________________________________________________________________________________________________
usepackage{makecell}

begin{document}

begin{table}[ht!]
settowidthcolwidth{textbf{Solution}} % <-- need to use textbf to get correct measurement
settowidthcolwidthb{textbf{Molar mass}} % <-- need to use textbf to get correct measurement
begin{tabularx}{textwidth}{@{} B{colwidth} @{} B{colwidthb}@{} *{7}{L} @{}} 
toprule 
 multirow{2}*{textbf{Solution}}   &
 multirowcell{2}[0pt][l]{textbf{Molar mass} {}[$si{grampermol}$] }  &
 multirowcell{2}[0pt][l]{textbf{V mass} {}[si{milliliter}] }  &
    multicolumn{3}{c}{textbf{Amount of substance [$si{millimol}$]}  }     &   
    multicolumn{3}{c}{textbf{Mass [$si{gram}$]}  }    
       
cmidrule(lr){4-6} cmidrule(lr){7-9}
 & & & ce{KH2PO4} & ce{Na2HPO4} & ce{NaCl} & ce{KH2PO4} & ce{Na2HPO4} & ce{NaCl} 
midrule
$ce{KH2PO4}$ stock     &  num{150}  & $num{1.5}$  & -                 & $num{}$ &  $num{}$ & -         & $num{}$ 
$ce{Na2HPO4}$ stock    &  num{100}  & -             & $num{1}$      & $num{}$ & -            &num{}     & $num{}$ 
bottomrule
end{tabularx}
%endtabularx
end{table}

end{document}

enter image description here

Answered by Pieter van Oostrum on May 15, 2021

With {NiceTabular} of nicematrix.

documentclass[a4paper, 11pt]{article}
usepackage[margin=2cm]{geometry}
usepackage{siunitx}
usepackage[version=4]{mhchem} 
usepackage{booktabs}
usepackage{nicematrix}

begin{document}

begin{table}[ht!]
small
begin{NiceTabular}{@{}l S[table-format=3] S[table-format=1.1] *{6}{c} @{}} 
toprule 
Block{2-1}{bfseries Solution} 
& Block{2-1}<bfseries>{Molar mass{} [si{grampermol}]} 
& Block{2-1}<bfseries>{V stock{} [si{milliliter}]} 
& Block{1-3}<bfseries>{Amount of substance [si{millimol}]} &&
& Block{1-3}{bfseries Mass [si{gram}]} 
cmidrule(r){4-6} cmidrule(l){7-9}
                     &       &      & ce{KH2PO4} & ce{Na2HPO4} & ce{NaCl} & ce{KH2PO4} & ce{Na2HPO4} & ce{NaCl}
midrule
ce{KH2PO4} stock    &  150  & 1.5  & -           &              &           & -           &              &     
ce{Na2HPO4} stock   &  10   & {-}  & 1           &              & -         &             &              &     
bottomrule         
end{NiceTabular}
end{table}

end{document}

Output of the above code

Answered by F. Pantigny on May 15, 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