使用 tabularx 在多行中进行换行

使用 tabularx 在多行中进行换行

我尝试将前三个描述放在多行环境中并包含换行符。

但我没能成功,而且没有产生错误。你知道我该怎么做吗?

% 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]{>{\scriptsize\RaggedRight\hspace{0pt}}p{#1}}
\newcolumntype{L}{>{\scriptsize\RaggedRight\hspace{0pt}}X}
\newlength\colwidth
%____________________________________________________________________________________________________________
% figures 
\usepackage{float}
%____________________________________________________________________________________________________________

\begin{document}

\begin{table}[ht!]
\settowidth\colwidth{\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{\gram\per\mol}$]}}}   & \multirow{2}{*}{\textbf{ V stock \\ [$\si{\milli\liter}$] }}&      \multicolumn{3}{c}{\textbf{Amount of substance [$\si{\milli\mol}$]}  }     &       \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}

这里

答案1

以下是两个备选建议:

在此处输入图片描述

\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{\gram\per\mol}]}}   & {\thead{ V stock\\{}  [\si{\milli\liter}]}} &      \multicolumn{3}{c}{\thead{Amount of substance\\{} [\si{\milli\mol}]}  }     &       \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{\gram\per\mol}]}}}   & {\multirow{2.5}{*}{\thead{ V stock\\{}  [\si{\milli\liter}]}}} &      \multicolumn{3}{c}{\thead{Amount of substance [\si{\milli\mol}]}  }     &       \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}

答案2

这是一个解决方案。我使用了多行包\multirowcell中的s,并将单位留在了 s 之外,因为单位不会加粗。第二列适合“摩尔质量”宽度。\makecell\multirow\textbf

编辑:实际上\multirow{2}={\textbf{Molar mass} \\{}[$\si{\gram\per\mol}$] }效果与一样好\multirowcell,并且对于另一个来说也类似。

\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]{>{\scriptsize\RaggedRight\hspace{0pt}}p{#1}}
\newcolumntype{L}{>{\scriptsize\RaggedRight\hspace{0pt}}X}
\newlength\colwidth
\newlength\colwidthb
%____________________________________________________________________________________________________________
% figures 
\usepackage{float}
%____________________________________________________________________________________________________________
\usepackage{makecell}

\begin{document}

\begin{table}[ht!]
\settowidth\colwidth{\textbf{Solution}} % <-- need to use \textbf to get correct measurement
\settowidth\colwidthb{\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{\gram\per\mol}$] }  &
 \multirowcell{2}[0pt][l]{\textbf{V mass} \\{}[\si{\milli\liter}] }  &
    \multicolumn{3}{c}{\textbf{Amount of substance [$\si{\milli\mol}$]}  }     &   
    \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}

在此处输入图片描述

答案3

与。{NiceTabular}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{\gram\per\mol}]} 
& \Block{2-1}<\bfseries>{V stock\\{} [\si{\milli\liter}]} 
& \Block{1-3}<\bfseries>{Amount of substance\\ [\si{\milli\mol}]} &&
& \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}

上述代码的输出

相关内容