tabularx 表格部分的中心文本

tabularx 表格部分的中心文本

我想问一下如何将文本置于描述行的中央(而不是表格的其余部分)。这可以减少文本与描述行之间的空间。\cmidrule

\documentclass[a4paper, 11pt]{article}
\usepackage[left=2cm,top=2cm,right=2cm, bottom=2cm]{geometry}
\usepackage[utf8]{inputenc} 
\usepackage{caption}
\usepackage{siunitx}
\usepackage{ragged2e}
\usepackage{booktabs, tabularx} 
\newcolumntype{C}{>{\RaggedRight\hspace{0pt}\arraybackslash}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\newlength\colwidth
\usepackage{float}  
\usepackage[version=4]{mhchem}


\begin{document}


 \begin{table}[H]
\settowidth\colwidth{Solutionn} 
\begin{tabularx}{\textwidth}{@{}P{\colwidth}*  {7}{C} @{}}
\toprule 
\textbf{Solution}     &      \multicolumn{3}{c}{\textbf{amount of substance [$\si{\mol}$]}  }     &       \multicolumn{3}{c}{\textbf{mass of substance [$\si{\gram}$]}  }     &\textbf{Volume of Stock [$\si{\milli\liter}$]}      \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
 & $\ce{KH2PO4}$ & $\ce{Na2HPO4}$ & $\ce{NaCl}$ & $\ce{KH2PO4}$ & $\ce{Na2HPO4}$ & $\ce{NaCl}$\\
\midrule
A  \\
\bottomrule
\end{tabularx}
\endtabularx
\end{table}

\end{document}

图片

答案1

我认为您正在寻找以下内容:

在此处输入图片描述

通过使用\multirow包,您可以定义跨行中相邻单元格中跨越两行(或更多行)文本的单元格。要使某些单元格中的文本水平居中,您可以使用\multicolumn{1}{c}{...}命令。

在下面的 MWE 中也被视为您问题下方的@Mico 评论:

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{caption}
\usepackage{siunitx}
\usepackage{ragged2e}
\usepackage{booktabs, multirow, tabularx}
\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}\arraybackslash%
                     \hsize=#1\hsize}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\NewExpandableDocumentCommand\mcc{O{c}m}{\multicolumn{1}{#1}{#2}}
\newlength\colwidth
\usepackage{float}
\usepackage[version=4]{mhchem}

\begin{document}
    \begin{table}[ht]   % <---
    \settowidth\colwidth{Solutionn}
    \setlength\tabcolsep{3pt}
\begin{tabularx}{\textwidth}{@{} P{\colwidth} *{6}{L{0.98}} L{1.12} @{}}
    \toprule
\multirow{2.4}{=}{\textbf{Solution}}
    & \multicolumn{3}{c}{\textbf{amount of substance [\si{\mol}]} }     
        & \multicolumn{3}{c}{\textbf{mass of substance [\si{\gram}]} }     
            & \multirow{2.4}{=}{\textbf{Volume of Stock [\si{\milli\liter}]}}\\
    \cmidrule(lr){2-4} \cmidrule(lr){5-7}
    & \mcc{\ce{KH2PO4}} & \mcc{\ce{Na2HPO4}} & \mcc{\ce{NaCl}} 
    & \mcc{\ce{KH2PO4}} & \mcc{\ce{Na2HPO4}} & \mcc{\ce{NaCl}}    &    \\
    \midrule
A  &    &   &   &   &   &   &                       \\
    \bottomrule
\end{tabularx}
%\endtabularx % superfluous 
    \end{table}
\end{document}

答案2

创建快捷宏很方便,例如,

\newcolumntype{C}{>{\Centering\hspace{0pt}}X}

并写入\mC{\ce{KH2PO4}}仅覆盖该单元格的默认格式。

在此处输入图片描述

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage{caption,siunitx,ragged2e,booktabs,tabularx} 
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X}
\newcolumntype{C}{>{\Centering\hspace{0pt}}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\usepackage[version=4]{mhchem}

\newlength\colwidth
\newcommand\mC[1]{\multicolumn{1}{C}{#1}} % handy shortcut macro

\begin{document}

\begin{table}[ht!]
\settowidth\colwidth{\textbf{Solution}} % <-- need to use \textbf to get correct measurement
\begin{tabularx}{\textwidth}{@{} P{\colwidth} *{7}{L} @{}}
\toprule 
\textbf{Solution} &  
\multicolumn{3}{c}{\textbf{Amount of substance} [\si{\mol}]}   &       
\multicolumn{3}{c}{\textbf{Mass of substance} [\si{\gram}]}   &
\multicolumn{1}{C@{}}{\textbf{Volume of stock} [\si{\milli\liter}]}      \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& \mC{\ce{KH2PO4}} & \mC{\ce{Na2HPO4}} & \mC{\ce{NaCl}}
& \mC{\ce{KH2PO4}} & \mC{\ce{Na2HPO4}} & \mC{\ce{NaCl}} \\
\midrule
A \\
B \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

相关内容