使 tabularx 环境适合文本块

使 tabularx 环境适合文本块

我的tabularx环境在宽度上不适合文本块。s\hline可以,但文本不行。

使用Excel2latext制作表格,只添加了‘x’ tabular

\begin{table}[htbp]
 \centering
 \caption{Add caption}
   \begin{tabularx}{\textwidth}{rrcrrcrrrrrr}
\toprule
\multicolumn{1}{c}{\textbf{Specialty}} & \multicolumn{1}{c}{\textbf{Procedure}} & \textbf{Theatre Minutes} & \multicolumn{1}{c}{\textbf{Major/Minor Grouping}} & \multicolumn{1}{c}{\textbf{Mean (historical data)}} &\textbf{standard deviation} & \multicolumn{1}{c}{\textbf{total number of surguries}} & \multicolumn{1}{c}{\textbf{}} & \multicolumn{1}{c}{\textbf{percentile difference for each operation}} & \multicolumn{1}{c}{\textbf{total  time difference of estimate(during 2 years of data)}} & \multicolumn{1}{c}{\textbf{time difference per day}} & \multicolumn{1}{c}{\textbf{number of operations per day}} \\
\midrule
\textbf{General Surgery} & Abscess Drainage & 30    & \textbf{major} & 11.17 & 9.02  & \textbf{1135} &       & 168.64\% & 21375 & 29.28082192 & 1.555 \\
\textbf{} & Appendicectomy & 60    & \textbf{major} & 51.83 & 21.25 & \textbf{800} &       & 15.77\% & 6540  & 8.95890411 & 1.096 \\
\textbf{} & Biopsy/Excision/Drainage & 30    & \textbf{major} & 36.40 & 28.57 & \textbf{1073} &       & -17.58\% & -6868 & -9.408219178 & 1.470 \\
\textbf{} & Cholecystectomy & 90    & \textbf{major} & 91.42 & 36.80 & \textbf{782} &       & -1.56\% & -1114 & -1.526027397 & 1.071 \\
\textbf{} & Dressing/wound & 30    & \textbf{major} & 30.67 & 24.50 & \textbf{363} &       & -2.18\% & -243  & -0.332876712 & 0.497 \\
\textbf{} & EUA   & 30    & \textbf{major} & 31.71 & 34.00 & \textbf{329} &       & -5.38\% & -561  & -0.768493151 & 0.451 \\
\textbf{} & Hernia Repair & 60    & \textbf{major} & 75.93 & 45.37 & \textbf{521} &       & -20.97\% & -8297 & -11.36575342 & 0.714 \\
\textbf{} & Laparoscopy (excluding appendicectomy) & 60    & \textbf{major} & 59.41 & 35.32 & \textbf{476} &       & 1.00\% & 282   & 0.38630137 & 0.652 \\
\textbf{} & Laparotomy (incl. open chole etc) & 210   & \textbf{major} & 152.22 & 88.32 & \textbf{1533} &       & 37.95\% & 88571 & 121.330137 & 2.100 \\
\textbf{} & Scoping procedure & 90    & \textbf{major} & 103.99 & 84.69 & \textbf{111} &       & -13.45\% & -1553 & -2.12739726 & 0.152 \\
\bottomrule
\end{tabularx}%
\label{tab:addlabel}%
\end{table}%

你能看出我的代码哪里出错了吗?

答案1

正如 Werner 所说,问题在于它tabularx至少需要一个X类型列。但问题还在于它没有任何魔力:你的表格不会缩小,因为tabularx无法缩小内容,而只能拉伸空间使其变大。

下面的操作稍微多一点,以便至少在横向模式下旋转查看时将表格适合 A4 纸张。

RC是新的列类型,基于X使用定义大批包。它们替代rc。标题行必须特殊处理。\centering在此处添加,并\arraybackslash在最后一列中添加以保留使用来\\结束行。

\hskip0pt被添加到每一列,以允许对单元格中的第一个单词进行连字。

实现这个并删除一些脏东西(例如\textbf{}),我们得到:

\documentclass[a4paper]{article}
\usepackage{geometry,tabularx,booktabs,rotating}
\geometry{scale=.8}
\newcolumntype{R}{>{\raggedleft\arraybackslash\hskip0pt}X}
\newcolumntype{C}{>{\centering\arraybackslash\hskip0pt}X}
\begin{document}
  \begin{sidewaystable}[htbp]
    \centering
    \caption{Add caption}
    \begin{tabularx}{\textwidth}{RRCRRC*{5}{R}}
      \toprule
      \centering\textbf{Specialty} & \centering\textbf{Procedure} & \centering\textbf{Theatre Minutes} & \centering\textbf{Major / Minor Grouping} & \centering\textbf{Mean (historical data)} &\centering\textbf{standard deviation} & \centering\textbf{total number of surgeries} & \centering\textbf{percentile difference for each operation} & \centering\textbf{total  time difference of estimate (during 2 years of data)} & \centering\textbf{time difference per day} & \centering\arraybackslash\textbf{number of operations per day} \\
      \midrule
      \centering\textbf{General Surgery} & Abscess Drainage & 30 & \textbf{major} & 11.17 & 9.02  & \textbf{1135} & 168.64\% & 21375 & 29.28082192 & 1.555 \\
       & Appendicectomy & 60    & \textbf{major} & 51.83 & 21.25 & \textbf{800} & 15.77\% & 6540  & 8.95890411 & 1.096 \\
       & Biopsy / Excision / Drainage & 30    & \textbf{major} & 36.40 & 28.57 & \textbf{1073} & -17.58\% & -6868 & -9.408219178 & 1.470 \\
       & Cholecystectomy & 90    & \textbf{major} & 91.42 & 36.80 & \textbf{782} & -1.56\% & -1114 & -1.526027397 & 1.071 \\
       & Dressing/wound & 30    & \textbf{major} & 30.67 & 24.50 & \textbf{363} & -2.18\% & -243  & -0.332876712 & 0.497 \\
       & EUA   & 30    & \textbf{major} & 31.71 & 34.00 & \textbf{329} & -5.38\% & -561  & -0.768493151 & 0.451 \\
       & Hernia Repair & 60    & \textbf{major} & 75.93 & 45.37 & \textbf{521} & -20.97\% & -8297 & -11.36575342 & 0.714 \\
       & Laparoscopy (excluding appendicectomy) & 60    & \textbf{major} & 59.41 & 35.32 & \textbf{476} & 1.00\% & 282   & 0.38630137 & 0.652 \\
       & Laparotomy (incl. open chole etc) & 210   & \textbf{major} & 152.22 & 88.32 & \textbf{1533} & 37.95\% & 88571 & 121.330137 & 2.100 \\
       & Scoping procedure & 90    & \textbf{major} & 103.99 & 84.69 & \textbf{111} & -13.45\% & -1553 & -2.12739726 & 0.152 \\
      \bottomrule
    \end{tabularx}
    \label{tab:addlabel}
  \end{sidewaystable}
\end{document}

生产:

A4 上的表格

这几乎不具吸引力,甚至不是特别容易理解,但它确实适合页面,尽管有几处关于坏盒子的投诉。

除此之外,您还需要考虑如何呈现数据。例如,目前,该major/minor列是无用的,因为显示的所有案例都是主要案例。因此,可以将其合并到标题中,或作为表格下方的注释添加,或者以最合适的方式添加。同样,该speciality列也是无用的。即使这只是较长表格的一部分,这种信息也可以作为表格内子部分的标题呈现,而不是占据大部分由空白空间组成的列。无论如何,您无法在页面上容纳更多内容,因此可以推测这实际上是一个表格,任何其他数据都将放在其他表格中。

下面的内容就比较容易理解了:

提高消化率

但像这样的事情可能会进一步改善:

进一步改进

这是使用三部分表(尽管三部分表可以在此处执行)来创建注释,从而使标题更加简洁。符号%也已移至相关标题中,而percentile删除了,从而进一步减少了此处所需的行数。结果符合预期,没有出现坏框的投诉。

