3

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}
2
  • 1
    The LaTeX command should have two backslashes. \\[20pt] --- could just be a problem of escaping? I do not know which languages is that, but in python I need "\\\\[20pt]" (notice the four backslash). Anyway, if you post a MWE in LaTeX we could help better. – Rmano Apr 5 '16 at 17:57
  • OK, here is the knitr MWE. The content of the code chunk is R. – kamila Apr 6 '16 at 6:25
3

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

3
  • Thank you very much. I managed to generate your code in knitr and it worked. I do not how to use \strut inside knitr/xtable. With a table generated manually inside LaTeX this would be better. Would you know how to combine your code with this one \\rowcolor[gray]{0.8} for some (each second) of my rows? BTW. My code in the question is a MWE but for knitr no pure LaTeX – kamila Apr 6 '16 at 18:27
  • A \strut is similar to the invisible rule I defined in \splat, but with the font height and depth. To color every other row, you can let LaTeX do it: tex.stackexchange.com/questions/5363/… – Rmano Apr 6 '16 at 21:42
  • Thanks, but would it be possible to do this inside lines? I do not know how to put a command to an extra line into xtable... I do not want to modify the LaTeX code... – kamila Apr 7 '16 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.