26

The table that I'd like to make does not display fractions with enough space between lines. What's the reasoning for this? (Not enough rep to be able to post a picture yet unfortunately.)

\documentclass{article}
\usepackage{amsmath,amsfonts}
\usepackage{booktabs}

\begin{document}
\begin{table}
\centering
\begin{tabular}{lcccc}
\toprule
\textbf{Care type} & $\mathbb{P}(Pat \cap H)$ & $\mathbb{P}(Pat \cap H')$ & $\mathbb{P}(Pat' \cap H)$ & $\mathbb{P}(Pat' \cap H')$\\
\midrule
Direct care  &$\displaystyle \dfrac{58}{197}$  &  $\displaystyle \dfrac{48}{197}$  & $\displaystyle \dfrac{21}{197}$* &$1-\displaystyle \dfrac{58}{197}$ \\
Housekeeping &$\displaystyle \dfrac{12}{17}$  &  $\displaystyle \dfrac{2}{17}$  & $\displaystyle \dfrac{5}{17}$* &$1-\displaystyle \dfrac{12}{17}$\\
\bottomrule
\end{tabular}
\caption{Probabilities of hand hygiene given direct patient contact}
\label{tab:handwash_given_pat}
\end{table}
\end{document}

Edit: Image added and MWE completed.

Table with too narrow rows

4
  • write before the tabular environment \def\arraystretch{1.5}
    – user2478
    Aug 14, 2012 at 8:49
  • This also changes the spacing in the head line. Aug 14, 2012 at 8:52
  • Thanks! I tried \def\arraystretch{2}, which is just about acceptable. Is this the best workaround?
    – HCAI
    Aug 14, 2012 at 8:59
  • Two suggestions. (1) Why not write 58/197 and so on? (2) All fractions are integral multiples of 1/197, so you could write everything in multiples of 1/197l this would make it easier to compare the probabilities in the same columns.
    – user10274
    Oct 21, 2014 at 13:23

5 Answers 5

23

Manually the spacing can be fixed by several methods:

  • \renewcommand*{\arraystretch}{2} (see Herbert's comment)
    However, this also affects the spacing of the other lines.

  • Adding a vertical space in the optional argument of \\, e.g.:

    \begin{tabular}{...}
      ...
      ...\\[2ex]
      ...\\[1.5ex]
      \bottomrule
    \end{tabular}
    
  • Adding an invisible rule in the line, e.g.:

    \begin{tabular}{...}
      ...
      \rule{0pt}{3.5ex}Direct care ...\\
      \rule[-2.5ex]{0pt}{7ex}Housekeeping ...\\
      \bottomrule
    \end{tabular}
    
3
  • Thank you Heiko, this is very useful! :) Somehow it seems that LaTex doesn't deal with deep table cells well. Why do you think this is?
    – HCAI
    Aug 14, 2012 at 9:12
  • 1
    In the tables array struts are automatically added, kind of invisible rules with .7\baselineskip height and .3\baselineskip depth. The total amount is changed by \arraystretch. Package array allows the addition of \extrarowheight. The strut stuff works with normal text, but the method is limited if large objects are used. Aug 14, 2012 at 10:11
  • Thanks! The "invisible rule" trick is awesome. Exactly what I was looking for ;)
    – citronas
    Apr 21, 2013 at 10:49
8

I'll add three other methods.

First, since you load booktabs, you can add \addlinespace between the second and third rows.

Second, you can use the cellspace package, which enables you to define minimal vertical spacings at the top and bottom of cells in columns with specifier prefixed with the letter S (or C if you use siunitx). This way, the contents is vertically centred in the cell, contrary to the use of \arraystretch.

Last, the makecell package, through its \setcellgapes and \makegapedcells commands, adds vertical spacing at the top and bottom of every cell. This can also be done for individual cells with the \Gaped command.

\documentclass{article}
\usepackage{amsmath,amsfonts}
\usepackage{booktabs}
\usepackage{cellspace}%
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}
\usepackage{makecell}
\setcellgapes{3pt}

\begin{document}

\begin{table}[!htbp]
  \centering
  \begin{tabular}{l*{4}{>{$}c<{$}}}
    \toprule
    \textbf{Care type} & \mathbb{P}(Pat ∩ H) & \mathbb{P}(Pat ∩ H') &  \mathbb{P}(Pat' ∩ H) & \mathbb{P}(Pat' ∩ H')  \\ \midrule
    Direct care & \dfrac{58}{197} & \dfrac{48}{197} & \dfrac{21}{197} * & 1-\dfrac{58}{197}  \\\addlinespace%
    Housekeeping & \dfrac{12}{17} & \dfrac{2}{17} &\dfrac{5}{17}* &  1- \dfrac{12}{17} \\ \bottomrule
  \end{tabular}
  \caption{Probabilities of hand hygiene given direct patient contact}
  \label{tab:handwash_given_pat}
