# How do I centre a tabularx table?

I have a pretty big data table for an organic chemistry lab and trying to make it look nice as possible. However, not all headers are centred.

% 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}
\pagenumbering{roman}

\begin{document}
\titleGP % This command includes the title page
\clearpage
\setcounter{page}{1}

\pagestyle{fancy}
\fancyhf{}
\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}


And I like all fields to be centred or maybe formatted better than I have it now if you think that is the right thing to do.

• I'd recommend following the booktabs guidelines, using print-quality horizontal rules and eliminating the vertical rules. To centre a single cell, you can use \centering. To centre an X column, you can use >{\centering\arraybackslash}X in the table specification in place of X. Or you can define a new column type for this if you expect to use it repeatedly. – cfr Apr 22 '16 at 22:43
• Personally, I would not centre some of the stuff you are currently centring, rather than the other way round. And the heavy black rectangles are very distracting visually. They catch the eye rather than cells with actual content, which is probably not what you want. – cfr Apr 22 '16 at 22:44
• The reason for tabularx is I like the full width table size but as originally the table was blank for handwritten data prior to the experiment, I suppose now it isn't an entirely an issue. I agree vertical rules are ugly and just used Excel as a "guide" to make it. – Jonathan Weinraub Apr 22 '16 at 22:45
• My suggestions are entirely compatible with tabularx. I did not suggest dropping it. – cfr Apr 22 '16 at 22:46
• @cfr I would like to use a lightweight slashes but can never figure out how to do that in Tex. – Jonathan Weinraub Apr 22 '16 at 22:46

Perhaps this will at least provide some ideas. I'm not sure what the final contents of the table will look like but I assume some empty cells will end up filled which will change things. At least, that's what I assume here.

This is an abuse of both makecells and booktabs. \midrule is not meant to be used like this. Neither is \diaghead.

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{array}
\usepackage{makecell}
\usepackage{booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table}
\caption{Lab Practicum Organic Chemistry II}\label{tab:pracII}
\begin{tabularx}{\linewidth}{>{\bfseries\arraybackslash}l*{4}{C}}
\toprule
\midrule
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
\midrule
Benzaldehyde & Solid &  & Slight ppt & \\
Unknown \#6 & Solid & & Precipitated & \\
\midrule
\midrule
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}


This produces

However, I would probably do something simpler such as

\begin{table}
\caption{Lab Practicum Organic Chemistry II}\label{tab:pracII}
\begin{tabularx}{\linewidth}{>{\bfseries\arraybackslash}l*{4}{C}}
\toprule
\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
\midrule
Acetone & Solid & --- & --- & --- \\
Benzaldehyde & Solid & --- & Slight ppt &--- \\
Unknown \#6 & Solid &--- & Precipitated & \\
\midrule
\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}

• Wow! I love that much better actually I see what you mean, it is much nicer without the vertical lines. And sparingly horizontal lines. Thank YOU so SO MUCH! – Jonathan Weinraub Apr 23 '16 at 1:33
• Glad you like it! – cfr Apr 23 '16 at 1:43

I propose two layouts with tabularx, with vertical rules and without. I loaded booktabs, andcellspace to give some vertical padding to the rows:

\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}} \\ %
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}} \\%
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}} \\
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}


• (+1) but shouldn't you have a line under Iodoform Test in the second example for consistency? – cfr Apr 23 '16 at 1:41
• @cfr: You're right I changed that, and took the opportunity to change cropping of some \cmidrule`s. – Bernard Apr 23 '16 at 8:54
• Normally, no, but I forgot to check it was effective. I did check now! – Bernard Apr 23 '16 at 12:57
• I did notice that so I fixed it on my copy. But added the diagonal on the bottom right quadrant – Jonathan Weinraub Apr 23 '16 at 22:48