87

I want to tabulate formulas, but I find that the lines of the table cells are too close together, and this is aesthetically unsatisfactory. Example (formulae copied from wikipedia):

\begin{tabular}{|c|c|}
\hline
Cylindrical & $\displaystyle{{1 \over \rho}{\partial \over \partial\rho}\left(\rho {\partial f \over \partial \rho}\right)
+ {1 \over \rho^2}{\partial^2 f \over \partial \phi^2}  + {\partial^2 f \over \partial z^2}}$\\\hline
Spherical & $\displaystyle{{1 \over r^2}{\partial \over \partial r}\!\left(r^2 {\partial f \over \partial r}\right)
\!+\!{1 \over r^2\!\sin\theta}{\partial \over \partial \theta}\!\left(\sin\theta {\partial f \over \partial \theta}\right)
\!+\!{1 \over r^2\!\sin^2\theta}{\partial^2 f \over \partial \phi^2}}$\\\hline
\end{tabular}

gives this

table output

and you see that the top of the formulas are chopped by the lines. Suggestions?

2
  • See Column padding in tables on ways of adjusting both vertical and horizontal padding of cell entries in tabular or array.
    – Werner
    Apr 1, 2012 at 14:18
  • @Werner Since this is an answer, you should pose it as such. Jul 4, 2014 at 15:17

5 Answers 5

108

I have solved this in the past by modifying the value of \arraystretch

you can do this by adding to your source :

{\renewcommand{\arraystretch}{1.2} %<- modify value to suit your needs
\begin{tabular}{|c|c|}
...
\end{tabular}
}

Edit:

Actually having researched this a bit more it seems that having equations in the cells has unexpected behaviour, where the space is increase disproportionally at the top (value set to 3 to start getting the space at the bottom):

tabular with arraystretch set to 3

more recently I have started to use the tabu package to replace all tables (from tabular to tabularx to longtable). It also provide a few more controls. in this case the \tabulinesep has a much more appropirate effect on the results:

\documentclass[preview]{standalone}
   \usepackage{tabu}
   \begin{document}
   {\tabulinesep=1.2mm
   \begin{tabu} {|c|c|}
       \hline
       Cylindrical & $\displaystyle{{1 \over \rho}{\partial \over \partial\rho}\left(\rho {\partial f \over \partial \rho}\right)
       + {1 \over \rho^2}{\partial^2 f \over \partial \phi^2}  + {\partial^2 f \over \partial z^2}}$\\\hline
       Spherical & $\displaystyle{{1 \over r^2}{\partial \over \partial r}\!\left(r^2 {\partial f \over \partial r}\right)
       \!+\!{1 \over r^2\!\sin\theta}{\partial \over \partial \theta}\!\left(\sin\theta {\partial f \over \partial \theta}\right)
       \!+\!{1 \over r^2\!\sin^2\theta}{\partial^2 f \over \partial \phi^2}}$\\\hline
   \end{tabu}}
\end{document}

which produces the following result:

tabu with tabulinesep set to 1.2mm

