Change table spacing

edit:

  • with use of the tabularx,
  • text is vertically centered in cells by:

    \renewcommand\tabularxcolumn[1]{m{#1}}
    
  • font size is set to \footnotesize

  • for units in cells' contents is used the siunitx package
  • more vertical space around the cell contents is done by use of the cellspace package
  • for preserving features of the X columns is used (modified) macro

    \addparagraphcolumntypes{>{\null}Y}
    

    where Y is defined by \newcolumntype{Y}{>{\centering\arraybackslash}X}

  • width of the multicolumn cells is defined by new command

    \usepackage{xparse}
    \DeclareExpandableDocumentCommand\xmc{m m}%
        {\multicolumn{#1}
                     {C{>{\hsize=\dimexpr#1\hsize+
                                 #1\tabcolsep+#1\tabcolsep-2\tabcolsep+
                                 #1\arrayrulewidth\relax}Y}|}
                     {#2}}
    
  • mwe:

    \documentclass{article}
    \usepackage[margin=20mm]{geometry}
    
    \usepackage{cellspace, makecell, tabularx}
    \setlength\cellspacetoplimit{5pt}
    \setlength\cellspacebottomlimit{5pt}
    \renewcommand\tabularxcolumn[1]{m{#1}}
    \newcolumntype{Y}{>{\centering\arraybackslash}X}
    \addparagraphcolumntypes{>{\null}Y}
    \usepackage{xparse}
    \DeclareExpandableDocumentCommand\xmc{m m}%
        {\multicolumn{#1}
                     {C{>{\hsize=\dimexpr#1\hsize+
                                 #1\tabcolsep+#1\tabcolsep-2\tabcolsep+
                                 #1\arrayrulewidth\relax}Y}|}
    \usepackage{siunitx}
    
    \begin{document}
        \begin{table}
    \footnotesize
    \setlength\tabcolsep{3pt}
    \begin{tabularx}{\linewidth}{|Cc|*{13}{C{Y}|}}
        \hline
    \makecell{Study\\ month}
        & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 \\
        \hline
    SD  &   \multicolumn{13}{Cc|}{Standard diet,
            fed \SI{2}{\%} of mean BW until \SI{35}{kg}, then fixed ration of \SI{500}{g}} \\
        \hline
    FFC & \xmc{5}{High fat, fructose, cholesterol (\SI{2}{\%}), fed \SI{2}{\%} of mean BW}
            &   \xmc{6}{High fat, fructose, cholesterol (\SI{1}{\%}),
                fed \SI{2}{\%} of mean BW (max \SI{1000}{g})}
                &   \xmc{2}{High fat, fructose,  cholesterol (\SI{1}{\%}),
                    changed to fixed ration of \SI{500}{g}} \\
        \hline
    FFC/SD
        &   \xmc{5}{High fat, fructose, cholesterol (\SI{2}{\%}),
            fed \SI{2}{\%} of mean BW}
                &   \xmc{2}{High fat, fructose, cholesterol (\SI{1}{\%}),
                    fed \SI{2}{\%} of mean BW (max \SI{1000}{g})}
                    &   \xmc{6}{Changed to standard diet,
                        fed fixed ration of \SI{500}{g}.}\\
        \hline
    FFC$_\mathrm{DIA}$
        &   \multicolumn{13}{Cc|}{High fat, fructose, cholesterol (\si{1}{\%}),
            fed \SI{2.5}{\%} of mean BW (max \SI{1000}{g})} \\
        \hline
    \end{tabularx}
        \end{table}
    \end{document}
    

enter image description here


One possible solution is to use the array package. Here's my proposed code. Notice I use \tabularnewline so as to avoid confusion with the \\ in each paragraph cell. Also, you repeat \multicolumn a sufficient number of times that it justifies the creation of a \cmulticolumn command (which also includes some formatting parameters). The solution makes use of a nice thing about the array package: it allows one to define a tabular token (z, for example) which displays a paragraph of a given with.

\documentclass{article}
\usepackage{pdfpages}
\usepackage{array}
\newcolumntype{z}[1]{>{\centering}p{#1}}

\begin{document}

\newdimen\mycol
\setlength\mycol{10ex}
\def\cmulticolumn#1#2{%
\multicolumn{#1}{c|}{\parbox{#1\mycol}{\mbox{}\\[1ex]\centering#2\\\mbox{}}}
}

\resizebox{\textwidth}{!}{%
\def\arraystretch{2}
\begin{tabular}{|z{13ex}*{13}{|z{\mycol}}|}
\hline
Study month & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 \tabularnewline\hline
SD & \cmulticolumn{13}{Standard diet,\\ fed 2\% of mean BW until 35kg, then fixed ration of 500g} \tabularnewline\hline
FFC & \cmulticolumn{5}{High fat, fructose, cholesterol (2\%), \\ fed 2\% of mean BW} & \cmulticolumn{6}{High fat, fructose, cholesterol (1\%),\\ fed 2\% of mean BW (max 1000g)} & \cmulticolumn{2}{High fat, fructose, \\ cholesterol (1\%),\\ changed to fixed\\ ration of 500g} \tabularnewline\hline
FFC/SD & \cmulticolumn{5}{High fat, fructose, cholesterol (2\%),\\ fed 2\% of mean BW} & \cmulticolumn{2}{High fat, fructose,\\ cholesterol (1\%),\\ fed 2\% of mean \\ BW (max 1000g)} & \cmulticolumn{6}{Changed to standard diet, \\ fed fixed ration of 500g.}\tabularnewline \hline
FFC$_{DIA}$ & \cmulticolumn{13}{High fat, fructose, cholesterol (1\%), \\ fed 2.5\% of mean BW (max 1000g)} \tabularnewline\hline
\end{tabular}
}

\end{document}

enter image description here


The following calculates the available width for equal spaced columns so that the resulting tabular is exactly \linewidth wide. The macro doing so is called \equalspacing and takes as arguments an optional first column (specify the widest entry) and the number of columns you need. The resulting column width is stored in \equalspacingwidth. The calculation assumes that there is a vertical rule everywhere and each column has a full \tabcolsep on both sides (so that you don't use @{}). You can then specify the columns with the *{<count>}{<definition>} notation (e.g. *{13}{p{\equalspacingwidth}|} in for 13 columns).

Further a \multiequal{<count>}{<content>} is defined, which is a \multicolumn as wide as <count> columns of width \equalspacingwidth (plus the \tabcolsep and \arrayrulewidth for each of the spanned columns).

With these two macros you can get the following:

\documentclass{article}

\usepackage{amsmath}                                                            
\usepackage[top=3cm, bottom=3cm,
            inner=3cm, outer=2cm, footskip = 1.5cm]{geometry}       
\usepackage{pdfpages}
\usepackage{array}
\makeatletter
\newlength\equalspacingwidth
\newsavebox\equalspacing@TMPbox
\newlength\equalspacing@tmp@length
\newcommand\equalspacing[2][]
  {%
    \equalspacingwidth\linewidth
    \equalspacing@tmp@length\arrayrulewidth
    \advance\equalspacing@tmp@length2\tabcolsep
    \if\relax\detokenize{#1}\relax
    \else
      \setbox\equalspacing@TMPbox\hbox
        {%
          \begingroup
          \def\colsep{\hskip2\tabcolsep}%
          \def\colsepR{\hskip2\tabcolsep\hskip\arrayrulewidth}%
          #1%
          \endgroup
        }%
      \advance\equalspacingwidth by -\wd\equalspacing@TMPbox
      \advance\equalspacingwidth by -2\tabcolsep
      \advance\equalspacingwidth by -\arrayrulewidth
    \fi
    \advance\equalspacingwidth by -\arrayrulewidth
    \divide\equalspacingwidth by #2
    \advance\equalspacingwidth by -\equalspacing@tmp@length
  }
\newcommand\multiequal[2]
  {%
    \multicolumn{#1}
      {%
        >{\raggedright\arraybackslash}p{%
          \dimexpr
            #1\equalspacingwidth
            +#1\arrayrulewidth
            +#1\tabcolsep
            +#1\tabcolsep
            -2\tabcolsep
            -\arrayrulewidth
          \relax
        }|%
      }{#2}%
  }
\newcommand\onecell[2][c]
  {%
    \begin{tabular}{@{}#1@{}}#2\end{tabular}%
  }
\makeatother

\begin{document}
\begin{table}[h]
\centering
\equalspacing[FFC/SD]{13}
\begin{tabular}{|c|*{13}{>{\centering\arraybackslash}p{\equalspacingwidth}|}}
\hline
  \onecell{Study\\month}
    & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 \\ \hline
  SD
    & \multiequal{13}
        {%
          Standard diet, fed 2\% of mean BW until 35kg, then fixed ration of
          500g%
        }
  \\ \hline
  FFC
    & \multiequal{5}{High fat, fructose, cholesterol (2\%), fed 2\% of mean BW}
    & \multiequal{6}
        {High fat, fructose, cholesterol (1\%), fed 2\% of mean BW (max 1000g)}
    & \multiequal{2}
        {High fat,fructose, cholesterol (1\%), changed to fixed ration of 500g}
  \\ \hline
  FFC/SD
    & \multiequal{5}
        {High fat, fructose, cholesterol (2\%), fed 2\% of mean BW}
    & \multiequal{2}
        {High fat, fructose, cholesterol (1\%), fed 2\% of mean BW (max 1000g)}
    & \multiequal{6}{Changed to standard diet, fed fixed ration of 500g.}
  \\ \hline
  FFC$_{DIA}$
    & \multiequal{13}
        {%
          High fat, fructose, cholesterol (1\%), fed 2.5\% of mean BW (max
          1000g)%
        }
  \\ \hline
\end{tabular}
\end{table}
\end{document}

enter image description here

EDIT: To get vertically centred cells, you can use the m specifier instead of p in the definition of \multiequal.

So with the following definition of \multiequal and the same remaining document you get this:

\newcommand\multiequal[2]
  {%
    \multicolumn{#1}
      {%
        >{\raggedright\arraybackslash}m{%
          \dimexpr
            #1\equalspacingwidth
            +#1\arrayrulewidth
            +#1\tabcolsep
            +#1\tabcolsep
            -2\tabcolsep
            -\arrayrulewidth
          \relax
        }|%
      }{#2}%
  }

enter image description here

Tags:

Spacing

Tables