2

My tabularx environment doesn't fit, width-wise, inside the text block. The \hlines do, but the text does not.

Excel2latext is used to make the table. Only the 'x' is added to 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}%

Can you see where my code goes wrong?

3
  • 2
    tabularx only first its contents to the width if you use at least one X-column. You don't have any.
    – Werner
    Nov 26, 2015 at 0:39
  • 2
    ...note that tabularx will not shrink the content to fit within the given width. It'll only stretch out X-columns to make the entire tabularx width fit. If your table content is too wide, you have to find your own way of shrinking it. One way would be to stack the header content. See How to add a forced line break inside a table cell to manage non-paragraph-like columns (like c, l, and r).
    – Werner
    Nov 26, 2015 at 0:54
  • Welcome to TeX.SE!
    – Mico
    Nov 26, 2015 at 12:16

3 Answers 3

4

As Werner said, the problem is that tabularx requires at least one X type column. But the problem is also that there is no magic involved: your table will not shrink in size because tabularx cannot make your content smaller, but only stretch the space to make it bigger.

The following does a bit more in order to at least fit the table onto A4 when rotated for viewing in landscape mode.

R and C are new column types, based on X defined using the array package. These are substituted for r and c. The header lines must be handled specially. \centering is added here, with \arraybackslash added in the final column to preserve the use of \\ to end the row.

\hskip0pt is added to every column to allow hyphenation of the first word in the cell.

Implementing this and deleting some of the gunky stuff (e.g. \textbf{}), we get:

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

producing:

table on A4

This is hardly attractive or, even, especially intelligible, but it does fit on the page, albeit with several complaints about bad boxes.

Beyond this, you need to think about how to present the data. For example, right now, the major/minor column is useless since all cases shown are major. So that could be incorporated into the caption or added as a note beneath the table or whatever would be most appropriate. Similarly, the speciality column is useless. Even if this is only part of a longer table, this kind of information can be presented as headings for sub-divisions within the table, rather than taking up a column which will mostly consist of empty space. In any case, as it is, you can't fit much more on the page, so presumably this is, in fact, a single table and any other data will go in other tables.

Something like this is a bit less indigestible:

improved digestibility

But something like this is probably a further improvement:

further improvement

This was done using threeparttablex (though threeparttable would do here) to create the notes, allowing the headings to be made more concise. The % symbols have also been moved into the relevant heading and the percentile removed, further reducing the lines required here. The result fits with no complaints of bad boxes.

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

Well, I was to slow with my solution to be first :-(. Anyway, I will show results of my (two hours, most spend in formatting of my MWE code) effort. It slightly different from @cfr solution, however main points are the same.

To fit table into text width I use packages ltablex, threeparttablex, and to long column heads and procedures description describe with full names in table notes. I also use package siunitx for better number formatting and remuve local use of \tectbf{...} and \%. By this measure and considered, that left/right margins width are 25 mm I obtain:

enter image description here

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

The following solution uses a combination of (modified) X columns for the header cells and S columns (from the siunitx package) for the contents of the numeric columns. To allow line breaks at / symbols, I've replaced them with \slash macros. I've gotten rid of most bold-facing (except the header cells) as it mostly yells "I know how to shout". And, I've applied automatic rounding to the numbers in the next-to-last column, as I don't think your readers will get anything out of being shown 8 or 9 digits after the decimal point.

enter image description here

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

Addendum: Two issues are not addressed by the preceding solution: First, is the first (left-hand-most) column needed? Instead of taking up an entire column, could the "General surgery" tag be placed in the table's caption? Second, the column "major/minor grouping" appears to be redundant as every single cell in that column says "major". Could this information be placed in the caption as well? For sure, the table would look a lot cleaner and would be easier to read without these two (apparently redundant) columns.

Assuming a somewhat-normal width of the text block, removing the two redundant columns and providing the eliminated information in the table's caption makes it possible to typeset the entire table in portrait mode rather than in landscape mode. Your readers may appreciate not having to crane their necks just to be able to read the table.

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .