# Specify vertical separation in arrays

Currently, I specify vertical separation with for example \def\arraystretch{1.4}, but I find that I need to adjust the argument to the environment depending on how high the rows are, which is tedious, and I can't adjust the height dynamically inside of the array. For example, the following array has rows with very different heights and would benefit from something that simulated a dynamic arraystretch:

$$\def\arraystretch{2.0} \begin{array}{rl} q = & \text{some expression with low height} \\ = & \text{some expression with the same low height} \\ = & \def\arraystretch{1.0} \begin{pmatrix} \text{two} \\ \text{matrices,} \\ \text{each} \\ \text{with} \\ \end{pmatrix} \\ = & \def\arraystretch{1.0} \begin{pmatrix} \text{relatively} \\ \text{large} \\ \text{height} \\ \end{pmatrix} \\ \end{array}$$ Even though the arraystretch has been set so high that the spacing between the first two rows has become too large, there is still no vertical spacing at all between the last two rows. This highly varying spacing between rows is undesirable.

For columns, there is \arraycolsep, which creates a uniform horizontal separation, but I haven't found any corresponding variable for vertical separation between rows (i.e., the distance between the bottom of one row and the top of the next row). The only way I know to achieve the latter is by choosing a small arraystretch and adding a newline by using \\ between the rows, but this creates a spacing that is too high.

So, is there some way to get a uniform, customizable vertical separation between rows in the array environment?

• Lest you first compile the table measuring the height and depth of each row, I don't see a way. Mar 16, 2021 at 22:57
• You can see the cellspace package with option [math]; iy defines minimal vertical spacings at the top and boyyom of cells in rows with specifier prefixed with the letter S by default, or C if you load siunitx or whatever letter you please with the option [column=…]. Mar 16, 2021 at 23:14
• Can you be more specific on what kind of tables you are trying to build? Maybe provide typical examples? Mar 16, 2021 at 23:29
• The reason I ask is that stackengine and tabstackengine can build arrays with either a fixed baselineskip or with a fixed gap between rows. But what it cannot do is give a separate eq number to each line of the stack. It is also limited in what it can do with lines between array elements. So, depending on the specifics of your need, the package may or may not offer some relief. Here is a quick overview answer: tex.stackexchange.com/questions/12726/stacking-two-hboxes/… Mar 16, 2021 at 23:41
• @StevenB.Segletes I've added an example, but for some reason the code I've added won't turn in to a math equation ... why? Do I use the wrong syntax for instantiating a LaTeX math expression? Mar 16, 2021 at 23:49

As long as you don't need separate rows numbered and various vertical and horizontal lines between rows and columns, the stackengine package provides "short" stacks that give equal gap between adjacent row data, account for the height of the row content. Here, I set the gap to 9pt, which can be adjusted.

The stacking gap will not affect the row spacing within the pmatrix, only the gap between adjacent rows of text and pmatrices.

\documentclass{article}
\usepackage{amsmath,tabstackengine}
\TABstackMath
\begin{document}
\setstackgap{S}{9pt}% ADJUST THE INTER-ROW GAP HERE
\alignShortstack{ q = & \text{some expression with low height} \\ = & \text{some expression with the same low height} \\ = & {\begin{pmatrix} \text{some} \\ \text{expression} \\ \text{with} \\ \text{great} \\ \text{height} \\ \end{pmatrix}}\\ = & {\begin{pmatrix} \text{relatively} \\ \text{large} \\ \text{height} \\ \end{pmatrix}}}
\end{document} SUPPLEMENT

To answer a comment from the OP, if one wants different alignments, don't use \alignShortstack, but \tabbedShortstack{...} (if all columns have the same alignment), or \tabularShortstack{<alignments>}{...} to specify particular alignments. And yes, the number of columns can be extended, as well as the intercolumn gap being specified.

\documentclass{article}
\usepackage{amsmath,tabstackengine}
\TABstackMath
\begin{document}
\setstackgap{S}{9pt}% ADJUST THE INTER-ROW GAP HERE
\setstacktabulargap{0pt}% INTER COLUMN GAP
$\tabularShortstack{rcr}{ q = & \text{some expression} & \text{with low height} \\ = & \text{some expression with} & \text{/the same low height} \\ = & {\begin{pmatrix} \text{some} \\ \text{expression} \\ \text{with} \\ \text{great} \\ \text{height} \\ \end{pmatrix}} & more\\ = & {\begin{pmatrix} \text{relatively} \\ \text{large} \\ \text{height} \\ \end{pmatrix}} & even~more}$
\end{document} • Can I specify how the content should be aligned within the various columns somehow? Now you have simply removed [rl], which is not what I want. And can I use more that two columns? This macro seems to solve the vertical separation problem, but instead introduces two new problems which are more severe. May 12, 2021 at 19:34
• @StrawberryFieldsForever The answer is yes, more columns can be added and different alignments specified. See the SUPPLEMENT portion of my updated answer. May 12, 2021 at 19:45
• Thanks, tabbedShortstack seems to work most of the times, but it fails if I try to nest an array environment or another tabbedShortstack inside of it. Do you know if this is supposed to work? May 12, 2021 at 20:35
• @StrawberryFieldsForever It works if (as in the above examples), you enclose the array in braces: for example, {\begin{array}{cc} a & b \\ aaa & b \\ a& bbb \\ \end{array}} works fine as an element of a \tabbedShortstack. May 12, 2021 at 22:14
• @StrawberryFieldsForever Still guessing at what you might mean? Maybe this? \documentclass{article} \usepackage{tabstackengine} \TABstackMath \TABbinary \setstacktabulargap{0pt} \begin{document} $\begin{array}{cc} A & B\\ CC & D \tabularShortunderstack{cll}{ = & EE &+ F\\ = & G &+HH} \end{array}$ \end{document}  Nov 6, 2021 at 13:03