7

Consider the following table:

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

I'm trying to figure out how to:

  1. center the column headers (as opposed to the default left-justify), and
  2. how to force single-line headers (e.g, columns 3 and 4 in example) to be aligned either in the middle, or at the bottom of the header 'cell'.

The only way I can figure out how to generate multi-line column headers is using a 'paragraph', but if I do, I can't for the life of me figure out how to center the column header text, or align it at anything other than the top.

  • 1
    Load array and use m rather than p. Assuming you want vertical centring. For horizontal, use >{\centering\arraybackslash} before the m or p. – cfr Jan 8 '17 at 2:27
4

There's no real benefit to using multi-line columns when none of the rows have more than one line. In these cases, l and c are simplest.

For the others, you can use array's m{} in place of p{} for vertical centring and >{\centering\arraybackslash} for horizontal centring. In addition, consider using tabularx to avoid needing to manually figure correct widths. With those in your example, the table is simply too wide for the page.

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

final table

2

enter image description here

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

width of columns is determined with new command \mch (you can select other name) with two arguments: first is width of column, second is content of cell. Content is centered and in italic font shape.

2

A simple solution with makecell, which allows for line breaks and a common formatting of cells in l, r, c columns, with the \thead and \makecell commands. The default alignment is vertical and horizontal centring.

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.