表格中单行和多行列标题的对齐(LaTeX)

表格中单行和多行列标题的对齐(LaTeX)

请考虑下表:

\documentclass[10pt,letterpaper,oneside]{article}
\usepackage[dvips,top=1.2in,bottom=0.65in,left=1.15in,right=1.15in,includefoot]{geometry}

\usepackage{booktabs}

\begin{document}

\begin{center}
\begin{tabular}{p{4.2cm}p{1.5cm}p{5.5cm}p{3.25cm}}
\toprule
 \emph{\centering observed frequencies of each detection history} & \emph{\centering detections per site} & \emph{equivalent detection histories} ($h_i$) & \emph{probability} \\
\midrule
\multicolumn{1}{c} {10}&    \multicolumn{1}{c}4    &\texttt{1111}&    $\psi p^4$\\
\multicolumn{1}{c}{20}&    \multicolumn{1}{c}3    &\texttt{1110, 1101, 1011, 0111}&    $\psi p^3(1-p)$\\
\multicolumn{1}{c}{30}&\multicolumn{1}{c}2    &\texttt{1100, 1001, 1010, 0011, 0101}&     $\psi p^2(1-p)^2$\\
\multicolumn{1}{c}{20}&    \multicolumn{1}{c}1    &\texttt{1000, 0100, 0010, 0001}&    $\psi p(1-p)^3$\\
\multicolumn{1}{c}{20}&    \multicolumn{1}{c}0    &\texttt{0000}    & $\psi (1-p)^4 + (1-\psi)$\\
\bottomrule
\end{tabular}
\end{center}

\end{document}

我正在尝试弄清楚如何:

  1. 将列标题居中(而不是默认的左对齐),以及
  2. 如何强制单行标题(例如示例中的第 3 列和第 4 列)对齐在中间或标题“单元格”的底部。

我能弄清楚如何生成多行列标题的唯一方法是使用“段落”,但如果我这样做,我无论如何也想不出如何使列标题文本居中,或者将其对齐到顶部以外的任何位置。

答案1

如果所有行都不超过一行,那么使用多行列并没有什么实际好处。在这些情况下,lc是最简单的。

对于其他表格,您可以使用array代替m{}进行p{}垂直居中和>{\centering\arraybackslash}进行水平居中。此外,请考虑使用tabularx以避免需要手动计算正确的宽度。对于您的示例中的表格,表格对于页面来说太宽了。

\documentclass{article}
\usepackage{array,tabularx}
\usepackage[top=1.2in,bottom=0.65in,left=1.15in,right=1.15in,includefoot]{geometry}
\usepackage{booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{document}

\begin{center}
  \begin{tabularx}{\linewidth}{YC{1.5cm}ll}
  \toprule
  \emph{observed frequencies of each detection history} & \emph{detections per site} & \multicolumn{1}{c}{\emph{equivalent detection histories} ($h_i$)} & \multicolumn{1}{c}{\emph{probability}} \\
  \midrule
  10& 4 &\texttt{1111}& $\psi p^4$\\
  20& 3 &\texttt{1110, 1101, 1011, 0111}&   $\psi p^3(1-p)$\\
  30& 2 &\texttt{1100, 1001, 1010, 0011, 0101}&  $\psi p^2(1-p)^2$\\
  20& 1 &\texttt{1000, 0100, 0010, 0001}&   $\psi p(1-p)^3$\\
  20& 0 &\texttt{0000}  & $\psi (1-p)^4 + (1-\psi)$\\
  \bottomrule
\end{tabularx}
\end{center}    
\end{document}

决赛桌

答案2

在此处输入图片描述

\documentclass[10pt,letterpaper,oneside]{article}
\usepackage[dvips,
            top=1.2in,bottom=0.65in,left=1.15in,right=1.15in,
            includefoot]{geometry}
\usepackage{array,
            booktabs}
\newcommand\mch[2]{\multicolumn{1}{>{\centering\arraybackslash\itshape}b{#1}}{#2}}

\begin{document}
\begin{center}
\begin{tabular}{c c 
                >{\ttfamily}l
                >{$}l<{$}
                }
    \toprule
\mch{3.5cm}{observed frequencies of each detection history} 
    &   \mch{1.5cm}{detections per site} 
            &   \mch{5.5cm}{equivalent detection histories ($h_i$)} 
                &   \mch{3cm}{probability}    \\
\midrule
10  &   4   &   1111                        &   \psi p^4                  \\
20  &   3   &   1110, 1101, 1011, 0111      &   \psi p^3(1-p)             \\
3   &   2   &   1100, 1001, 1010, 0011, 0101&   \psi p^2(1-p)^2           \\
20  &   1   &   1000, 0100, 0010, 0001      &   \psi p(1-p)^3             \\
20  &   0   &   0000                        &   \psi (1-p)^4 + (1-\psi)   \\
\bottomrule
\end{tabular}
\end{center}
\end{document}

列宽由新命令(您可以选择其他名称)确定,该命令\mch有两个参数:第一个是列宽,第二个是单元格的内容。内容居中并以斜体字体显示。

答案3

一个简单的解决方案是使用makecell,它允许换行和使用和命令对l, r, c列中的单元格进行通用格式设置。默认对齐方式是垂直和水平居中。\thead\makecell

\documentclass{article}
\usepackage[top=1.2in,bottom=0.65in,left=1.15in,right=1.15in,includefoot]{geometry}
\usepackage{booktabs}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize\itshape}

\begin{document}

\begin{center}
  \begin{tabular}{ccll}
  \toprule
  \thead{observed frequencies of\\each detection history} & \thead{detections\\ per site} & \thead[l]{equivalent detection histories ($h_i$)} & \thead[l]{probability} \\
  \midrule
  10& 4 &\texttt{1111}& $\psi p^4$\\
  20& 3 &\texttt{1110, 1101, 1011, 0111}& $\psi p^3(1-p)$\\
  30& 2 &\texttt{1100, 1001, 1010, 0011, 0101}& $\psi p^2(1-p)^2$\\
  20& 1 &\texttt{1000, 0100, 0010, 0001}& $\psi p(1-p)^3$\\
  20& 0 &\texttt{0000} & $\psi (1-p)^4 + (1-\psi)$\\
  \bottomrule
\end{tabular}
\end{center}
\vspace{1cm}
\begin{center}
\renewcommand{\theadalign}{b}
  \begin{tabular}{ccll}
  \toprule
  \thead{observed frequencies of\\each detection history} & \thead{detections\\ per site} & \thead[l]{equivalent detection histories ($h_i$)} & \thead[l]{probability} \\
  \midrule
  10& 4 &\texttt{1111}& $\psi p^4$\\
  20& 3 &\texttt{1110, 1101, 1011, 0111}& $\psi p^3(1-p)$\\
  30& 2 &\texttt{1100, 1001, 1010, 0011, 0101}& $\psi p^2(1-p)^2$\\
  20& 1 &\texttt{1000, 0100, 0010, 0001}& $\psi p(1-p)^3$\\
  20& 0 &\texttt{0000} & $\psi (1-p)^4 + (1-\psi)$\\
  \bottomrule
\end{tabular}
\end{center}

\end{document} 

在此处输入图片描述

相关内容