在 tabularx 中使用段落列换行文本时,多列居中不起作用

在 tabularx 中使用段落列换行文本时,多列居中不起作用

我在表格末尾使用多列来表示某些缩写的含义。我有足够多的缩写,需要换行,所以我使用定义了居中的段落列。但是,我注意到当我不使用完整的文本宽度时,多列的内容不会像我预期的那样居中。有什么办法可以解决这个问题吗?MWE 在下面。

\documentclass{article}

\usepackage{tabularx,array,multirow}
\usepackage{booktabs}
\usepackage{caption}

\newcolumntype{P}[1]{>{\footnotesize\centering\arraybackslash}m{#1}}

\begin{document}

\noindent\begin{table}[!htbp]
\captionsetup{justification=centering}
\caption{Characteristics of Patients\label{tab:ptChar}}
\centering
\begin{tabularx}{\textwidth}{P{0.15\textwidth}
                             >{\footnotesize\centering\arraybackslash}X 
                             >{\footnotesize\centering\arraybackslash}X
                             >{\footnotesize\centering\arraybackslash}X
                             >{\footnotesize\centering\arraybackslash}X}
\toprule[1.5pt]
Characteristic & Patients (n=132) & $>$30\% (n=11) & $<$30\% (n=92) & p \\
\midrule

\raggedright Mean Age at Pathologic Diagnosis & 54.8 & 51.6 & 55.1 & 0.47* \\

\raggedright Ethnicity &  &  &  & 1.00\textsuperscript{\#} \\

\raggedright \hangindent=3mm \hangafter=0 Caucasian        & 76 &
 \shortstack{6 \\ (55\%)} & \shortstack{52 \\ (57\%)} & \\

\raggedright \hangindent=3mm \hangafter=0 African American & 45 &
 \shortstack{4 \\ (36\%)} & \shortstack{30 \\ (33\%)} & \\

\raggedright \hangindent=3mm \hangafter=0 NA               & 11 &
 \shortstack{1 \\ (9\%)}  & \shortstack{10 \\ (11\%)} & \\

\bottomrule[1.5pt]
\multicolumn{5}{P{0.8\textwidth}}{sfkdjkjke: lkjdfkljkljflasf; ioawejroias: kjskldfj.asdjf.; pval: p-value; *: Wilcoxon test; \textsuperscript{\#}: Fisher's Exact Test; NA: missing data }\\
\end{tabularx}
\end{table}

\end{document}

在此处输入图片描述

答案1

您可以大大简化您的输入:

\documentclass{article}

\usepackage{array}
\usepackage{booktabs}
\usepackage{caption}

\newcommand{\splitcell}[2][c]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}

\begin{document}

\begin{table}[!htbp]
\centering\footnotesize

\caption{Characteristics of Patients\label{tab:ptChar}}

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lcccc@{}}
\toprule
Characteristic & Patients ($n=132$) & $>30\%$ ($n=11$) & $<30\%$ ($n=92$) & p \\
\midrule

\splitcell[l]{Mean Age at \\ Pathologic \\ Diagnosis} & 54.8 & 51.6 & 55.1 & 0.47* \\

\addlinespace

Ethnicity &  &  &  & 1.00\textsuperscript{\#} \\

\quad Caucasian        & 76 &
 \splitcell{6 \\ (55\%)} & \splitcell{52 \\ (57\%)} & \\

\quad \splitcell[l]{African \\ American} & 45 &
 \splitcell{4 \\ (36\%)} & \splitcell{30 \\ (33\%)} & \\

\quad NA               & 11 &
 \splitcell{1 \\ (9\%)}  & \splitcell{10 \\ (11\%)} & \\

\midrule[\heavyrulewidth]
\multicolumn{5}{@{}c@{}}{%
  \parbox{0.8\textwidth}{\centering sfkdjkjke: lkjdfkljkljflasf; 
  ioawejroias: kjskldfj.asdjf.; pval:~p-value; *:~Wilcoxon test; 
  \textsuperscript{\#}:~Fisher's Exact Test; NA:~missing data}}\\
\end{tabular*}

\end{table}

\end{document}

不要过度使用tabularx。此外,如果合适,请尝试使用\small\footnotesize来获得更易读的输出。

在此处输入图片描述

答案2

使用makecellthreeparttablesiunitx包:

\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{booktabs, 
            makecell,                   % new
            tabularx}
\usepackage[referable]{threeparttablex} % new
\usepackage{siunitx}                    % new

\begin{document}
    \begin{table}[!htb]  
\begin{threeparttable}
    \caption{Characteristics of Patients}
    \label{tab:ptChar}
\small
    \sisetup{input-comparators=<>,
             input-symbols = {( )},
             table-space-text-pre={(},
             table-space-text-post={\#},
             table-align-text-post=false}
    \setTableNoteFont{\footnotesize}
\begin{tabularx}{0.9\textwidth}{@{}>{\raggedright}X 
                        *{3}{S[table-format=2.1]}
                             S[table-format=1.2]
                             @{}}
    \toprule
Characteristic  &   {\makecell[t]{Patients\\ (n=132)}} 
                    &   {\makecell[t]{\SI{> 30}{\%}\\ (n=11)}} 
                        &   {\makecell[t]{\SI{< 30}{\%}\\ (n=92)}} 
                            &   {p value}\\
    \midrule
Mean Age at Pathologic Diagnosis 
    & 54.8  & 51.6      & 55.1      & 0.47\tnote{*}     \\
    \addlinespace 
Ethnicity 
    &       &           &           & 1.00\tnote{\#}    \\
\quad   Caucasian        
    & 76    & 6         & 52        &                   \\
    &       & (55\,\%)  & (57\,\%)  &                   \\
\quad   African American 
    & 45    & 4         & 30        &                   \\
    &       & (36\,\%)  & (33\,\%)  &                   \\
\quad   NA  
    & 11    & 1         & 10        &                   \\
    &       & (9\,\%)   & (91\,\%)  &                   \\
\bottomrule
\end{tabularx}
\begin{tablenotes}[para]\centering
\note lkjdfkljkljflasf; ioawejroias: kjskldfj.asdjf.; NA:   missing data\\
       p-values:
       \item[*]:    Wilcoxon test,
       \item[\#]:   Fisher's Exact Test,    
\end{tablenotes}
\end{threeparttable}
    \end{table}
\end{document}     

在此处输入图片描述

答案3

没有必要用 来启动表格环境\noindent。我还建议您删除表格左右两侧的侧边栏。

您可以在最后一个多列中使用嵌套表格。多列应具有表格的整个宽度并居中。我使用固定宽度的wc列。嵌套表格有一列0.8\linewidth

在此处输入图片描述

\documentclass{article}

\usepackage{tabularx,array,multirow}
\usepackage{booktabs}
\usepackage{caption}

\newcolumntype{P}[1]{>{\footnotesize\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}[!htbp]  %% <-- Remove \noindent
\captionsetup{justification=centering}
\caption{Characteristics of Patients\label{tab:ptChar}}
\centering
\begin{tabularx}{\textwidth}{@{}P{0.15\textwidth}
                             >{\footnotesize\centering\arraybackslash}X 
                             >{\footnotesize\centering\arraybackslash}X
                             >{\footnotesize\centering\arraybackslash}X
                             >{\footnotesize\centering\arraybackslash}X@{}}
\toprule[1.5pt]
Characteristic & Patients (n=132) & $>$30\% (n=11) & $<$30\% (n=92) & p \\
\midrule

\raggedright Mean Age at Pathologic Diagnosis & 54.8 & 51.6 & 55.1 & 0.47* \\

\raggedright Ethnicity &  &  &  & 1.00\textsuperscript{\#} \\

\raggedright \hangindent=3mm \hangafter=0 Caucasian        & 76 &
 \shortstack{6 \\ (55\%)} & \shortstack{52 \\ (57\%)} & \\

\raggedright \hangindent=3mm \hangafter=0 African American & 45 &
 \shortstack{4 \\ (36\%)} & \shortstack{30 \\ (33\%)} & \\

\raggedright \hangindent=3mm \hangafter=0 NA               & 11 &
 \shortstack{1 \\ (9\%)}  & \shortstack{10 \\ (11\%)} & \\

\bottomrule[1.5pt]
\multicolumn{5}{wc{\linewidth}}{\begin{tabular}{@{}P{0.8\linewidth}@{}}
sfkdjkjke: lkjdfkljkljflasf; ioawejroias: kjskldfj.asdjf.; pval: p-value; *:~Wilcoxon test; \textsuperscript{\#}:~Fisher's Exact Test; NA:~missing data\\
\end{tabular}}
\end{tabularx}
\end{table}

\end{document}

相关内容