I would like to create a table that looks like the one from Excel (First picture). But the top row is creating weird spacing in LaTeX (Second picture.) Excel table

    \usepackage{amsmath}                                                            
\usepackage[top=3cm, bottom=3cm,
            inner=3cm, outer=2cm, footskip = 1.5cm]{geometry}       
\usepackage{booktabs}
\usepackage{pdfpages}

\begin{document}
\begin{table}[h]
\centering
\resizebox{\textwidth}{!}{\begin{tabular}{c|c|c|c|c|c|c|c|c|c|c|c|c|c}
\toprule
\begin{tabular}[c]{@{}c@{}}
    Study \\ month\end{tabular} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 \\ \hline

    SD & \multicolumn{13}{c}{\begin{tabular}[c]{@{}c@{}}Standard diet, \\fed 2\% of mean BW until 35kg, then fixed ration of 500g \end{tabular}}\\ \hline

    FFC & \multicolumn{5}{c|}{\begin{tabular}[c]{@{}c@{}}High fat, fructose, cholesterol (2\%), \\ fed 2\% of mean BW \end{tabular}} & \multicolumn{6}{c|}{\begin{tabular}[c]{@{}c@{}}High fat, fructose, cholesterol (1\%),\\ fed 2\% of mean BW (max 1000g) \end{tabular}}& \multicolumn{2}{c}{\begin{tabular}[c]{@{}c@{}}High fat,fructose, \\cholesterol (1\%),\\
    changed to fixed\\ ration of 500g \end{tabular}} \\ \hline

    FFC/SD                                                 & \multicolumn{5}{c|}{\begin{tabular}[c]{@{}c@{}}High fat, fructose, cholesterol (2\%),\\ fed 2\% of mean BW
    \end{tabular}} & \multicolumn{2}{c|}{\begin{tabular}[c]{@{}c@{}}High fat, fructose,\\ cholesterol (1\%),\\ fed 2\% of mean \\ BW (max 1000g)\end{tabular}} & \multicolumn{6}{c}{\begin{tabular}[c]{@{}c@{}}Changed to standard diet,  \\
    fed fixed ration of 500g.
    \end{tabular}}                                                              \\ \hline

    FFC$_{DIA}$                                                 & \multicolumn{13}{c}{\begin{tabular}[c]{@{}c@{}}High fat, fructose, cholesterol (1\%), \\
    fed 2.5\% of mean BW (max 1000g)
\end{tabular}}                                                                         \\ \bottomrule
\end{tabular}}
\end{table}
\end{document}

Latex Table

I feel I have tried everything, but I cannot fix it my self. Kindly asking for help.

  • This somes from the rules of booktabs, which are not designed for use with vertical rules as they add some vertical padding. You can add, just after \begin{table}, \aboverulesep=0pt and \belowrulesep=0pt. – Bernard Aug 17 at 15:37
  • And the unequal space for your columns is caused by the way a too wide \multicolumn is handled (adding all of the excess space to the last column). – Skillmon Aug 17 at 15:45
  • Don't use \resizebox and the like on tables. It causes inconsistent font sizes and spacing. – Skillmon Aug 17 at 15:47
up vote 3 down vote accepted

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

  • @CamillaAarupKristensen glad it helped you. A thank you however should be expressed in votes rather than comments (if you didn't vote, that is). Furthermore you should accept the best suited answer (you got 3 to choose from and it's been 2 days). – Skillmon Aug 20 at 11:07

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

  • Might be me, but from your screenshot the vertical rules don't seem aligned. – Skillmon Aug 17 at 18:35
  • @Skillmon, your observation is very good :-)except for mcx i didn't consider \arrayrulewidth at other \multicolum cells. and also conside wrong numberof \tabcolsep. corrected now – Zarko Aug 17 at 18:39
  • You could create one \mcx for spanning arbitrary number of X columns (would be very similar to my \multiequal). – Skillmon Aug 17 at 18:44
  • @Skillmon, done. i use xparse package for this and spend a wile to solve problem with \addparagraphcolumntypes which works properly at adjusting width of the multicolumn` cells. see edited answer. – Zarko Aug 18 at 4:48

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

  • 1
    You should use \newcolumntype{z}[1]{>{\centering\arraybackslash}p{#1}} for the column definition. This way you don't need to use \tabularnewline after a column using this definition. – Skillmon Aug 17 at 17:38
  • Good point, @Slillmon; thanks. I might add that my \cmulticolumn command does not make use of all of the space available since it does not include the space between columns. However, to the extent that the text is centered, this does not matter that much. Also, there may be more elegant ways of adding space above and below each row. – LMBC Aug 17 at 18:02

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.