[Tex/LaTex] Automatically setting table row height

tables

When creating a table, Latex sometimes doesn't fit the row height well, making the content intersect with the borders as in the following:

enter image description here

Notice how the first row is too short while the second looks good.

This is generated by the code:

\begin{tabular} {   | c | c | }
    \hline
    $\left(\frac{1}{2},\frac{1}{2}\right)$   & $(1,1)$   \\ \hline      
    $(1,1)$      &  $\left(\frac{1}{1+c},\frac{1}{1+c}\right)$    \\  \hline        
\end{tabular}

Any ideas as to how to make Latex allocate enough space for the rows without explicit commands?

Perhaps the problem is not with the table but rather the parenthesis, so a solution to that (make the parenthesis around the contents of the first cell larger) would help as well.

Best Answer

You have three main solutions:

  1. Use the cellspace package, which defines a minimal vertical spacing between the contents of a cell and the above or below cell (\cellspacetoplimit and \cellspacebottomlimit). You have to prefix the column specifier with the letter S, or C if you use siunitx.
  2. Use the makecell package and the pair of commands \setcellgapes{some length} (in preamble) and \setcellgapes (for specific tables)
  3. Use the booktabs package and its toprule,\(c)midrule and \bottomrule commands, which add some vertical (adjustable) spacing around them. No vertical rules in this case.

Here is a demo with your specific code. See the details in the documentation of the packages:

\documentclass{article}

\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\usepackage{makecell}
\setcellgapes{4pt}

\usepackage{booktabs}

\begin{document}
\begin{tabular} { | c | c | }
  \hline
  $\left(\frac{1}{2},\frac{1}{2}\right)$ & $(1,1)$ \\ \hline
  $(1,1)$ & $\left(\frac{1}{1+c},\frac{1}{1+c}\right)$ \\ \hline
\end{tabular}
\qquad
\begin{tabular} { | Sc | Sc | }
  \hline
  $\left(\frac{1}{2},\frac{1}{2}\right)$ & $(1,1)$ \\
  \hline
  $(1,1)$ & $\left(\frac{1}{1+c},\frac{1}{1+c}\right)$ \\
  \hline
\end{tabular}
\qquad
{\makegapedcells\begin{tabular} { | c | c | }
  \hline
  $\left(\frac{1}{2},\frac{1}{2}\right)$ & $(1,1)$ \\ \hline
  $(1,1)$ & $\left(\frac{1}{1+c},\frac{1}{1+c}\right)$ \\ \hline
  \end{tabular}}
\vskip1cm
\begin{tabular} {cc}
  \toprule
  $\left(\frac{1}{2},\frac{1}{2}\right)$ & $(1,1)$ \\
  \midrule
  $(1,1)$ & $\left(\frac{1}{1+c},\frac{1}{1+c}\right)$ \\
  \bottomrule
\end{tabular}

\end{document} 

enter image description here