3

I am trying to create a table that includes multiple rows in a single cell without using \shortstack which seems to squash the words close together. In the sample I include below I opted to just create new rows with what I want to have on the second line. This works, but I don't like how single-lined rows are not centered.

How can this be fixed?

Maybe \multirow? Maybe a p column, which I do not know how to use?

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

enter image description here

I did find this thread, but I did not see its application here (either that or I do not know how to apply it to this situation).

3 Answers 3

4

For example can you use package makecell and command \makecell like this:

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

with the result:

resulting table

As you can see I changed the first two rows (see red arrows in screenshot and marked code <======= in mwe).

Have you considered to get rid of the vertical and horizontal lines? Would be a better table I think ...

0
4

With m{...} column type in the first column, increasing vertical gapes in cells by help of cellspace and using medium size of fraction (\mfrac) defined in the nccmath package:

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

enter image description here

2
  • Last line, first column: you should remove the \newline following Poisson, as it ruins the centering. :)
    – frougon
    May 29, 2019 at 7:11
  • @frougon, you are right. corrected.
    – Zarko
    May 29, 2019 at 8:44
1

And if you use the cals package, the code and result may look like this if you remove vertical lines. No need for double rows, but if you really need them, it is no problem to change the code to keep them, and have the cell content to align correctly:

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .