5

I would like to add some space between the rows in a table, specifically to prevent the brackets in my matrices to touch, cf. this MWE:

\documentclass{article}

\newcommand*{\mymatrix}[1]{ \ensuremath{% \left[\begin{tabular}{@{}l@{}}#1\end{tabular}\right]% }% }

\begin{document}

\begin{tabular}{*3{l}} foo & bar & baz\ foo & \mymatrix{top\bottom} & baz\ foo & \mymatrix{top\bottom} & baz\ foo & bar & baz\ \end{tabular}

\end{document}

enter image description here

There are many questions on TeX.SE asking how to add space between rows in a table, and it seems to me that the answers always suggest increasing the value of \arraystretch, see e.g. this question among many.

But as far as I can tell, this doesn't actually add space between rows, it simply stretches the rows vertically. If something fills the vertical space of the rows, as my matrices do, the content of these rows still touches, cf. below:

\documentclass{article}

\newcommand*{\mymatrix}[1]{ \ensuremath{% \left[\begin{tabular}{@{}l@{}}#1\end{tabular}\right]% }% }

\renewcommand{\arraystretch}{2}

\begin{document}

\begin{tabular}{*3{l}} foo & bar & baz\ foo & \mymatrix{top\bottom} & baz\ foo & \mymatrix{top\bottom} & baz\ foo & bar & baz\ \end{tabular}

\end{document}

enter image description here

So my question is, is there a way to truly add space between rows in a table (other than inserting empty rows, which adds too much space)?

Sverre
  • 20,729

4 Answers4

10

Here's a solution that employs \addlinespace of the booktabs package.

enter image description here

\addlinespace takes an optional argument, which indicates how much vertical whitespace should be inserted. If no argument is specified, the parameter \defaultaddspace is applied, which is set to 0.5em by the package.

\documentclass{article}
\newcommand*{\mymatrix}[1]{%
  \ensuremath{\left[\begin{tabular}{@{}l@{}} #1 \end{tabular}\right]}}
\usepackage{booktabs} % for "\addlinespace" macro

\begin{document}

\begin{tabular}{*{3}{l}} foo & bar & baz\ foo & \mymatrix{top\bottom} & baz\ \addlinespace foo & \mymatrix{top\bottom} & baz\ \addlinespace foo & bar & baz \end{tabular} \end{document}

Mico
  • 506,678
6

If you use {NiceTabular} of nicematrix, you have two options cell-space-top-limit and cell-space-bottom-limit (the names are inspired by the parameters \cellspacetoplimit and cellspacebottomlimit of cellspace) that you can set once for all and they will apply to all the environments {NiceTabular}.

\documentclass{article}
\usepackage{nicematrix}
\NiceMatrixOptions{cell-space-top-limit=1pt,cell-space-bottom-limit=1pt}

\newcommand*{\mymatrix}[1]{ \ensuremath{% \left[\begin{tabular}{@{}l@{}}#1\end{tabular}\right]% }% }

\begin{document}

\begin{NiceTabular}{*3{l}} foo & bar & baz\ foo & \mymatrix{top\bottom} & baz\ foo & \mymatrix{top\bottom} & baz\ foo & bar & baz\ \end{NiceTabular}

\end{document}

Ouput of the above code

F. Pantigny
  • 40,250
  • Does this package also allow adding this extra space for selected tables? In my case, I don't need to add extra white space in tables that don't have matrices. – Sverre Aug 22 '20 at 11:34
  • 1
    With the keys cell-space-top-limit and cell-space-bottom-limit, nicematrix won't add space in all the {NiceTabular}. It will only add space in the cells which have a height or a depth greater than the standard height and depth of a row in {tabular}. That is to say, \begin{tabular}{cc}A&B\\ C&D\end{tabular} will have the same output as \begin{NiceTabular}{cc}A&B\\C&D\end{NiceTabular}. However, in {NiceTabular}, you can also add vertical space with the tools avaiable in {tabular}. You can write \\[1mm} or use \addlinespace of booktabs. – F. Pantigny Aug 22 '20 at 13:05
5

Does this solve the problem

No extra packages

enter image description here

\documentclass{article}

\newcommand*{\mymatrix}[1]{ \ensuremath{% \left[\begin{tabular}{@{}l@{}}#1\end{tabular}\right]% }% }

\begin{document}

\begin{tabular}{*3{l}} foo & bar & baz\ foo & \mymatrix{top\bottom} & baz\[8pt] foo & \mymatrix{top\bottom} & baz\[8pt] foo & bar & baz\ \end{tabular}

\end{document}

**edit adding a strut predefined also works **

same result

enter image description here

\def\mystrut{\rule{0pt}{2\normalbaselineskip}}
\begin{tabular}{*3{l}}
    foo & bar & baz\\
    foo & \mymatrix{top\\bottom}\mystrut & baz\\[8pt]
    foo & \mymatrix{top\\bottom}\mystrut & baz\\[8pt]
    foo & bar & baz\\
\end{tabular}
js bibra
  • 21,280
4

It is easy to obtain what you want automatically with cellspace, which defines minimal vertical spacings at the top and bottom of cells in columns with specifier prefixed with the letter S, or C if you load siunitx, or even any letter you please through the column= loading time option:

\documentclass{article}
\usepackage[column=O]{cellspace}
\setlength{\cellspacetoplimit}{3pt}
\setlength{\cellspacebottomlimit}{3pt}

\newcommand*{\mymatrix}[1]{ \ensuremath{% \left[\begin{tabular}{@{}l@{}}#1\end{tabular}\right]% }% }

\begin{document}

\begin{tabular}{*3{Ol}} foo & bar & baz\ foo & \mymatrix{top\bottom} & baz\ foo & \mymatrix{top\bottom} & baz\ foo & bar & baz \end{tabular}

\end{document}

enter image description here

projetmbc
  • 13,315
Bernard
  • 271,350
  • @projetmbc: Thank you for making the code readable. As often, I had forgotten to check… – Bernard Aug 21 '20 at 16:48
  • Does this package also allow adding this extra space for selected tables? In my case, I don't need to add extra white space in tables that don't have matrices. – Sverre Aug 22 '20 at 11:35
  • 1
    @Sverre: It doesn't adds extra space systematically – it adds extra space if the actual spacing is less than the minimal values, so as to obtain these minimal values. That's the main difference with the \makegapedcells command from makecell or the extrarowheight length defined by array – Bernard Aug 22 '20 at 13:57