\documentclass[a4paper]{article}
\usepackage{geometry,tabularx,booktabs,rotating,threeparttablex}
\geometry{scale=.8}
\newcolumntype{R}{>{\raggedleft\arraybackslash\hskip0pt}X}
\newcolumntype{L}{>{\raggedright\arraybackslash\hskip0pt}X}
\newcolumntype{C}{>{\centering\arraybackslash\hskip0pt}X}
\begin{document}
  \begin{sidewaystable}[htbp]
    \centering
    \begin{threeparttable}
      \caption{Add caption}
      \begin{tabularx}{\textwidth}{LCRC*{5}{R}}
        \toprule
        \multicolumn{8}{l}{\bfseries Speciality: General Surgery / Grouping: major} &\\\cmidrule{1-9}     \centering\textbf{Procedure} & \centering\textbf{Theatre Minutes} & \centering\textbf{Mean\tnote{a} } &\centering\textbf{standard deviation} & \centering\textbf{total number of surgeries} & \centering\textbf{\% difference per operation} & \centering\textbf{total  time difference of estimate\tnote{b}} & \centering\textbf{time difference per day} & \centering\arraybackslash\textbf{number of operations per day} \\
        \midrule
        Abscess Drainage & 30  & 11.17 & 9.02  & \textbf{1135} & 168.64 & 21375 & 29.28082192 & 1.555 \\
        Appendicectomy & 60     & 51.83 & 21.25 & \textbf{800} & 15.77 & 6540  & 8.95890411 & 1.096 \\
        Biopsy / Excision / Drainage & 30     & 36.40 & 28.57 & \textbf{1073} & -17.58 & -6868 & -9.408219178 & 1.470 \\
        Cholecystectomy & 90     & 91.42 & 36.80 & \textbf{782} & -1.56 & -1114 & -1.526027397 & 1.071 \\
        Dressing/wound & 30     & 30.67 & 24.50 & \textbf{363} & -2.18 & -243  & -0.332876712 & 0.497 \\
        EUA   & 30     & 31.71 & 34.00 & \textbf{329} & -5.38 & -561  & -0.768493151 & 0.451 \\
        Hernia Repair & 60     & 75.93 & 45.37 & \textbf{521} & -20.97 & -8297 & -11.36575342 & 0.714 \\
        Laparoscopy (excluding appendicectomy) & 60     & 59.41 & 35.32 & \textbf{476} & 1.00 & 282   & 0.38630137 & 0.652 \\
        Laparotomy (incl. open chole etc) & 210    & 152.22 & 88.32 & \textbf{1533} & 37.95 & 88571 & 121.330137 & 2.100 \\
        Scoping procedure & 90     & 103.99 & 84.69 & \textbf{111} & -13.45 & -1553 & -2.12739726 & 0.152 \\
        \bottomrule
      \end{tabularx}
      \begin{tablenotes}
        \item[a]historical data
        \item[b]during 2 years of data
      \end{tablenotes}
      \label{tab:addlabel}
    \end{threeparttable}
  \end{sidewaystable}
\end{document}

答案2

