tcolorbox 中的 tabularx 没有正确居中

tcolorbox 中的 tabularx 没有正确居中

我试图将 tabularx 放在 tcolorbox 中,但它没有正确居中,而是稍微向右移动了一点。这是我使用的代码。我使用figure环境作为标题。有没有更好的方法可以做到这一点?

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

在此处输入图片描述

答案1

由于单元格内的换行既不期望也不可取,并且四列的自然宽度差别很大,因此使用tabularx环境似乎是错误的。我建议您改用环境tabular*

在此处输入图片描述

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

答案2

但为什么会有这种表格呢?

姆韦

\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[][Propadiene]{\chemfig{C(-[:135,.6]H)(-[:225,.6]H) =[,.6]C=[,.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} %
\chemname{\chemfig{C(-[:135,.6]H)(-[:225,.6]H) =[,.6]C=[,.6]C(-[:45,.6]H)(-[:315,.6]H)}}{(c) Propadiene} %
\chemname{\chemfig{C(-[:180,.6]H) ~[,.6]C -[,.6]C(-[:90,.6]H)(-[:0,.6]H)(-[:270,.6]H)}}{(d) Propyne}\linebreak
\end{tcolorbox} 
\caption{caption}
\end{figure}

\end{document}

相关内容