使用 tabularx 將表格內容左/右對齊

使用 tabularx 將表格內容左/右對齊

我嘗試了多種方法,但仍然無法獲得正確的對齊方式。我希望第一列位於左側,然後其他列位於右側。

\documentclass{article}
\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}
\renewcommand{\TPTtagStyle}{\itshape} % optional
\usepackage{tabularx, ragged2e} 
\usepackage{threeparttable} 
\begin{document}

\begin{table}[!ht]
\centering

 \begin{tablenotes}
\centering
\small
\item Dependent Variable: Testing1234 
\end{tablenotes}
\begin{threeparttable} 
%\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hsize=2\hsize\linewidth=\hsize}X}
%\newcolumntype{W}{>{\RaggedLeft\arraybackslash\hsize=0.75\hsize\linewidth=\hsize}X}
%\begin{tabularx}{\textwidth}{@{}Y*{3}{W}@{}}
\begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}Xlllll}
    \toprule
     \small {\textbf{Source}}
     & \small {\textbf{Type III Sum of Squares}} 
     & \small {\textbf{df}}
     & \small {\textbf{Mean Square}}
     & \small {\textbf{F}}
     & \small {\textbf{Sig.}}\\
     \midrule
    Corrected Model & 14573.300\tnote{a} & 3 & 4857.767 & 40.297 & .000 \\
    Testing1234 & 214036.900 & 1 & 214036.900 & 1775.503 & .000\\
    Testing1234 & .100 & 1 & .100 & .001 & .977\\
    Testing1234Testing1234 & 14137.600 & 1 & 14137.600 & 117.276 & .000\\
    Testing1234Testing1234Testing1234 & 435.600 & 1 & 435.600 & 3.613 & .065 \\
    \bottomrule
    \end{tabularx}
\footnotesize 
\begin{tablenotes}
\item[a]Testing1234Testing1234Testing1234 % the footnote itself 
\end{tablenotes}

\end{threeparttable}
\caption{Testing1234
    \label{tab:test123} }
\end{table}

\end{document}

在此輸入影像描述

我已經累了:

\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hsize=2\hsize\linewidth=\hsize}X}
\newcolumntype{W}{>{\RaggedLeft\arraybackslash\hsize=0.75\hsize\linewidth=\hsize}X}
\begin{tabularx}{\textwidth}{@{}Y*{3}{W}@{}}

不工作

\begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}Xlllll}

也不工作

\begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}Xrrrrr}

越來越迷茫了。

答案1

有了\begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xrrrrr} 你就可以得到想要的結果。我還使用該\thead命令替換重複的\smalland\textbf命令,並在第二列和第四列的列標題中插入手動換行符:

在此輸入影像描述

\documentclass{article}
\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}
\renewcommand{\TPTtagStyle}{\itshape} % optional
\usepackage{tabularx, ragged2e} 

\usepackage{makecell}
\renewcommand\theadfont{\small\bfseries} 
\begin{document}

\begin{table}[!ht]
\centering
\small
Dependent Variable: Testing1234 
\begin{threeparttable} 
\begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xrrrrr}
    \toprule
     \thead{Source}
     & \thead{Type III\\ Sum of\\ Squares} 
     & \thead{df}
     & \thead{Mean\\ Square}
     & \thead{F}
     & \thead{Sig.}\\
     \midrule
    Corrected Model & 14573.300\tnote{a} & 3 & 4857.767 & 40.297 & .000 \\
    Testing1234 & 214036.900 & 1 & 214036.900 & 1775.503 & .000\\
    Testing1234 & .100 & 1 & .100 & .001 & .977\\
    Testing 1234Tes ting1234 & 14137.600 & 1 & 14137.600 & 117.276 & .000\\
    Testing 1234Test ing12 34Te ting1234 & 435.600 & 1 & 435.600 & 3.613 & .065 \\
    \bottomrule
    \end{tabularx}
\footnotesize 
\begin{tablenotes}
\item[a]Testing1234Testing1234Testing1234 % the footnote itself 
\end{tablenotes}
\end{threeparttable}
\caption{Testing1234\label{tab:test123}}
\end{table}

\end{document}

答案2

您可以使用alsotabulary代替,tabularx因為此環境有L、C、R 和J 欄位。

但為什麼不是數字的 R 列,而是 S 列呢siunitx

在此輸入影像描述

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabulary,siunitx,lipsum} 
\begin{document}
\lipsum[1][1-4]
\begin{table}[h]
\begin{tabulary}{\textwidth}{@{}LS[table-format=6.3]
cS[table-format=6.3]S[table-format=4.3]c@{}}\toprule
          & {Type III Sum} &       & {Mean}   &        &          \\
Source    & {of Squares}   & {df}  & {Square} & {F}    & {Sig.}   \\\midrule
Corrected Model & 14573.300$^a$ & 3 & 4857.767 & 40.297 & .000 \\
Testing1234 & 214036.900 & 1 & 214036.900 & 1775.503 & .000\\
Testing1234 & .100 & 1 & .100 & .001 & .977\\
Testing1234Testing1234 & 14137.600 & 1 & 14137.600 & 117.276 & .000\\
Testing1234Testing1234\-Testing1234 & 435.600 & 1 & 435.600 & 3.613 & .065 \\
    \bottomrule
\end{tabulary}
$^a$ \footnotesize Testing1234Testing1234Testing1234
\end{table}
\lipsum[2][1-4]
\end{document}

tabularx是固定寬度的表格,至少有一個等寬的X列(但是a,ta採取使不等列與任何justito適應右對齊文本(R列)Lef寬度適應單元格比賽嘗試使列的寬度不相等,嘗試採用最佳寬度

相關內容