我正在尝试创建一个在单个单元格中包含多行的表格,而不使用\shortstack
似乎会将单词挤压在一起的按钮。在下面的示例中,我选择只创建新行,并将我想要的内容放在第二行。这可行,但我不喜欢单行不居中的情况。
如何修复此问题?
也许\multirow
?也许是一p
列,但我不知道如何使用?
\documentclass{article}
\usepackage[margin= .75in]{geometry}
\usepackage{amsmath}
\begin{document}
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
distribution & pmf & $\mu$ & $\sigma^{2}$ & mgf \\
\hline
Uniform & $ f(x) = \frac{1}{m}, \quad x=0,1,\dots,m $ & $ \frac{m+1}{2} $ & $ \frac{m^{2} - 1}{12} $ & -- \\
$X \sim U(m)$ & & & & \\
\hline
Hypergeometric & $f(x) \frac{ \binom{N_{1}}{x} \binom{N_{2}}{n-x} }{ \binom{N}{n} }, \quad x= 0,1,\dots, n$ & $n\frac{N_{1}}{N}$ & $n \frac{N_{1}}{N} \frac{N_{2}}{N} \frac{N-n}{N-1}$ & -- \\
$N = N_{1} + N_{2}$ & & & & \\
\hline
Bernoulli & $f(x) = p^{x}q^{1-x}, \quad x = 0,1 $ & $p$ & $pq$ & $1 - p + pe^{t}$ \\
\hline
Binomial & $ f(x) = \binom{n}{x} p^{x}q^{n-x}, \quad x = 0,1,\dots, n $ & $np$ & $npq$ & $(1-p+pe^{t})^{n}$ \\
$X \sim b(n,p)$ & & & & \\
\hline
Poisson & $f(x) = \frac{\lambda^{x} e ^{-\lambda}}{x!}$ & $\lambda$ & $\lambda$ & $e^{\lambda e^{t}}$ \\
$X \sim Poisson(\lambda)$ & & & & \\
\hline
\end{tabular}
\end{center}
\end{document}
我确实找到了这线程,但我没有在这里看到它的应用(要么就是我不知道如何将它应用于这种情况)。
答案1
例如,您可以使用这样的包makecell
和命令:\makecell
\documentclass{article}
\usepackage[margin= .75in]{geometry}
\usepackage{amsmath}
\usepackage{makecell} % <===============================================
\begin{document}
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
distribution & pmf & $\mu$ & $\sigma^{2}$ & mgf \\
\hline
\makecell{Uniform \\ $X \sim U(m)$} % <===============================
& $ f(x) = \frac{1}{m}, \quad x=0,1,\dots,m $ & $ \frac{m+1}{2} $ & $ \frac{m^{2} - 1}{12} $ & -- \\
\hline
\makecell{Hypergeometric \\ $N = N_{1} + N_{2}$} % <==================
& $f(x) \frac{ \binom{N_{1}}{x} \binom{N_{2}}{n-x} }{ \binom{N}{n} }, \quad x= 0,1,\dots, n$ & $n\frac{N_{1}}{N}$ & $n \frac{N_{1}}{N} \frac{N_{2}}{N} \frac{N-n}{N-1}$ & -- \\
\hline
Bernoulli & $f(x) = p^{x}q^{1-x}, \quad x = 0,1 $ & $p$ & $pq$ & $1 - p + pe^{t}$ \\
\hline
Binomial & $ f(x) = \binom{n}{x} p^{x}q^{n-x}, \quad x = 0,1,\dots, n $ & $np$ & $npq$ & $(1-p+pe^{t})^{n}$ \\
$X \sim b(n,p)$ & & & & \\
\hline
Poisson & $f(x) = \frac{\lambda^{x} e ^{-\lambda}}{x!}$ & $\lambda$ & $\lambda$ & $e^{\lambda e^{t}}$ \\
$X \sim Poisson(\lambda)$ & & & & \\
\hline
\end{tabular}
\end{center}
\end{document}
结果如下:
正如您所看到的,我改变了前两行(参见屏幕截图中的红色箭头和<=======
mwe 中标记的代码)。
您是否考虑过去掉垂直线和水平线?我认为这样会更好...
答案2
m{...}
在第一列中使用列类型,通过包中定义的cellspace
分数()的中等大小来增加单元格的垂直间隙:\mfrac
nccmath
\documentclass{article}
\usepackage[margin= .75in]{geometry}
\usepackage{nccmath} % new
\DeclareMathOperator{\e}{e} % new
\usepackage{array,cellspace} % new
\setlength\cellspacetoplimit{5pt} % new
\setlength\cellspacebottomlimit{5pt} % new
\begin{document}
\begin{center}
\begin{tabular}{|>{\centering}S{m{8em}}| % changed
*{4}{>{$\displaystyle}Sc<{$}|}} % changed
\hline
distribution
& \text{pmf} & \mu & \sigma^{2} & \text{mgf} \\
\hline
Uniform $X\sim U(m)$
& f(x) = \mfrac{1}{m}, \quad x=0,1,\dots,m
& \frac{m+1}{2}
& \mfrac{m^{2} - 1}{12}
& -- \\
\hline
Hypergeometric $N = N_{1} + N_{2}$
& f(x)\frac{\binom{N_{1}}{x} \binom{N_{2}}{n-x} }{\binom{N}{n} }, \quad
x = 0,1,\dots,n
& n\mfrac{N_{1}}{N}
& n\mfrac{N_{1}}{N} \mfrac{N_{2}}{N} \mfrac{N-n}{N-1}
& -- \\
\hline
Bernoulli
& f(x) = p^{x}q^{1-x}, \quad x = 0,1
& p & pq & 1 - p + pe^{t} \\
\hline
Binomial $X\sim b(n,p)$
& f(x) = \binom{n}{x} p^{x}q^{n-x}, \quad x = 0,1,\dots, n
& np & npq & (1-p+p\e^{t})^{n} \\
\hline
Poisson \mbox{$X\sim\mathit{Poisson}(\lambda)$}
& f(x) = \frac{\lambda^{x} \e^{-\lambda} }{x!}
& \lambda & \lambda & \e^{\lambda \e^{t}}\\
\hline
\end{tabular}
\end{center}
\end{document}
答案3
如果你使用cals 包,如果删除垂直线,代码和结果可能如下所示。不需要双行,但如果确实需要,更改代码以保留它们并让单元格内容正确对齐也是没有问题的:
\documentclass{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{amsmath, nccmath, cals}
\usepackage[table]{xcolor}
\DeclareMathOperator{\e}{e}
\begin{document}
\begin{calstable}
\colwidths{{\dimexpr(\columnwidth/40 *7)\relax}
{\dimexpr(\columnwidth/40 *14)\relax}
{\dimexpr(\columnwidth/40 *6)\relax}
{\dimexpr(\columnwidth/40 *6)\relax}
{\dimexpr(\columnwidth/40 * 7)\relax}
}
\makeatletter
\def\cals@framers@width{0.8pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0pt}
% R1 Heading
\thead{\bfseries
\brow
\setlength\cals@paddingL{0pt}
\alignC\cell{distribution}
\cals@setpadding{Ag}
\alignC\cell{pmf}
\alignC\cell{$\mu$}
\alignC\cell{$\sigma^{2}$}
\setlength\cals@paddingL{0pt}
\alignC\cell{mgf}
\cals@setpadding{Ag}
\erow
\mdseries}
% R2
\brow
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil Uniform\par $X \sim U(m)$}
\cals@setpadding{Ag}
\alignC\cell{\vfil $f(x) = \frac{1}{m}, \quad x=0,1,\dots,m$}
\alignC\cell{\vfil $\frac{m+1}{2}$}
\alignC\cell{\vfil $\frac{m^{2} - 1}{12}$}
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil --}
\cals@setpadding{Ag}
\erow
%R3
\brow
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil Hypergeometric\par $N = N_{1} + N_{2}$}
\cals@setpadding{Ag}
\alignC\alignC\cell{\vfil $f(x) \frac{ \binom{N_{1}}{x} \binom{N_{2}}{n-x} }{ \binom{N}{n} }, \quad x= 0,1,\dots, n$}
\cell{\vfil $n\frac{N_{1}}{N}$}
\cell{\vfil $n \frac{N_{1}}{N} \frac{N_{2}}{N} \frac{N-n}{N-1}$}
\setlength\cals@paddingL{0pt}
\cell{\vfil --}
\cals@setpadding{Ag}
\erow
%R4
\brow
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil Bernoulli}
\cals@setpadding{Ag}
\alignC\cell{\vfil $f(x) = p^{x}q^{1-x}, \quad x = 0,1 $}
\cell{\vfil $p$}
\cell{\vfil $pq$}
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil $1 - p + pe^{t}$}
\cals@setpadding{Ag}
\erow
%R5
\brow
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil Binomial\par $X \sim b(n,p)$}
\cals@setpadding{Ag}
\alignC\cell{\vfil $f(x) = \binom{n}{x} p^{x}q^{n-x}, \quad x = 0,1,\dots, n$}
\cell{\vfil $np$}
\cell{\vfil $npq$}
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil $(1-p+pe^{t})^{n}$}
\cals@setpadding{Ag}
\erow
%R6
\brow
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil Poisson\par $X \sim Poisson(\lambda)$}
\cals@setpadding{Ag}
\alignC\cell{\vfil $f(x) = \frac{\lambda^{x} e ^{-\lambda}}{x!}$}
\cell{\vfil $\lambda$}
\cell{\vfil $\lambda$}
\setlength\cals@paddingL{0pt}
\alignC\cell{\vfil $e^{\lambda e^{t}}$}
\cals@setpadding{Ag}
\erow
\makeatother
\end{calstable}
\end{document}