5
  • 3
    Good advice! tabu is a really good package!
    – Alberto
    Feb 26, 2013 at 21:09
  • 1
    tabu is no longer actively maintained. Oct 10, 2021 at 8:51
  • @LorenzoDonati--Codidact.com correct, and hasn't been for some time. However, this answer is now over 9 years old. There have been other answers since which you can use now based on the newer and maintained nicematrix and tabularray packages. I was going to add these solutions to my answer, but since they are already there, I didn't see the point.
    – ArTourter
    Oct 11, 2021 at 18:01
  • 1
    Not arguing about that. I just stumbled on your answer after a google search and felt compelled to add that notice. A couple years ago I updated my TeXLive installation and, to my horror, all my heavily-tabu-based documents went awry. I think it's a service for the community to flag that because someone could find the solution interesting but could have an old installation and could invest time learning tabu and then discover the snag when it's too late. Oct 11, 2021 at 19:39
  • {\renewcommand{\arraystretch}{1.2}... works with longtable for me.
    – frmbelz
    Mar 20 at 18:30
13

You may use a different table format, leaving out the vertical lines which are by no means necessary; you can also greatly simplify the input with a personal command for partial derivatives:

\documentclass{article}

\usepackage{booktabs,amsmath}

\newcommand{\dpder}[3][]{\dfrac{\partial^{#1}#2}{\partial #3}}

\begin{document}
\begin{tabular}{lc}
\toprule
Cartesian &
$\dpder[2]{f}{x^2}+\dpder[2]{f}{y^2}+\dpder[2]{f}{z^2}$
\\
\midrule
Cylindrical &
$\dfrac{1}{\rho} \dpder{}{\rho}{\left(\rho \dpder{f}{\rho}\right)}
  + \dfrac{1}{\rho^2} \dpder[2]{f}{\phi^2} + \dpder[2]{f}{z^2}$
\\
\midrule
Spherical &
$\dfrac{1}{r^2}\dpder{}{r}{\left(r^2 \dpder{f}{r}\right)}
  +\dfrac{1}{r^2\sin\theta}\dpder{}{\theta}{\left(\sin\theta \dpder{f}{\theta}\right)}
  +\dfrac{1}{r^2\sin^2\theta}\dpder[2]{f}{\phi^2}$
\\
\bottomrule
\end{tabular}
\end{document}

Notice how to avoid unwanted spaces in the "cylindrical" and "spherical" rows by using {\left(...\right)} (a couple of braces is sufficient instead of explicit backing up). Note also the usage of \dfrac in the definition of \dpder that avoids specifying \displaystyle (which is however not a command taking arguments, but a declaration).

With booktabs there's rarely the need to adjust the row spacing.

enter image description here

5
  • Thanks. I usually use macros but in this case I compiled a list of useful formulas for my students, and just copy-pasted it from wikipedia. Can you explain what the second (empty) square braces do in the definition of \dpder?
    – yohbs
    Apr 1, 2012 at 20:22
  • 3
    @yohbs With \newcommand{\dpder}[3][]{...} we say that the command has one optional argument and two mandatory ones; the default of the optional argument is empty (the exponent for the \partial in the numerator.
    – egreg
    Apr 1, 2012 at 20:43
  • I didn't knew about the spacingissue. Really nice, egreg! :)` Nov 30, 2013 at 15:36
  • Can booktabs be used together with tabularx in order to have the X column type?
    – Andyc
    Dec 10, 2020 at 19:52
  • @Andyc yes, certainly
    – egreg
    Dec 10, 2020 at 20:04
5

As an alternative to the outdated tabu package, the new tabularray package provides rowsep option for tables:

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\begin{tblr}{|c|c|}
\hline
Cylindrical & $\displaystyle{{1 \over \rho}{\partial \over \partial\rho}\left(\rho {\partial f \over \partial \rho}\right)
+ {1 \over \rho^2}{\partial^2 f \over \partial \phi^2}  + {\partial^2 f \over \partial z^2}}$\\\hline
Spherical & $\displaystyle{{1 \over r^2}{\partial \over \partial r}\!\left(r^2 {\partial f \over \partial r}\right)
\!+\!{1 \over r^2\!\sin\theta}{\partial \over \partial \theta}\!\left(\sin\theta {\partial f \over \partial \theta}\right)
\!+\!{1 \over r^2\!\sin^2\theta}{\partial^2 f \over \partial \phi^2}}$\\\hline
\end{tblr}

\bigskip

\SetTblrInner{rowsep=4pt}

\begin{tblr}{|c|c|}
\hline
Cylindrical & $\displaystyle{{1 \over \rho}{\partial \over \partial\rho}\left(\rho {\partial f \over \partial \rho}\right)
+ {1 \over \rho^2}{\partial^2 f \over \partial \phi^2}  + {\partial^2 f \over \partial z^2}}$\\\hline
Spherical & $\displaystyle{{1 \over r^2}{\partial \over \partial r}\!\left(r^2 {\partial f \over \partial r}\right)
\!+\!{1 \over r^2\!\sin\theta}{\partial \over \partial \theta}\!\left(\sin\theta {\partial f \over \partial \theta}\right)
\!+\!{1 \over r^2\!\sin^2\theta}{\partial^2 f \over \partial \phi^2}}$\\\hline
\end{tblr}

\end{document}

enter image description here

2

I ended up here after googling "lyx extra space in tables for dfrac", so this is an answer for LyX, but it probably also applies to plain LaTeX code.

I use the arraystretch-command in my preamble, but if I use dfrac in a cell the vertical space will not reflect the needed hight increase.

\renewcommand*\arraystretch{1.3}

The "tabu" package seems like a good solution, but the package is not recommended by several people because the author didn't tried to maintain backward compability hard enough (as stated by jon here, but I don't know if this is true though). Also I had a hard time getting the "tabu" package to work in LyX.

Anyway,

  • I found a solution to expand the vertical space in a table cell both upwards and downwards vertically by simply insering an empty box-control before the cell content.

Example code for a single 3-cell row:

\hline
Logarithms & $\ln\left(x\right)$ & %
\begin{minipage}[c][10mm][t]{0.1mm}%
%
\end{minipage}$\dfrac{1}{x}$\tabularnewline

Resulting PDF without box: enter image description here

Resulting PDF with box: 3cell example PDF output

I used the following settings in LyX for the box: Box settings in LyX

I hope this tip can help someone to expand their vertical cell white spaces in LaTeX!

1

With {NiceTabular} of nicematrix, you can use \arraystretch and \extrarowheight of array but you have also a key cell-space-limits.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}{cc}[hvlines,cell-space-limits=3pt]
Cylindrical & $\displaystyle{{1 \over \rho}{\partial \over \partial\rho}\left(\rho {\partial f \over \partial \rho}\right)
+ {1 \over \rho^2}{\partial^2 f \over \partial \phi^2}  + {\partial^2 f \over \partial z^2}}$\\
Spherical & $\displaystyle{{1 \over r^2}{\partial \over \partial r}\!\left(r^2 {\partial f \over \partial r}\right)
\!+\!{1 \over r^2\!\sin\theta}{\partial \over \partial \theta}\!\left(\sin\theta {\partial f \over \partial \theta}\right)
\!+\!{1 \over r^2\!\sin^2\theta}{\partial^2 f \over \partial \phi^2}}$\\
\end{NiceTabular}
\end{document}

Output of the above code

You must log in to answer this question.

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