8

I want to display a table inside an enumerate environment such that the first row aligns with the item label. My current code is the following:

\documentclass{article}
\usepackage{enumitem}
\usepackage{booktabs}

\setenumerate{listparindent=\parindent, parsep=0pt}

\begin{document}
\begin{enumerate}[align=left, label=\large\sffamily\bfseries \alph*)]
  \item
  \begin{tabular}[t]{cccc}
    \toprule
    $m$ & $P(R=a,M=m)$ & $P(R=b,M=m)$ & $P(R=c,M=m)$ \\
    \midrule
    1 & 0.1 & 0.2 & 0.3 \\
    2 & 0.2 & 0.1 & 0.1 \\
    \bottomrule
  \end{tabular}

  \item
  \( 
    \displaystyle
    P(0) = P(0|B) P(B) + P(0|G) P(G)
  \)
\end{enumerate}
\end{document}

Unfortunately (despite using the t option of the tabular environment), the alignment is not as expected due to the \toprule command. If I remove it, everthing is fine. How may I solve this?

Furthermore, how can I center the table horizontally within the current line? I tried to use a floating table by surrounding it with \begin{table}\end{table}, but this changed the vertical placement even more.

1
  • 1
    The issue you're looking to fix isn't really specific to \toprule: It also occurs when the standard LaTeX \hline command is used.
    – Mico
    Nov 9, 2013 at 17:27

3 Answers 3

7

You need to forcefully adjust the first line to align with the label. Below I've introduced ttabular that inserts this vertical adjustment (comprised of a regular baseline skip plus all the rule widths that make up \toprule*). Horizontal centering is obtain using an \hfill<item>\hfill\null approach. You can also use a box of width \linewidth.

enter image description here

\documentclass{article}
\usepackage{enumitem,booktabs}% http://ctan.org/pkg/{enumitem,booktabs}

\setenumerate{listparindent=\parindent, parsep=0pt}

\newenvironment{ttabular}[2][c]
  {\begin{tabular}[#1]{#2}
     \\[\dimexpr-\normalbaselineskip-\abovetopsep-\belowrulesep-\heavyrulewidth]
  }{\end{tabular}}

\begin{document}
\begin{enumerate}[align=left, label=\large\sffamily\bfseries \alph*)]
  \item \hfill%
  \begin{ttabular}[t]{cccc}
    \toprule
    $m$ & $P(R=a,M=m)$ & $P(R=b,M=m)$ & $P(R=c,M=m)$ \\
    \midrule
    1 & 0.1 & 0.2 & 0.3 \\
    2 & 0.2 & 0.1 & 0.1 \\
    \bottomrule
  \end{ttabular}%
  \hfill\null%

  \item
  \( 
    \displaystyle
    P(0) = P(0|B) P(B) + P(0|G) P(G)
  \)
\end{enumerate}
\end{document}

Note that the vertical adjustment causes the tabular to rise quite high, which may overlap with contents above it. In that sense, you'll have to make adjustments to accommodate for that as well, if needed.

* One of these lengths \abovetopsep is actually set to 0pt by default.

7

You can emulate what array does with \firsthline; however, the tabular will most likely clash with the preceding material and you have to add some vertical space anyway:

\documentclass{article}
\usepackage{lipsum} % just for the example
\usepackage{enumitem}
\usepackage{booktabs}

\makeatletter
\newcommand{\firsttoprule}{%
  \addlinespace[\dimexpr-\aboverulesep-\belowrulesep-\heavyrulewidth-\ht\@arstrutbox]
  \toprule}
\makeatother

\setlist[enumerate]{listparindent=\parindent, parsep=0pt}

\begin{document}
\lipsum*[2]
\begin{enumerate}[
  align=left,
  label=\large\sffamily\bfseries\alph*),
  before=\vspace{1.5ex}
]
\item
  \begin{tabular}[t]{cccc}
  \firsttoprule
  $m$ & $P(R=a,M=m)$ & $P(R=b,M=m)$ & $P(R=c,M=m)$ \\
  \midrule
  1 & 0.1 & 0.2 & 0.3 \\
  2 & 0.2 & 0.1 & 0.1 \\
  \bottomrule
  \end{tabular}

\item
  \( 
    \displaystyle
    P(0) = P(0|B) P(B) + P(0|G) P(G)
  \)
\end{enumerate}
\lipsum[2]
\end{document}

enter image description here

Note that \setenumerate is a deprecated command.

6

You may also use the adjustbox package by adjusting the vertical alignment:

enter image description here enter image description here

\documentclass{article}
\usepackage{enumitem}
\usepackage{booktabs}
\usepackage{adjustbox}

\setlist[enumerate]{listparindent=\parindent, parsep=0pt} % edited as per @egreg comment

\begin{document}
\begin{enumerate}[align=left, label=\large\sffamily\bfseries \alph*)]
  \item
    \begin{adjustbox}{valign=t} % The key value, t, adjusts the vertical alignment
    \makebox[\linewidth]{       % Causes the centering
    \begin{tabular}{cccc}
    %\noalign{\vskip-.85ex}      % Uncomment to see the second output
    \toprule
    $m$ & $P(R=a,M=m)$ & $P(R=b,M=m)$ & $P(R=c,M=m)$ \\
    \midrule
    1 & 0.1 & 0.2 & 0.3 \\
    2 & 0.2 & 0.1 & 0.1 \\
    \bottomrule
  \end{tabular}}
    \end{adjustbox}
  \item
  \( 
    \displaystyle
    P(0) = P(0|B) P(B) + P(0|G) P(G)
  \)
\end{enumerate}
\end{document}
2
  • Thanks, I didn't know the adjustbox package before. I picked Werner's answer, though, because I wanted the text of the first row to be on the same baseline as the item label. Your answer might still be helpful in similar cases, so +1. ;)
    – ph4nt0m
    Nov 9, 2013 at 18:35
  • @ph4nt0m see updated answer.
    – azetina
    Nov 9, 2013 at 19:06

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.