\end{table}

\begin{table}[!htbp]
  \centering
  \begin{tabular}{l*{4}{>{$}Sc<{$}}}
    \toprule
    \textbf{Care type} &  \mathbb{P}(Pat ∩ H)$ & \mathbb{P}(Pat ∩ H') & \mathbb{P}(Pat' ∩ H)  &  \mathbb{P}(Pat' ∩ H') \\ \midrule
    Direct care & \dfrac{58}{197} & \dfrac{48}{197} & \dfrac{21}{197}* &  1-\dfrac{58}{197} \\%
    Housekeeping & \dfrac{12}{17} & \dfrac{2}{17} & \dfrac{5}{17}* &  1-\dfrac{12}{17} \\ \bottomrule
  \end{tabular}
  \caption{Probabilities of hand hygiene given direct patient contact}
  \label{tab:handwash_given_pat}
\end{table}

\begin{table}[!htbp]
  \centering\makegapedcells
  \begin{tabular}{l*{4}{c}}
    \toprule
    \textbf{Care type} & mathbb{P}(Pat ∩ H) & \mathbb{P}(Pat ∩ H') & \mathbb{P}(Pat' ∩ H) & \mathbb{P}(Pat' ∩ H') \\ \midrule
    Direct care & \dfrac{58}{197} & \dfrac{48}{197} & \dfrac{21}{197}$* & 1-\dfrac{58}{197} \\%
    Housekeeping & \dfrac{12}{17} &\dfrac{2}{17} & \dfrac{5}{17}$* & 1-\dfrac{12}{17} \\ \bottomrule
  \end{tabular}
  \caption{Probabilities of hand hygiene given direct patient contact}
  \label{tab:handwash_given_pat}
\end{table}

\end{document} 

enter image description here

2

Another option is to use the package units and then replace your \frac{a}{b} with \nicefrac{a}{b}. The numerator and denominator will be offset diagonally and they fit nicely into tables.

1
  • 1
    You should probably also mention that if one pursues this route, the numbers will be typeset in scriptstyle mode, with a font size that's reduced by 30 percent relative to textstyle mode used in the OP's code.
    – Mico
    Oct 21, 2014 at 13:13
1

With {NiceTabular} of nicematrix, you have two keys cell-space-top-limit and cell-space-bottom-limit which sets the minimal space above and below the cells of the tabular. There is also a key cell-space-limits which is the conjunction of these keys.

\documentclass{article}
\usepackage{amsmath,amsfonts}
\usepackage{booktabs,nicematrix}

\begin{document}
\begin{table}
\centering
\begin{NiceTabular}{lcccc}[cell-space-limits=2pt]
\toprule
\textbf{Care type} & $\mathbb{P}(Pat \cap H)$ & $\mathbb{P}(Pat \cap H')$ & $\mathbb{P}(Pat' \cap H)$ & $\mathbb{P}(Pat' \cap H')$\\
\midrule
Direct care  &$\displaystyle \dfrac{58}{197}$  &  $\displaystyle \dfrac{48}{197}$  & $\displaystyle \dfrac{21}{197}$* &$1-\displaystyle \dfrac{58}{197}$ \\
Housekeeping &$\displaystyle \dfrac{12}{17}$  &  $\displaystyle \dfrac{2}{17}$  & $\displaystyle \dfrac{5}{17}$* &$1-\displaystyle \dfrac{12}{17}$\\
\bottomrule
\end{NiceTabular}
\caption{Probabilities of hand hygiene given direct patient contact}
\label{tab:handwash_given_pat}
\end{table}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

0

For me, $\displaystyle \dfrac{\mathstrut 58}{\mathstrut 197}$ works well in cases in which I do only have a small number of elements. For more information about \mathstrut and friends, see here.

enter image description here

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{tabular}{|l|}
    \hline
    $\dfrac{100}{30}$\\
    \hline
    $\dfrac{\mathstrut 100}{30}$\\
    \hline
    $\dfrac{\mathstrut 100}{\mathstrut 30}$\\
    \hline     
\end{tabular}

\end{document}

You must log in to answer this question.

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