更改表格间距

更改表格间距

我想创建一个看起来像 Excel 中的表格(第一张图片)。但顶行在 LaTeX 中产生了奇怪的间距(第二张图片)。Excel 表格

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

乳胶桌

我觉得我已经尝试了所有方法,但我自己无法修复它。请寻求帮助。

答案1

以下计算等距列的可用宽度,以使结果宽度tabular准确\linewidth。执行此操作的宏被调用\equalspacing,并将可选的第一列(指定最宽的条目)和所需的列数作为参数。结果列宽存储在中。计算假设处处都有垂直规则,并且每列两侧\equalspacingwidth都有完整的(因此您不使用)。然后,您可以使用符号指定列(例如,13 列为)。\tabcolsep@{}*{<count>}{<definition>}*{13}{p{\equalspacingwidth}|}

\multiequal{<count>}{<content>}此外,还定义了一个,它的宽度与宽度的列\multicolumn一样宽(加上每个跨越列的和)。<count>\equalspacingwidth\tabcolsep\arrayrulewidth

使用这两个宏可以得到以下内容:

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

在此处输入图片描述

编辑:要获得垂直居中的单元格,您可以在定义中使用m说明符而不是。p\multiequal

因此,通过以下定义\multiequal和相同的剩余文档,您可以获得以下内容:

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

在此处输入图片描述

答案2

一种可能的解决方案是使用array包。这是我建议的代码。请注意,我使用\tabularnewline以避免与\\每个段落单元格中的混淆。此外,您重复的\multicolumn次数足够多,足以证明创建\cmulticolumn命令是合理的(其中还包括一些格式化参数)。该解决方案利用了包的一个优点array:它允许定义一个表格标记(z例如),它显示给定段落的一段。

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

在此处输入图片描述

答案3

编辑:

  • 使用tabularx
  • 文本在单元格中垂直居中:

    \renewcommand\tabularxcolumn[1]{m{#1}}
    
  • 字体大小设置为\footnotesize

  • 对于单元格内容中的单位使用siunitx
  • 通过使用包,可以在单元格内容周围留出更多垂直cellspace空间
  • 为了保留X列的特征,使用(修改后的)宏

    \addparagraphcolumntypes{>{\null}Y}
    

    其中Y定义为\newcolumntype{Y}{>{\centering\arraybackslash}X}

  • 单元格的宽度multicolumn由新命令定义

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

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

在此处输入图片描述

相关内容