如何使 tabularx 表居中?

如何使 tabularx 表居中?

我有一个很大的有机化学实验室数据表,想让它看起来尽可能美观。但是,并非所有标题都居中。

% Organic Chemistry II Lab Template

\documentclass{article}

\usepackage[letterpaper, portrait, margin=2cm]{geometry}
\usepackage[english]{babel}
\usepackage[protrusion=true,expansion=true]{microtype}  
\usepackage{amsmath,amsfonts,amsthm,amssymb}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[svgnames,table,xcdraw,dvipsnames]{xcolor} 
\usepackage[version=3]{mhchem}
\usepackage{chemfig}
\usepackage{gensymb}
\usepackage[most]{tcolorbox}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{array}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{makecell}
\usepackage{floatflt}
\usepackage{wrapfig}
\usepackage{fancyhdr}
\usepackage{epstopdf}
\usepackage{booktabs}
\usetikzlibrary{calc,shadings,patterns}
\pagenumbering{roman}


\begin{document} 
\thispagestyle{empty}               % Remove page numbering on this page
\titleGP % This command includes the title page
\clearpage
\setcounter{page}{1}

\pagestyle{fancy}
\fancyhf{}
\rhead{J. Weinraub}
\lhead{Experiment XXIV: Aldehydes and Ketones}
\cfoot{\thepage}
\section*{Introduction}

\section*{Results}

