3

WHen I needed to increase vertical space in a matrix I found this great custom command:

\makeatletter
\renewcommand*\env@matrix[1][\arraystretch]{%
  \edef\arraystretch{#1}%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols c}}
\makeatother

which allows me to make matrices like \begin{bmatrix}[2.5] ... \end{bmatrix} that have increased vertical space.

I'm not too familiar with creating custom commands for latex. Could someone help me edit the above command to allow me to make tables with this increased spacing option?

bdeonovic
  • 1,342
  • It is the \arraystretch{} that can increase the vertical space. You can use that for tabular environments, too (e.g., \renewcommand\arraystretch{1.2}). But beware, it will continue to stay in force, unless you you reset it with \renewcommand\arraystretch{1}. If you load the array package, there is also \setlength\extrarowheight{length}. – Steven B. Segletes Feb 21 '14 at 15:38
  • When I use the above macro for matrices, does it change the vertical spacing of all matrices, or just the one I am working with? – bdeonovic Feb 21 '14 at 15:42
  • If you want your comment to come explicitly to my attention (rather than me stumbling across it) then you must include @Steven B. Segletes in your comment. The renewed value of \arraystretch will change the setting from the point of invocation going forward in your document, unless later reset. All tabular , array and other similar environments will be affected. – Steven B. Segletes Feb 21 '14 at 15:45
  • You are using LaTeX, and you've changed a command (via \renewcommand) that takes an optional argument (formerly it didn't). If I use your code is a minimal example with a bmatrix (from amsmath, then I get a matrix that has increased spacing. So, I guess, I don't understand the question really. – Werner Feb 21 '14 at 16:07
  • @Werner I would like to make a similar macro, but for the tabular environment instead of matrix environment – bdeonovic Feb 21 '14 at 16:26
  • @Benjamin: The problem with that request is that tabular already takes an optional argument. The complete interface is \begin{tabular}[<pos>]{<col spec>}. What are you anticipating will be your use case? For example, do you want \begin{tabular}[<arraystretch>]{<col spec>} and completely forego the regular [<pos>] positioning argument (that is optional)? – Werner Feb 21 '14 at 16:31
  • @Werner I'd like to have a simple way to increasing vertical space in tables – bdeonovic Feb 21 '14 at 16:40

1 Answers1

3

"A simple way to increase vertical spacing in tables" is to replace the optional positioning parameter with an \arraystretch one:

enter image description here

\documentclass{article}
\let\oldtabular\tabular% Store a copy of \tabular
\let\endoldtabular\endtabular% Store a copy of \endtabular
\renewenvironment{tabular}[2][\arraystretch]
  {\edef\arraystretch{#1}% Update \arraystretch
   \oldtabular{#2}}% \begin{tabular}[<stretch>]{<col spec>}
  {\endoldtabular}% \end{tabular}

\begin{document}
\begin{tabular}{*{6}{c}}
  a & b & c & d & e & f \\
  g & h & i & j & k & l \\
  m & n & o & p & q & r \\
  s & t & u & v & w & x \\
  y & z
\end{tabular}

\begin{tabular}[2.5]{*{6}{c}}
  a & b & c & d & e & f \\
  g & h & i & j & k & l \\
  m & n & o & p & q & r \\
  s & t & u & v & w & x \\
  y & z
\end{tabular}

\end{document}

The new interface is \begin{tabular}[<stretch>]{<col spec>}...\end{tabular} where <stretch> defaults to \arraystretch. You could make that 1 also, but the above suggestion is more general.

It would be possible to extend this so it allows you to use both optional arguments, if needed. But two optional arguments only work as expected if you supply both when you need only the second one. In those instances, a key-value approach is usually better. However, that would be very similar to just adding the new \arraystretch as needed, so I don't think it's always simpler.

Werner
  • 603,163