对齐表格中的值

对齐表格中的值

我已经按自己想要的方式对齐了表格中的值。但是,出于某种原因,这完全弄乱了我的第一行。我只想让标签位于值上方的中央。我该如何解决这个问题?

提前谢谢您!

\begin{table}[H]
       \centering
       \renewcommand{\arraystretch}{1.7}
       %\begin{tabular}[width=\textwidth]{l l l l l l}
       \begin{tabularx}{\textwidth}{XSSSSS}
       \hline
       \hline
        \multicolumn{6}{c}{Price} \\
       \hline
          & Cheese & Butter & Milk & Yoghurt & Cream  \\
          \hline
          \hline
          Cheese &  -0.847*** &  0.002 &  -0.068*** & -0.066*** &  -0.031** \\
          
          Butter & 0.017 & -0.996*** &  0.007    &    0.008   &    -0.028\\
          
          Milk  &   -0.224***  &   0.002   &    -0.846***  &   0.029    &    0.016 \\
          
          Yoghurt  &   -0.208*** &    0.018   &     0.052   &    -0.748***  &  -0.033  \\
          
          Cream  &  -0.289*** &   -0.072   &     0.039   &    -0.091***  &  -0.631***  \\
          \hline
          \hline
           \multicolumn{6}{c}{\footnotesize  * p$<$0.1, ** p$<$0.05, *** p$<$0.01}
       \end{tabularx}
       \caption{Second stage estimation of own- and cross-price elasticities}
       \label{cross_price_2d}
\end{table}

在此处输入图片描述

答案1

首先,您需要将标题单词“cheese”、“butter”等括在花括号中,以告知包的机制siunitx它们是单词,而不是数字。(特别是字母“e”,容易混淆S列类型。)

其次,为了节省水平空间,您应该从朴素的S列类型(将整个列内容集中在小数点上)切换到诸如 之类的类型S[table-format=-1.6]。我还会发出指令\sisetup{table-align-text-post = false},以避免数字和尾随星号之间出现任何间隙。

第三,我将\hline用 booktabs 包的线条绘制宏替换指令:、、、\toprule和。请参阅下面的代码以了解此想法的实现。我也不会\cmidrule执行。\midrule\bottomrule\addlinespace\renewcommand{\arraystretch}{1.7}

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage{siunitx,tabularx,booktabs}
\begin{document}

\begin{table}[ht]
\sisetup{table-align-text-post = false}
\begin{tabularx}{\textwidth}{@{} X *{5}{S[table-format=-1.6]} @{}}
    \toprule
    & \multicolumn{5}{c}{Price} \\
    \cmidrule(l){2-6}
    & {Cheese} & {Butter} & {Milk} & {Yoghurt} & {Cream}  \\
    \toprule
    Cheese & -0.847*** &  0.002    & -0.068*** & -0.066*** & -0.031** \\
    
    Butter &  0.017    & -0.996*** &  0.007    &  0.008    & -0.028 \\
    
    Milk   & -0.224*** &  0.002    & -0.846*** &  0.029    &  0.016 \\
    
    Yoghurt& -0.208*** &  0.018    &  0.052    & -0.748*** & -0.033  \\
    
    Cream  & -0.289*** & -0.072    &  0.039    & -0.091*** & -0.631***  \\
    \bottomrule\addlinespace
    \multicolumn{6}{c}{\footnotesize $*\ p<0.1$, ${**}\ p<0.05$, ${*{**}}\ p<0.01$}
\end{tabularx}
\caption{Second stage estimation of own- and cross-price elasticities}
\label{cross_price_2d}
\end{table}

\end{document}

答案2