\begin{table}[h]
\begin{tabularx}{\linewidth}{|c|*{3}{X|>{\centering\arraybackslash}X|}}
\hline
\multicolumn{5}{|c|}{Lab Practicum Organic Chemistry II}                                                                                                                                                                          \\ \hline
\multicolumn{1}{|c|}{\textbf{Compound}} & \multicolumn{2}{c|}    {\textbf{Solubility}}                                                        &     \multicolumn{2}{c|}{\textbf{Density}}                                                 \\ \hline
\multicolumn{1}{|c|}{}                  & \textit{\textbf{Observation}}                       & \textit{\textbf{Conclusion}}                  & \textit{\textbf{Observation}}     & \multicolumn{1}{c|}{\textit{\textbf{Conclusion}}} \\ \hline
\textbf{Acetone}                        & Miscible                                            & \cellcolor[HTML]{000000}                      & Miscible                          & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{1-Butanal}                      & $\sim$8-9 drops                                     & \cellcolor[HTML]{000000}                      & Floats                            & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{2-Butanone}                     & \textless10 drops                                   & \cellcolor[HTML]{000000}                      & Floats                            & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{Cyclohexanone}                  & 6 drops                                             & \cellcolor[HTML]{000000}                      & Sinks                             & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{Benzaladehyde}                  & \textgreater10 drops                                & \cellcolor[HTML]{000000}                      & Sinks                             & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{Unknown \#6}                    & 4-5 drops                                           & Boils at 75                                   & Floats                            &                                                   \\ \hline
                                        & \multicolumn{2}{c|}{\textbf{2,4-    Dinitrophenylhydrazone}}                                        &     \multicolumn{2}{c|}{\textbf{Semicarbazones}}                                          \\ \hline
\textbf{Acetone}                        & Solid                                               & \cellcolor[HTML]{000000}                      & ---                               & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{Benzaldehyde}                   & Solid                                               & \cellcolor[HTML]{000000}                      & Slight ppt                        & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{Unknown \#6}                    & Solid                                               &                                               & Precipitated                      &                                                   \\ \hline
                                        & \multicolumn{2}{c|}    {\textbf{Chromic Acid Oxidation}}                                            &     \multicolumn{2}{c|}{\textbf{Tollen's Test}}                                           \\ \hline
\textbf{Acetone}                        & Positive                                            & \cellcolor[HTML]{000000}                      & Negative                          & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{1-Propanal}                     & Negative                                            & \cellcolor[HTML]{000000}                      & Positive                          & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{Benzaldehyde}                   & Positive                                            & \cellcolor[HTML]{000000}                      & Positive                          & \cellcolor[HTML]{000000}                          \\ \hline
\textbf{Glucose}                        & \cellcolor[HTML]    {C0C0C0}\textbf{Skip This Test} & \cellcolor[HTML]{000000}                          & Positive                      & \cellcolor[HTML]{000000}                              \\ \hline
\textbf{Unknown \#6}                    & Positive                                            &                                               & Positve                           &                                                   \\ \hline
                                        & \multicolumn{2}{c|}    {\textbf{Iodoform Test}}                                                     &     \multicolumn{1}{c|}{}         &                                                       \\ \hline
\textbf{Acetone}                        & Precipitated                                        & \multicolumn{1}{c|}{\cellcolor[HTML]{000000}} & \multicolumn{1}{c|}{}             &                                                   \\ \hline
\textbf{Ethanol}                        & No Reaction                                         & \multicolumn{1}{c|}{\cellcolor[HTML]{000000}} & \multicolumn{1}{c|}{}             &                                                   \\ \hline
\textbf{Cyclohexanone}                  & No Reaction                                         & \multicolumn{1}{c|}{\cellcolor[HTML]{000000}} & \multicolumn{1}{c|}{}             &                                                   \\ \hline
\textbf{Unknown \#6}                    & No Reaction                                         & \multicolumn{1}{c|}{}                         & \multicolumn{1}{c|}{}             &                                                   \\ \hline
\end{tabularx}
\end{table}
\end{document}

我的表格现在的样子

如果您认为这是正确的做法,我希望所有字段都居中或格式化得比现在更好。

答案1

也许这至少能提供一些想法。我不确定表格的最终内容会是什么样子,但我认为一些空单元格最终会被填满,这会改变一切。至少,这是我在这里的假设。

makecells这既是对和的滥用booktabs\midrule不应这样使用。 也不应这样使用\diaghead

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{array}
\usepackage{makecell}
\usepackage{booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\renewcommand\theadfont{\bfseries}% bold tabular headers
\renewcommand\theadalign{lcc}
\renewcommand\theadgape{}% booktabs rules already add vertical spacing
\newcommand\tsubheadfont{\itshape\bfseries}
\newcommand*\tsubhead[1]{\tsubheadfont #1}
\begin{document}
\begin{table}
  \caption{Lab Practicum Organic Chemistry II}\label{tab:pracII}
  \begin{tabularx}{\linewidth}{>{\bfseries\arraybackslash}l*{4}{C}}
    \toprule
    \thead{Compound} & \multicolumn{2}{c} {\theadfont Solubility} & \multicolumn{2}{c}{\theadfont Density} \\\cmidrule(lr){2-3}\cmidrule(lr){4-5}
     & \tsubhead{Observation} & \tsubhead{Conclusion} & \tsubhead{Observation} & \tsubhead{Conclusion} \\
    \midrule
    Acetone & Miscible & \multirow{5}*{\diaghead(2,1){\hskip\hsize}{}{}} & Miscible & \multirow{5}*{\diaghead(2,1){\hskip\hsize}{}{}} \\
    1-Butanal & $\sim$8-9 drops &  & Floats &  \\
    2-Butanone & \textless10 drops &  & Floats &  \\
    Cyclohexanone & 6 drops &  & Sinks &  \\
    Benzaladehyde & \textgreater10 drops &  & Sinks &  \\
    Unknown \#6 & 4-5 drops & Boils at 75 & Floats & \\
    \midrule
    & \multicolumn{2}{c}{\theadfont {2,4- Dinitrophenylhydrazone}} & \multicolumn{2}{c}{\theadfont {Semicarbazones}} \\
    \midrule
    Acetone & Solid & \multirow{3}*{\diaghead(4,1){\hskip\hsize}{}{}} & --- & \multirow{2}*{\diaghead(5,1){\hskip\hsize}{}{}} \\
    Benzaldehyde & Solid &  & Slight ppt & \\
    Unknown \#6 & Solid & & Precipitated & \\
    \midrule
    & \multicolumn{2}{c} {\theadfont Chromic Acid Oxidation} & \multicolumn{2}{c}{\theadfont {Tollen's Test}} \\
    \midrule
    Acetone & Positive & \multirow{4}*{\diaghead(3,1){\hskip\hsize}{}{}} & Negative & \multirow{4}*{\diaghead(3,1){\hskip\hsize}{}{}} \\
    1-Propanal & Negative &  & Positive &  \\
    Benzaldehyde & Positive &  & Positive &  \\
    Glucose & \textbf{Skip This Test} &  & Positive &  \\
    Unknown \#6 & Positive & & Positve & \\
    \midrule
    & \multicolumn{2}{c} {\theadfont {Iodoform Test}} &  & \\
    \midrule
    Acetone & Precipitated & \multirow{3}*{\diaghead(4,1){\hskip\hsize}{}{}} &  & \\
    Ethanol & No Reaction &  & & \\
    Cyclohexanone & No Reaction &  & & \\
    Unknown \#6 & No Reaction &  &  & \\
    \bottomrule
  \end{tabularx}
\end{table}
\end{document}

这产生了

黑客表格

不过,我可能会做一些更简单的事情,例如

更简单的表格

\begin{table}
  \caption{Lab Practicum Organic Chemistry II}\label{tab:pracII}
  \begin{tabularx}{\linewidth}{>{\bfseries\arraybackslash}l*{4}{C}}
    \toprule
    \thead{Compound} & \multicolumn{2}{c} {\theadfont Solubility} & \multicolumn{2}{c}{\theadfont Density} \\\cmidrule(lr){2-3}\cmidrule(lr){4-5}
     & \tsubhead{Observation} & \tsubhead{Conclusion} & \tsubhead{Observation} & \tsubhead{Conclusion} \\
    \midrule
    Acetone & Miscible & --- & Miscible & --- \\
    1-Butanal & $\sim$8-9 drops & --- & Floats & --- \\
    2-Butanone & \textless10 drops & --- & Floats & --- \\
    Cyclohexanone & 6 drops & --- & Sinks & --- \\
    Benzaladehyde & \textgreater10 drops & --- & Sinks & --- \\
    Unknown \#6 & 4-5 drops & Boils at 75 & Floats & \\
    \midrule
    & \multicolumn{2}{c}{\theadfont {2,4- Dinitrophenylhydrazone}} & \multicolumn{2}{c}{\theadfont {Semicarbazones}} \\
    \midrule
    Acetone & Solid & --- & --- & --- \\
    Benzaldehyde & Solid & --- & Slight ppt &--- \\
    Unknown \#6 & Solid &--- & Precipitated & \\
    \midrule
    & \multicolumn{2}{c} {\theadfont Chromic Acid Oxidation} & \multicolumn{2}{c}{\theadfont {Tollen's Test}} \\
    \midrule
    Acetone & Positive & --- & Negative & --- \\
    1-Propanal & Negative & --- & Positive & --- \\
    Benzaldehyde & Positive & --- & Positive &  ---\\
    Glucose & \textbf{Skip This Test} & --- & Positive & --- \\
    Unknown \#6 & Positive & & Positve & \\
    \midrule
    & \multicolumn{2}{c} {\theadfont {Iodoform Test}} &  & \\
    \midrule
    Acetone & Precipitated & --- &  & \\
    Ethanol & No Reaction & --- & & \\
    Cyclohexanone & No Reaction &---  & & \\
    Unknown \#6 & No Reaction & --- &  & \\
    \bottomrule
  \end{tabularx}
\end{table}

答案2

我建议使用两种布局tabularx,一种是带有垂直规则的布局,一种是不带有垂直规则的布局。我加载了booktabs, andcellspace` 来为行提供一些垂直填充:

\documentclass{article}
\usepackage[letterpaper, portrait, margin=2cm]{geometry}
\usepackage[english]{babel}
\usepackage[protrusion=true,expansion=true]{microtype}
\usepackage{amsmath,amsfonts,amsthm,amssymb}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{colortbl} \usepackage[svgnames,table,xcdraw,dvipsnames]{xcolor}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{array} 
\usepackage{booktabs}    
\setlength\cmidrulekern{1em}
\usepackage{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}

\begin{document}

\begin{table}[h]
  \begin{tabularx}{\linewidth}{|>{\bfseries}Sl|*{4}{>{\centering\arraybackslash}X|}}
    \hline
    \multicolumn{5}{|Sc|}{Lab Practicum Organic Chemistry II} \\ \hline
    \multicolumn{1}{|Sc|}{\textbf{Compound}} & \multicolumn{2}{c|} {\textbf{Solubility}} & \multicolumn{2}{c|}{\textbf{Density}} \\ \hline
                  & \textit{\textbf{Observation}} & \textit{\textbf{Conclusion}} & \textit{\textbf{Observation}} & \multicolumn{1}{c|}{\textit{\textbf{Conclusion}}} \\ \hline
    Acetone & Miscible & \cellcolor[HTML]{000000} & Miscible & \cellcolor[HTML]{000000} \\ \hline
    1-Butanal & $\sim$8-9 drops & \cellcolor[HTML]{000000} & Floats & \cellcolor[HTML]{000000} \\ \hline
    2-Butanone & \textless10 drops & \cellcolor[HTML]{000000} & Floats & \cellcolor[HTML]{000000} \\ \hline
    Cyclohexanone & 6 drops & \cellcolor[HTML]{000000} & Sinks & \cellcolor[HTML]{000000} \\ \hline
    Benzaladehyde & \textgreater10 drops & \cellcolor[HTML]{000000} & Sinks & \cellcolor[HTML]{000000} \\ \hline
    Unknown \#6 & 4-5 drops & Boils at 75 & Floats & \\ \hline
    & \multicolumn{2}{c|}{\textbf{2,4- Dinitrophenylhydrazone}} & \multicolumn{2}{c|}{\textbf{Semicarbazones}} \\ \hline
    Acetone & Solid & \cellcolor[HTML]{000000} & --- & \cellcolor[HTML]{000000} \\ \hline
    Benzaldehyde & Solid & \cellcolor[HTML]{000000} & Slight ppt & \cellcolor[HTML]{000000} \\ \hline
    Unknown \#6 & Solid & & Precipitated & \\ \hline
    & \multicolumn{2}{c|} {\textbf{Chromic Acid Oxidation}} & \multicolumn{2}{c|}{\textbf{Tollen's Test}} \\ \hline
    Acetone & Positive & \cellcolor[HTML]{000000} & Negative & \cellcolor[HTML]{000000} \\ \hline
    1-Propanal & Negative & \cellcolor[HTML]{000000} & Positive & \cellcolor[HTML]{000000} \\ \hline
    Benzaldehyde & Positive & \cellcolor[HTML]{000000} & Positive & \cellcolor[HTML]{000000} \\ \hline
    Glucose & \cellcolor[HTML] {C0C0C0}\textbf{Skip This Test} & \cellcolor[HTML]{000000} & Positive & \cellcolor[HTML]{000000} \\ \hline
    Unknown \#6 & Positive & & Positive & \\ \hline
    & \multicolumn{2}{c|} {\textbf{Iodoform Test}} & \multicolumn{1}{c|}{} & \\ \hline
    Acetone & Precipitated & \multicolumn{1}{c|}{\cellcolor[HTML]{000000}} & \multicolumn{1}{c|}{} & \\ \hline
    Ethanol & No Reaction & \multicolumn{1}{c|}{\cellcolor[HTML]{000000}} & \multicolumn{1}{c|}{} & \\ \hline
    Cyclohexanone & No Reaction & \multicolumn{1}{c|}{\cellcolor[HTML]{000000}} & \multicolumn{1}{c|}{} & \\ \hline
    Unknown \#6 & No Reaction & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \\ \hline
  \end{tabularx}
\end{table}


\begin{table}[h]
  \begin{tabularx}{\linewidth}{>{\bfseries}Sl*{4}{>{\centering\arraybackslash}X}}
    \toprule
    \multicolumn{5}{Sc}{Lab Practicum Organic Chemistry II} \\ \midrule[\heavyrulewidth]
    \multicolumn{1}{Sc}{\textbf{Compound}} & \multicolumn{2}{c} {\textbf{Solubility}} & \multicolumn{2}{c}{\textbf{Density}} \\
    \cmidrule{1-1} \cmidrule(l){2-3}\cmidrule(l){4-5}
    & \textit{\textbf{Observation}} & \textit{\textbf{Conclusion}} & \textit{\textbf{Observation}} & \textit{\textbf{Conclusion}} \\ \midrule\addlinespace
    Acetone & Miscible & \cellcolor[HTML]{000000} & Miscible & \cellcolor[HTML]{000000} \\ %
    1-Butanal & $\sim$8-9 drops & \cellcolor[HTML]{000000} & Floats & \cellcolor[HTML]{000000} \\
    2-Butanone & \textless10 drops & \cellcolor[HTML]{000000} & Floats & \cellcolor[HTML]{000000} \\ %
    Cyclohexanone & 6 drops & \cellcolor[HTML]{000000} & Sinks & \cellcolor[HTML]{000000} \\ %
    Benzaladehyde & \textgreater10 drops & \cellcolor[HTML]{000000} & Sinks & \cellcolor[HTML]{000000} \\ %
    Unknown \#6 & 4-5 drops & Boils at 75 & Floats & \\ \midrule\addlinespace[2ex]
    & \multicolumn{2}{c}{\textbf{2,4- Dinitrophenylhydrazone}} & \multicolumn{2}{c}{\textbf{Semicarbazones}} \\ %
    \cmidrule(l){2-3}\cmidrule(l){4-5}\addlinespace
    Acetone & Solid & \cellcolor[HTML]{000000} & --- & \cellcolor[HTML]{000000} \\ %
    Benzaldehyde & Solid & \cellcolor[HTML]{000000} & Slight ppt & \cellcolor[HTML]{000000} \\ Unknown \#6 & Solid & & Precipitated & \\ \midrule\addlinespace[2ex]%
    & \multicolumn{2}{c} {\textbf{Chromic Acid Oxidation}} & \multicolumn{2}{c}{\textbf{Tollen's Test}} \\%
    \cmidrule(l){2-3}\cmidrule(l){4-5}\addlinespace
    Acetone & Positive & \cellcolor[HTML]{000000} & Negative & \cellcolor[HTML]{000000} \\ %
    1-Propanal & Negative & \cellcolor[HTML]{000000} & Positive & \cellcolor[HTML]{000000} \\ %
    Benzaldehyde & Positive & \cellcolor[HTML]{000000} & Positive & \cellcolor[HTML]{000000} \\ %
    Glucose & \cellcolor[HTML] {C0C0C0}\textbf{Skip This Test} & \cellcolor[HTML]{000000} & Positive & \cellcolor[HTML]{000000} \\ %
    Unknown \#6 & Positive & & Positive & \\ \midrule\addlinespace[2ex]%
    & \multicolumn{2}{c} {\textbf{Iodoform Test}} \\
    \cmidrule(l){2-3}\addlinespace%
    Acetone & Precipitated & \multicolumn{1}{c}{\cellcolor[HTML]{000000}} \\ %
    Ethanol & No Reaction & \multicolumn{1}{c}{\cellcolor[HTML]{000000}} \\%
    Cyclohexanone & No Reaction & \multicolumn{1}{c}{\cellcolor[HTML]{000000}} \\ %
    Unknown \#6 & No Reaction \\ \bottomrule
  \end{tabularx}
\end{table}

\end{document} 

在此处输入图片描述在此处输入图片描述

相关内容