好吧,我的解决方案太慢了,不能成为第一个 :-(。无论如何,我将展示我的努力成果(两个小时,大部分时间花在格式化我的 MWE 代码上)。它与 @cfr 解决方案略有不同,但要点是相同的。

为了使表格适合文本宽度,我使用包ltablexthreeparttablex和来描述较长的列标题和程序描述,并在表格注释中使用全名。我还使用包来更好地格式化数字,并重新使用和的siunitx本地使用。通过这种测量和考虑,左右边距宽度为 25 毫米,我得到:\tectbf{...}\%

在此处输入图片描述

\documentclass{article}
    \usepackage{array,booktabs,ltablex,threeparttablex}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcommand{\mcbf}[1]{\multicolumn{1}{c}{\textbf{#1}}} 
    \usepackage{siunitx}

    \usepackage[margin=25mm,showframe]{geometry}

    \begin{document}
\begin{table}[htbp]
    \centering
    \small
    \setlength{\tabcolsep}{3pt}
\begin{ThreePartTable}
\caption{Add caption}
\label{tab:addlabel}%
    \setTableNoteFont{\footnotesize}
\begin{TableNotes}[para,raggedright]\smallskip
    \item[1] Theatre Minutes,
    \item[2] Mean of historical data,
    \item[3] Total number of surgeries,
    \item[4] Percentile difference for each operation,
    \item[5] Total time difference of estimate during 2 years of data,
    \item[6] Time difference per day,
    \item[7] Number of operations per day.\\

    \item[A] Biopsy with Excision and Drainage,
    \item[B] Laparotomy including open chole etc.
\end{TableNotes}
    \begin{tabularx}{\textwidth}{@{}L
                                    S[table-format=2.0]
                                    S[table-format=3.2]
                                    S[table-format=2.2]
                        >{\bfseries}S[detect-weight,
                                      table-format=5.0]
                                    S[table-format=3.2,
                                      table-space-text-post=\quad]<{$\;\%$}
                                    S[table-format=5.0]
                                    S[table-format=2.9]
                                    S@{}}
    \toprule
\multicolumn{9}{@{}l@{}}{\textbf{General Surgery, Grouping: major}} \\
    \midrule
\mcbf{Procedure}
    &   \mcbf{time\textsuperscript{1}}
            &  \mcbf{Mean\textsuperscript{2}}
                &   \mcbf{deviation\textsuperscript{3}}
                    &   \mcbf{Surgery\textsuperscript{4}}
                        &   \mcbf{diff.\textsuperscript{5}}
                            &  \mcbf{total diff.\textsuperscript{6}}
                                &   \mcbf{time diff.\textsuperscript{7}}
                                    &   \mcbf{operations\textsuperscript{8}}\\
    \midrule
Abscess Drainage
    & 30    & 11.17 & 9.02  & 1135
        & 168.64& 21375 & 29.28082192   & 1.555         \\
Appendicectomy
    & 60    & 51.83 & 21.25 & 800
        & 15.77 & 6540  & 8.95890411    & 1.096         \\
Biopsy\textsuperscript{A}
    & 30    & 36.40 & 28.57 & 1073
        & -17.58& -6868& -9.408219178   & 1.470         \\
Cholecystectomy
    & 90    & 91.42 & 36.80 & 782
        & -1.56 & -1114 & -1.526027397  & 1.071         \\
Dressing/wound
    & 30    & 30.67 & 24.50 & 363
        &-2.18  & -243  & -0.332876712  & 0.497         \\
EUA &30     & 31.71 & 34.00   & 329
    & -5.38 & -561  & -0.768493151      & 0.451         \\
Hernia Repair
    & 60  & 75.93 & 45.37 & 521
        & -20.97& -8297 & -11.36575342  & 0.714         \\
Laparoscopy
    & 60  & 59.41 & 35.32 &  476
        & 1.00  & 282   & 0.38630137    & 0.652         \\
Laparotomy\textsuperscript{B}
    & 210& 152.22  & 88.32   & 1533
        & 37.95     & 88571   & 121.330137   & 2.100    \\
Scoping procedure
    & 90 & 103.99  & 84.69   & 111    
        & -13.45& -1553 & -2.12739726   & 0.152         \\
    \bottomrule
    \insertTableNotes 
    \end{tabularx}
\end{ThreePartTable}
\end{table}
    \end{document}   

答案3

以下解决方案使用(修改后的)X列作为标题单元格,并使用S(来自siunitx包的)列作为数字列的内容。为了允许在/符号处换行,我将它们替换为\slash宏。我已删除大多数粗体(标题单元格除外),因为它主要在喊“我知道如何喊叫”。而且,我对倒数第二列的数字应用了自动舍入,因为我认为您的读者不会明白任何事物小数点后显示 8 位或 9 位数字。

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx,booktabs,ragged2e,geometry,rotating,siunitx,caption}
\captionsetup{skip=0.5\baselineskip}
\newcolumntype{L}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcolumntype{C}{>{\Centering\arraybackslash\hspace{0pt}}X}

\newcommand\mc[1]{\multicolumn{1}{C}{\bfseries#1}} % handy shortcut macro

\geometry{a4paper,margin=1in} % set page parameters

\begin{document}

\begin{sidewaystable}
\setlength\tabcolsep{4pt} % default value: 6pt

\caption{Add caption}
\label{tab:addlabel}

\begin{tabularx}{\textwidth}{@{} 
  >{\bfseries}L 
  L 
  S[table-format=3.0] 
  C  
  S[table-format=3.2] 
  S[table-format=3.2] 
  S[table-format=4.0] 
  S[parse-numbers=false,table-format=-3.3]
  S[table-format=-5.0]
  S[table-format=-3.2,round-mode=places,round-precision=2]
  S[table-format=1.3] @{}}

\toprule
Specialty 
& \textbf{Procedure} 
& \mc{Theatre Minutes} 
& \mc{Major\slash Minor Grouping} 
& \mc{Mean (historical data)} 
& \mc{standard deviation} 
& \mc{total number of surguries} 
& \mc{percentile difference for each operation} 
& \mc{total time difference of estimate (2 years of data)} 
& \mc{time difference per day (rounded)} 
& \mc{number of operations per day} \\
\midrule

General Surgery & Abscess Drainage & 30    & major & 11.17 & 9.02  & 1135 & 168.64\% & 21375 & 29.28082192 & 1.555 \\\addlinespace
 & Appendic\-ectomy & 60    & major & 51.83 & 21.25 & 800 & 15.77\% & 6540  & 8.95890411 & 1.096 \\\addlinespace
 & Biopsy\slash Excision\slash Drainage & 30    & major & 36.40 & 28.57 & 1073 & -17.58\% & -6868 & -9.408219178 & 1.470 \\\addlinespace
 & Cholecyst\-ectomy & 90    & major & 91.42 & 36.80 & 782 & -1.56\% & -1114 & -1.526027397 & 1.071 \\\addlinespace
 & Dressing\slash wound & 30    & major & 30.67 & 24.50 & 363 & -2.18\% & -243  & -0.332876712 & 0.497 \\\addlinespace
 & EUA   & 30    & major & 31.71 & 34.00 & 329 & -5.38\% & -561  & -0.768493151 & 0.451 \\\addlinespace
 & Hernia \mbox{Repair} & 60    & major & 75.93 & 45.37 & 521 & -20.97\% & -8297 & -11.36575342 & 0.714 \\\addlinespace
 & Laparo\-scopy (excluding appendic\-ectomy) & 60    & major & 59.41 & 35.32 & 476 & 1.00\% & 282   & 0.38630137 & 0.652 \\\addlinespace
 & Laparotomy (incl.\ open chole etc) & 210   & major & 152.22 & 88.32 & 1533 & 37.95\% & 88571 & 121.330137 & 2.100 \\\addlinespace
 & Scoping procedure & 90  & major & 103.99 & 84.69 & 111 & -13.45\% & -1553 & -2.12739726 & 0.152 \\
\bottomrule
\end{tabularx}
\end{sidewaystable}

\end{document}

附录:上述解决方案没有解决两个问题:首先,第一列(最左侧)是否需要?是否可以将“普通外科”标签放在表格标题中,而不是占用整列?其次,“主要/次要分组”列似乎是多余的,因为该列中的每个单元格都写着“主要”。是否可以将此信息也放在标题中?当然,如果没有这两个(显然是多余的)列,表格看起来会更干净,也更容易阅读。

假设文本块的宽度比较正常,删除两个多余的列并在表格标题中提供删除的信息,就可以排版整个表格肖像模式而不是横向模式。您的读者可能会很高兴不必伸长脖子就能看清表格。

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx,booktabs,ragged2e,geometry,siunitx,caption}
\captionsetup{skip=0.5\baselineskip}
\newcolumntype{C}{>{\Centering\arraybackslash\hspace{0pt}}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\geometry{a4paper,margin=1in} % set page parameters
\newcommand\mc[1]{\multicolumn{1}{C}{#1}} % handy shortcut macro

\begin{document}

\begin{table}
\setlength\tabcolsep{4pt} % default value: 6pt

\caption{General surgery, major groups---\dots}
\label{tab:addlabel}

\begin{tabularx}{\textwidth}{@{} 
  P{2.9cm} 
  S[table-format=3.0] 
  S[table-format=3.2] 
  S[table-format=3.2] 
  S[table-format=4.0] 
  S[parse-numbers=false,table-format=-2.3]
  S[table-format=-5.0,group-minimum-digits=4]
  S[table-format=-3.2,round-mode=places,round-precision=2]
  S[table-format=1.2,round-mode=places,round-precision=2] 
  @{}}
\toprule
  Procedure 
& \mc{Theatre Minutes}  
& \mc{Mean (historical data)} 
& \mc{Std.\ dev.} 
& \mc{Total number of surgeries} 
& \mc{Percentile difference for each operation} 
& \mc{Total time difference of estimate (2 years of data)} 
& \mc{Time difference per day} 
& \mc{Number of operations per day} \\
\midrule

Abscess Drainage & 30&11.17 & 9.02  & 1135 & 168.64\% & 21375 & 29.28082192 & 1.555 \\\addlinespace
Appendic\-ectomy & 60&51.83 & 21.25 & 800 & 15.77\% & 6540  & 8.95890411 & 1.096 \\\addlinespace
Biopsy\slash Excision\slash Drainage & 30&36.40 & 28.57 & 1073 & -17.58\% & -6868 & -9.408219178 & 1.470 \\\addlinespace
Cholecyst\-ectomy & 90&91.42 & 36.80 & 782 & -1.56\% & -1114 & -1.526027397 & 1.071 \\\addlinespace
Dressing\slash wound & 30&30.67 & 24.50 & 363 & -2.18\% & -243  & -0.332876712 & 0.497 \\\addlinespace
EUA   & 30&31.71 & 34.00 & 329 & -5.38\% & -561  & -0.768493151 & 0.451 \\\addlinespace
Hernia \mbox{Repair} & 60&75.93 & 45.37 & 521 & -20.97\% & -8297 & -11.36575342 & 0.714 \\\addlinespace
Laparo\-scopy (excl.\ appendic\-ectomy) & 60&59.41 & 35.32 & 476 & 1.00\% & 282   & 0.38630137 & 0.652 \\\addlinespace
Laparotomy (incl.\ open chole etc) & 210 & 152.22 & 88.32 & 1533 & 37.95\% & 88571 & 121.330137 & 2.100 \\\addlinespace
Scoping procedure & 90 & 103.99 & 84.69 & 111 & -13.45\% & -1553 & -2.12739726 & 0.152 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

相关内容