这是你想要的吗?我必须加载geometry以防止表格溢出到边距。

    \documentclass{article}
    \usepackage{geometry}
    \usepackage{float}
    \usepackage{siunitx}
    \usepackage{tabularx}

    \begin{document}

    \begin{table}[H]
           \centering
           \renewcommand{\arraystretch}{1.7}
           \begin{tabularx}{\textwidth}{XSSSSS}
           \hline
           \hline
            &\multicolumn{5}{c}{Price} \\
           \hline
              & {Cheese} & {Butter} & {Milk} & {Yoghurt} & {Cream} \\
              \hline
              \hline
              Cheese & -0.847*** & 0.002 & -0.068*** & -0.066*** & -0.031** \\

              Butter & 0.017 & -0.996*** & 0.007 & 0.008 & -0.028\\

              Milk & -0.224*** & 0.002 & -0.846*** & 0.029 & 0.016 \\

              Yoghurt & -0.208*** & 0.018 & 0.052 & -0.748*** & -0.033 \\

              Cream & -0.289*** & -0.072 & 0.039 & -0.091*** & -0.631*** \\
              \hline
              \hline
               \multicolumn{6}{c}{\footnotesize * p$<$0.1, ** p$<$0.05, *** p$<$0.01}
           \end{tabularx}
           \caption{Second stage estimation of own- and cross-price elasticities}
           \label{cross_price_2d}
    \end{table}

    \end{document} 

在此处输入图片描述

答案3

tabularray包装,在标准article文档中:

\documentclass{article}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}[ht]
\begin{talltblr}[
caption = {Second stage estimation of own- and cross-price elasticities},
  label = {cross_price_2d},
 note{} = {*:    $p<0.10$,\quad
           **:   $p<0.05$,\quad
           ***:  $p<0.01$.}
                ]{colsep = 3pt,
                 colspec = {@{} l *{5}{X[c,si={table-format=-1.3{**}}]} @{}},
                 }
    \toprule
        &   \SetCell[c=5]{c}   {{{Price}}}       
                    &           &           &           &                       \\
    \cmidrule{2-6}
        & {{{Cheese}}} 
                    & {{{Butter}}}
                                & {{{Milk}}}
                                            & {{{Yoghurt}}} 
                                                        & {{{Cream}}}           \\
    \midrule
Cheese  & -0.847\TblrNote{***}
                    &  0.002    & -0.068\TblrNote{***} 
                                            & -0.066\TblrNote{***} 
                                                        & -0.031\TblrNote{**}   \\
Butter  &  0.017    & -0.996\TblrNote{***}
                                &  0.007    &  0.008    & -0.028                \\
Milk    & -0.224\TblrNote{***}
                    &  0.002    & -0.846\TblrNote{***}
                                            &  0.029    &  0.016                \\
Yoghurt & -0.208\TblrNote{***}
                    &  0.018    &  0.052    & -0.748\TblrNote{***}
                                                        & -0.033                \\
Cream   & -0.289\TblrNote{***}
                    & -0.072    &  0.039    & -0.091\TblrNote{***}
                                                        & -0.631\TblrNote{***}  \\
    \bottomrule
\end{talltblr}
    \end{table}
\end{document}

在此处输入图片描述

(红线表示页面布局)

答案4

您想要指定table-format并使用tabular*,而不是tabularx

我还建议使用booktabs功能,不要将设置\arraystretch为 1.7 这样的大值。另外,避免[H]让表格浮动,除非您想破坏文档的排版方面。

\documentclass{article}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table}[htp]
\centering

\setlength{\tabcolsep}{0pt}% leave the computations to TeX

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  *{5}{S[table-format=-1.3***]}
  @{}
}
\toprule
&\multicolumn{5}{c}{Price} \\
\cmidrule{2-6}
& {Cheese} & {Butter} & {Milk} & {Yoghurt} & {Cream}  \\
\midrule
Cheese &  -0.847*** &  0.002 &  -0.068*** & -0.066*** &  -0.031** \\
Butter & 0.017 & -0.996*** &  0.007    &    0.008   &    -0.028\\
Milk  &   -0.224***  &   0.002   &    -0.846***  &   0.029    &    0.016 \\
Yoghurt  &   -0.208*** &    0.018   &     0.052   &    -0.748***  &  -0.033  \\
Cream  &  -0.289*** &   -0.072   &     0.039   &    -0.091***  &  -0.631***  \\
\midrule[\heavyrulewidth]
\multicolumn{6}{l}{\footnotesize  * p$<$0.1, ** p$<$0.05, *** p$<$0.01}
\end{tabular*}

\caption{Second stage estimation of own- and cross-price elasticities}
\label{cross_price_2d}

\end{table}

\end{document}

在此处输入图片描述

相关内容