单列多行

单列多行

我有下表:

\documentclass{article}
\usepackage{multirow}
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
\footnotesize
\begin{tabular}{lllll}
\toprule
Algorithm & Learner Memory & Production & Interpretation & Hyperparameters\\
\midrule
KNN & Samples (S) & KNN-based & KNN-based & $k$\\
\multirow{2}{*}{PE}  & Prototypes (P) & Discriminative & Nearest & $\alpha$\\
                     &                & Descriptive    &         &         \\
\multirow{2}{*}{AP}  & P + S          & Discriminative & Nearest & /       \\
                     &                & Descriptive    &         &         \\
\multirow{2}{*}{CWP} & P + Counts     & Discriminative & Nearest & $\alpha$\\
                     &                & Descriptive    &         &         \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

如您所见,我习惯multirow在第三列中有两个条目。但是,我不喜欢输出的外观。虽然第一列(Algorithm)中的条目垂直居中,但由于多行,其他只有 1 个条目的列(Learner MemoryInterpretationHyperparameters)在顶部对齐。我如何才能将这些条目也居中?

答案1

使用makecell。我在行之间添加了一些填充:

\documentclass{article}
\usepackage{multirow, makecell}
\usepackage{booktabs}
\def\DisDes{\makecell{Discriminative\\ Descriptive}}

\begin{document}

\begin{table}
  \centering
  \footnotesize
  \renewcommand{\cellalign}{lc}
  \begin{tabular}{lllll}
    \toprule
    Algorithm & Learner Memory & Production & Interpretation & Hyperparameters \\
    \midrule
    KNN & Samples (S) & KNN-based & KNN-based & $k$ \\
    \addlinespace
    PE & Prototypes (P) & \DisDes & Nearest & $\alpha$ \\
    \addlinespace
    AP & P + S & \DisDes & Nearest & / \\
    \addlinespace
    CWP & P + Counts & \DisDes & Nearest & $\alpha$ \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document} 

在此处输入图片描述

答案2

我会采取 Bernard 的优秀解决方案,但使用堆栈而不是makecell(并使用更少的击键)。

\documentclass{article}
\usepackage{multirow,stackengine}
\usepackage{booktabs}
\def\DisDes{\addstackgap[3pt]{\Centerstack[l]{Discriminative Descriptive}}}
\begin{document}
\begin{table}
  \centering
  \footnotesize
  \begin{tabular}{lllll}
    \toprule
    Algorithm & Learner Memory & Production & Interpretation & Hyperparameters \\
    \midrule
    KNN & Samples (S) & KNN-based & KNN-based & $k$ \\
    PE & Prototypes (P) & \DisDes & Nearest & $\alpha$ \\
    AP & P + S & \DisDes & Nearest & / \\
    CWP & P + Counts & \DisDes & Nearest & $\alpha$ \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document} 

在此处输入图片描述

答案3

如果我理解正确的话,将两行单元格标记为单个单元格比尝试使表格的其余部分跨越两行更简单。

在此处输入图片描述

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\newcommand\m[1]{%
\renewcommand\arraystretch{1}%
\begin{tabular}{@{}l@{}}#1\end{tabular}}
\begin{table}
\centering
\renewcommand\arraystretch{1.1}
\footnotesize
\begin{tabular}{lllll}
\toprule
Algorithm & Learner Memory & Production & Interpretation & Hyperparameters\\
\midrule
KNN & Samples (S) & KNN-based & KNN-based & $k$\\
PE  & Prototypes (P) & Discriminative & \m{Nearest\\Descriptive} & $\alpha$\\
AP  & P + S          & Discriminative & \m{Nearest\\Descriptive} & /       \\
CWP & P + Counts     & Discriminative & \m{Nearest\\Descriptive} & $\alpha$\\
\bottomrule
\end{tabular}
\end{table}

\end{document}

相关内容