13

In a basic tabular environment it seems that the optional parameter to \\ sometime has no effect. For instance, why is there no additional vertical spacing following the highlighted lines even though each line ends with a \\[2.0ex]?

enter image description here

Notes:

  • I am aware of solutions that apply to all lines in the table:

    • \renewcommand{\arraystretch}{1.5}
    • \extrarowheight=3pt\relax (With the array package)

but am specifically looking for a solution where I can control the spacing on a line by line basis as per the MWE.

References

Code:

\documentclass{article}
%% --------------------- These two for \rowcolor (to reproduce image)
%\usepackage{colortbl}
%\usepackage{xcolor}

\usepackage{array}% For \extrarowheight

\newcommand{\ShortText}{Short text.}% Fits on one line \newcommand{\LongText}{Long text that takes up more than one line.}%

\begin{document} \noindent %\renewcommand{\arraystretch}{1.5}% This works, but applies to ALL rows %\extrarowheight=3pt\relax% This works with the array package, but applies to ALL rows \begin{tabular}{l p{0.35\linewidth} p{0.45\linewidth}} Num & Column 1 & Column 2 \[2.0ex] 1 & \ShortText & \ShortText \[2.0ex] 2 & \ShortText & \LongText \[2.0ex] %\rowcolor{red!25}% <-- Needs "colortbl" and "xcolor" 3 & \LongText & \ShortText \[2.0ex]% <--- Why this optional parameter ignored 4 & \LongText & \LongText \[2.0ex] 5 & \ShortText & \ShortText \[2.0ex] \end{tabular} \end{document}

Peter Grill
  • 223,288

3 Answers3

13

\\ inside a table is defined via \@tabularcr. If there is an optional argument with positive length, the macro takes the default depth of the line, adds the length and sets an invisible rule with the larger depth.

However, the problematic lines have already a much larger depth because of top-aligned two line cells. As a result, the optional argument remains ineffective unless its length is increased to become larger than the (unknown) depth of the two line cells.

Macro \addlinespace of package booktabs uses a different method by inserting the space after the row.

Example:

 \documentclass{article}
%% --------------------- These two for \rowcolor (to reproduce image)
\usepackage[table]{xcolor}

\usepackage{array}% For \extrarowheight \usepackage{booktabs}% For \addlinespace, \defauladdspace

\newcommand{\ShortText}{Short text.}% Fits on one line \newcommand{\LongText}{Long text that takes up more than one line.}%

\begin{document} \noindent \setlength{\defaultaddspace}{2ex}% \begin{tabular}{l p{0.35\linewidth} p{0.45\linewidth}} Num & Column 1 & Column 2 \\addlinespace 1 & \ShortText & \ShortText \\addlinespace 2 & \ShortText & \LongText \\addlinespace \rowcolor{red!25}% <-- Needs xcolor with option table 3 & \LongText & \ShortText \\addlinespace 4 & \LongText & \LongText \\addlinespace 5 & \ShortText & \ShortText \ \end{tabular} \end{document}

Result

The reason for the different implementation is that the method of LaTeX's \@tabularcr supports vertical rules. Macro \addlinespace on the other side comes from package booktabs for tables without vertical rules.

Heiko Oberdiek
  • 271,626
3

Adding a spacing with the optional parameter works fine for one-line rows. If there are more lines in a row cell, you have to change the value.

A better solution consists in using the \cellspacetop-bottomlimit lengths from package cellspace, which defines a minimal vertical spacing at the top and bottom of cells in columns with specifier prefixed with the letter S (or C if you load siunitx). By definition, this spacing is added only if necessary.

Unrelated : if you load xcolor with option [table], you don't have to load yourself colortbl.

\documentclass{article}

\usepackage{cellspace}
\setlength{\cellspacebottomlimit}{1ex}
\setlength{\cellspacetoplimit}{1ex}
\usepackage{array}% For \extrarowheight

\newcommand{\ShortText}{Short text.}% Fits on one line
\newcommand{\LongText}{Long text that takes up more than one line.}%

\begin{document}
\noindent
\begin{tabular}{l S{p{0.35\linewidth}} S{p{0.45\linewidth}}}
    Num & Column 1 & Column 2 \\
     1 & \ShortText & \ShortText \\
     2 & \ShortText & \LongText \\
     3 & \LongText & \ShortText \\ % <--- Why this optional parameter ignored
     4 & \LongText & \LongText \\
     5 & \ShortText & \ShortText
\end{tabular}
\vskip1cm
\begin{tabular}{l p{0.35\linewidth} p{0.45\linewidth}}
    Num & Column 1 & Column 2 \\[2.0ex]
     1 & \ShortText & \ShortText \\[2.0ex]
     2 & \ShortText & \LongText \\[4.5ex]
     3 & \LongText & \ShortText \\[4.5ex]% <--- Why this optional parameter ignored
     4 & \LongText & \LongText \\[2.0ex]
     5 & \ShortText & \ShortText \\[2.0ex]
\end{tabular}
\end{document} 

enter image description here

Bernard
  • 271,350
1

Life is much easier if you are using tblr environment from the new LaTeX3 package tabularray: \\[dimen] always adds vertical space to the row.

\documentclass{article}

\usepackage{colortbl} \usepackage{xcolor}

\usepackage{array}

\usepackage{tabularray} \SetTblrInner{rowsep=0pt}

\newcommand{\ShortText}{Short text.}% Fits on one line \newcommand{\LongText}{Long text that takes up more than one line.}%

\begin{document}

\noindent \section{Tabular}

\begin{tabular}{l p{0.35\linewidth} p{0.45\linewidth}} \hline Num & Column 1 & Column 2 \[2.0ex] 1 & \ShortText & \ShortText \[2.0ex] 2 & \ShortText & \LongText \[2.0ex] \rowcolor{red!25} 3 & \LongText & \ShortText \[2.0ex]% <--- this optional parameter is ignored 4 & \LongText & \LongText \[2.0ex] 5 & \ShortText & \ShortText \[2.0ex] \hline \end{tabular}

\section{Tabularray}

\noindent \begin{tblr}{l p{0.35\linewidth} p{0.45\linewidth}} \hline Num & Column 1 & Column 2 \[2.0ex] 1 & \ShortText & \ShortText \[2.0ex] 2 & \ShortText & \LongText \[2.0ex] \SetRow{red8} 3 & \LongText & \ShortText \[2.0ex]% <--- this optional parameter is not ignored 4 & \LongText & \LongText \[2.0ex] 5 & \ShortText & \ShortText \[2.0ex] \hline \end{tblr}

\end{document}

enter image description here

L.J.R.
  • 10,932
  • Nice! Thanks for making me of this new pacakge! I am assuming that \SetTblrInner is a recent addition as that was not in my TeXLive2021 (but not recently updated). – Peter Grill Aug 27 '21 at 09:27
  • In old version, it was \SetTblrDefault command. – L.J.R. Aug 27 '21 at 09:33