tabularx inside tcolorbox not centered properly

I am trying to put a tabularx inside a tcolorbox but it's not centered properly, it's shifted slightly to the right. Here is the code I am using. I am using figure environment for the caption. Is there a better way to do this?

\documentclass[12pt, a4paper]{article}
\usepackage[top=1in, bottom=1.5in, right=1in, left=1in]{geometry}
\usepackage{tabularx}
\usepackage{tcolorbox}
\usepackage{chemfig}

\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{document}

\begin{figure}[ht]
\centering
\begin{tcolorbox}
\begin{tabularx}{1\linewidth}{YYYY}
{}\chemfig{C(-[:90,.6]H)(-[:180,.6]H)(-[:270,.6]H) -[,.6]C(-[:90,.6]H)(-[:0,.6]H)(-[:270,.6]H)} &
{}\chemfig{C(-[:135,.6]H)(-[:225,.6]H) =[,.6]C(-[:45,.6]H)(-[:315,.6]H)}                        &
{}\chemfig{C(-[:135,.6]H)(-[:225,.6]H) =[,.6]C=[,.6]C(-[:45,.6]H)(-[:315,.6]H)}                 &
{}\chemfig{C(-[:180,.6]H) ~[,.6]C -[,.6]C(-[:90,.6]H)(-[:0,.6]H)(-[:270,.6]H)}                  \\[5ex]

(a) Ethane & (b) Ethylene & (c) Propadiene & (d) Propyne\\
\end{tabularx}

\end{tcolorbox}
\vspace{-1.5ex}
\caption{caption}
\label{fig:my_label}
\end{figure}

\end{document}


• it is centred but as yiou have forced all the columns to be the same width, entries with smaller structures have more space around them. (if you used |Y|Y|Y|Y| so it added rules it would be clearer that the table itself is centred but there is white space inside the first column – David Carlisle Jan 14 '19 at 8:03

Since line breaking inside the cells is neither expected nor desirable, and since the natural widths of the four columns are quite different, using a tabularx environment seems wrong. I suggest you use a tabular* environment instead.

\documentclass[12pt, a4paper]{article}
\usepackage[top=1in, bottom=1.5in, hmargin=1in]{geometry}
\usepackage{tcolorbox,chemfig}

\begin{document}

\begin{figure}[ht]
\setlength\tabcolsep{0pt} % let LaTeX figure out intercolumn spacing
\begin{tcolorbox}
\begin{tabular*}{1\linewidth}{@{\extracolsep{\fill}} cccc }
\chemfig{C(-[:90,.6]H)(-[:180,.6]H)(-[:270,.6]H) -[,.6]C(-[:90,.6]H)(-[:0,.6]H)(-[:270,.6]H)} &
\chemfig{C(-[:135,.6]H)(-[:225,.6]H) =[,.6]C(-[:45,.6]H)(-[:315,.6]H)}                        &
\chemfig{C(-[:135,.6]H)(-[:225,.6]H) =[,.6]C=[,.6]C(-[:45,.6]H)(-[:315,.6]H)}                 &
\chemfig{C(-[:180,.6]H) ~[,.6]C -[,.6]C(-[:90,.6]H)(-[:0,.6]H)(-[:270,.6]H)}  \\[5ex]
(a) Ethane & (b) Ethylene & (c) Propadiene & (d) Propyne\\
\end{tabular*}
\end{tcolorbox}
\vspace{-2ex}
\caption{caption}
\label{fig:my_label}
\end{figure}

\end{document}

• Thank you so much! It was bugging me for a long time. – Tanton Jan 14 '19 at 8:11
• @Tanton - One of the main differences between tabularx and tabular* is that whereas the former works by adjusting the widths of the columns, the latter works by adjusting the amount of intercolumn whitespace. Since there is no reason for why the 4 columns should occupy equal widths, the tabular* environments seems to be the more natural device to employ. – Mico Jan 14 '19 at 8:15

But why the hell any kind of tabular?

\documentclass[12pt, a4paper]{article}
\usepackage[top=1in, bottom=1.5in, right=1in, left=1in]{geometry}
\usepackage{tcolorbox}
\usepackage{chemfig}
\usepackage{subfig}
\begin{document}

\begin{figure}[ht]
\begin{tcolorbox}[left=1.5em,right=1.5em]
\subfloat[][Ethane]{\chemfig{C(-[:90,.6]H)(-[:180,.6]H)(-[:270,.6]H) -[,.6]C(-[:90,.6]H)(-[:0,.6]H)(-[:270,.6]H)}}\hfill
\subfloat[][Ethylene]{\chemfig{C(-[:135,.6]H)(-[:225,.6]H) =[,.6]C(-[:45,.6]H)(-[:315,.6]H)}}\hfill
\subfloat[][Propyne]{\chemfig{C(-[:180,.6]H) ~[,.6]C -[,.6]C(-[:90,.6]H)(-[:0,.6]H)(-[:270,.6]H)}}
\end{tcolorbox}
\caption{caption}
\end{figure}

Or ...

\begin{figure}[ht]
\begin{tcolorbox}[top=1em,bottom=1em,left=1.5em,right=1.5em]
\chemname{\chemfig{C(-[:90,.6]H)(-[:180,.6]H)(-[:270,.6]H) -[,.6]C(-[:90,.6]H)(-[:0,.6]H)(-[:270,.6]H)}}{(a) Ethane} %
\chemname{\chemfig{C(-[:135,.6]H)(-[:225,.6]H) =[,.6]C(-[:45,.6]H)(-[:315,.6]H)}}{(b) Ethylene} %