[Tex/LaTex] How to change row height within xtable environment

formattingheightknitrxtable

I would like to change row height such that all rows except the header row will have fix height x and the header row will have eight y. I would like to do this within xtable environment because I need to generate this table inside a knitr code chunk. I want to retain all other formating used in the code example (each second row gray and the last column in italics).

\documentclass[a5paper]{article}
\usepackage{array}

\begin{document}
 <<do_table2, results = "asis">>=
    library(xtable)
    mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))
    rws <- seq(1, (nrow(head(iris))-1), by = 2)
    col <- rep("\\rowcolor[gray]{0.95}", length(rws))

    iris$Species <- paste0("\\textit{", iris$Species, "}")
    rep("\\[5pt]", nrow(head(iris)))

    print(xtable(head(iris)),
       add.to.row = list(pos = as.list(rws), command = col),
       sanitize.text.function = identity)

    @
\end{document}

Note: Firstly I tried to change row height at least somehow. I wanted to use add.to.row with command argument rep("\\[20pt]", length(rws)), but this placed the command in front of the line and therefore did not work.

\[20pt]6 & 5.40 & 3.90 & 1.70 & 0.40 & \textit{\textit{\textit{\textit{setosa}}

So I tried to put it in the end like this: iris$Species <- paste0("\\textit{", iris$Species, "}\\[20pt]") or iris$Species <- paste0("\\textit{", iris$Species, "}\\[20pt]\\"), but I also did not succeed.

Edit:
From the latex point of view I need to change line height inside a line such that the command will be in the very beginning of the line like this, but this does not worked me:

\begin{table}[ht]
\centering
\begin{tabular}{rrrrrl}
  \hline
 & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ 
  \hline
1 & 5.10 & 3.50 & 1.40 & 0.20 & setosa \\ 
   \\[20pt]2 & 4.90 & 3.00 & 1.40 & 0.20 & setosa \\ 
  3 & 4.70 & 3.20 & 1.30 & 0.20 & setosa \\ 
   \\[20pt]4 & 4.60 & 3.10 & 1.50 & 0.20 & setosa \\ 
  5 & 5.00 & 3.60 & 1.40 & 0.20 & setosa \\ 
   \\[20pt]6 & 5.40 & 3.90 & 1.70 & 0.40 & setosa \\ 
   \hline
\end{tabular}
\end{table}

Best Answer

For LaTeX, the fact that the \\ is at the start or at the end of the (text) line means nothing. Your problem is that you have blank lines (repeated \\ and then \\[20pt] following...

Try to generate something like that:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrl}
    \hline
    & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ 
    \hline
    \\[20pt]1 & 5.10 & 3.50 & 1.40 & 0.20 & setosa  
    \\[20pt]2 & 4.90 & 3.00 & 1.40 & 0.20 & setosa  
    \\[20pt]3 & 4.70 & 3.20 & 1.30 & 0.20 & setosa  
    \\[20pt]4 & 4.60 & 3.10 & 1.50 & 0.20 & setosa  
    \\[20pt]5 & 5.00 & 3.60 & 1.40 & 0.20 & setosa  
    \\[20pt]6 & 5.40 & 3.90 & 1.70 & 0.40 & setosa  
    \\[20pt]
    \hline
\end{tabular}
\end{table}    
\end{document}

(BTW: this is a correct MWE. Copy, paste and compile).

ugly table here

Although to maintain constant height, I would rather use a \strut or an invisible rule at the start of the line. For example:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}
\begin{table}[ht]
\centering
\newcommand{\splat}{\vrule width 0 pt height 15pt depth 5pt\relax}
\begin{tabular}{rrrrrl}
    \hline
    \splat & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ 
    \hline
    \splat 1 & 5.10 & 3.50 & 1.40 & 0.20 & setosa  \\
    \splat 2 & 4.90 & 3.00 & 1.40 & 0.20 & setosa  \\
    \splat 3 & 4.70 & 3.20 & 1.30 & 0.20 & setosa  \\
    \splat 4 & 4.60 & 3.10 & 1.50 & 0.20 & setosa  \\
    \splat 5 & 5.00 & 3.60 & 1.40 & 0.20 & setosa  \\
    \splat 6 & 5.40 & 3.90 & 1.70 & 0.40 & setosa  \\
    \hline
\end{tabular}
\end{table}    
\end{document}

And you can control the minimum size of a row by playing with the depth and height parameters in the definition of \splat.

Better looking table

And, if you really do not want to access the preamble of your LaTeX (but this is really limiting, so try to learn how to) and your xtable thing loads the appropriate packages (as it seems, otherwise \rowcolor would not be available) you can simply expand your macros manually:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrl}
    \hline
    \vrule width 0pt height 15pt depth 5pt\relax & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\ 
    \hline
    \vrule width 0pt height 15pt depth 5pt\relax 1 & 5.10 & 3.50 & 1.40 & 0.20 & setosa  \\
    \rowcolor{gray}{0.8}\vrule width 0pt height 15pt depth 5pt\relax 2 & 4.90 & 3.00 & 1.40 & 0.20 & setosa  \\
    \vrule width 0pt height 15pt depth 5pt\relax 3 & 4.70 & 3.20 & 1.30 & 0.20 & setosa  \\
    \rowcolor{gray}{0.8}\vrule width 0pt height 15pt depth 5pt\relax 4 & 4.60 & 3.10 & 1.50 & 0.20 & setosa  \\
    \vrule width 0pt height 15pt depth 5pt\relax 5 & 5.00 & 3.60 & 1.40 & 0.20 & setosa  \\
    \rowcolor{gray}{0.8}\vrule width 0pt height 15pt depth 5pt\relax 6 & 5.40 & 3.90 & 1.70 & 0.40 & setosa  \\
    \hline
\end{tabular}
\end{table}    
\end{document}

to obtain:

zebra table

Related Question