0

I am creating a report in latex using R and Sweave. The table is generated using xtable. Following is the code used to generate the table

<<echo=FALSE, results=tex>>=
tabulatedVal <- getStatTableMacro(portfolio, normalizingRow)
createdXTable <- xtable(tabulatedVal, digits = 2)
align(createdXTable) <- "l|l|c|"
rws <- seq(1, nrow(tabulatedVal), by=2)
col <- rep("\\rowcolor{blue!10}", length(rws))
print(createdXTable, booktabs=TRUE, add.to.row=list(pos=as.list(rws), command=col),     include.rownames=FALSE, size="\\small")
@

The values are generated fine but the vertical lines separating columns (as well as the borders) are not continuous.

color table

I tried to remove the colors and it seems better but still has gaps between the vertical lines/borders

<<echo=FALSE, results=tex>>=
tabulatedVal <- getStatTableMacro(portfolio, normalizingRow)
createdXTable <- xtable(tabulatedVal, digits = 2)
align(createdXTable) <- "l|l|c|"
rws <- seq(1, nrow(tabulatedVal), by=2)
print(createdXTable, booktabs=TRUE, include.rownames=FALSE, size="\\small")
@

table

Any idea as to how to fix this

Edit: Providing a more complete example as asked

\documentclass[xcolor=table,professionalfonts,a4paper,11pt]{article}
\usepackage[margin=1.0in]{geometry}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage[skip=12pt]{caption}
\usepackage{colortbl, xcolor}
\usepackage{helvet}
\usepackage[noae]{Sweave}

\begin{document}

<<echo=FALSE, results=tex>>=

#Colored Table

tabulatedVal <- data.frame(Statistics =c("Annualized Return (%)", "Standard Deviation (%)", "Skewness", "Kurtosis",
                             "Sharpe Ratio", "Maximum Drawdown", "Percent of losing months",
                              "Worst Monthly Return", "Worst Yearly Return"),
                           Value =c(0.09, 6.77, 0.11, 3.18, 1.34, 5.20, 31.84, -4.09, -0.33))
createdXTable <- xtable(tabulatedVal, digits = 2)
align(createdXTable) <- "l|l|c|"
rws <- seq(1, nrow(tabulatedVal), by=2)
col <- rep("\\rowcolor{blue!10}", length(rws))
print(createdXTable, booktabs=TRUE, add.to.row=list(pos=as.list(rws), command=col), include.rownames=FALSE, size="\\small")


#Uncolored Table

createdXTable <- xtable(tabulatedVal, digits = 2)
align(createdXTable) <- "l|l|c|"
rws <- seq(1, nrow(tabulatedVal), by=2)
print(createdXTable, booktabs=TRUE, include.rownames=FALSE, size="\\small")
@


\end{document}

The results are the same.

Also adding the generated tex file after running Sweave

\documentclass[xcolor=table,professionalfonts,a4paper,11pt]{article}
\usepackage[margin=1.0in]{geometry}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage[skip=12pt]{caption}
\usepackage{colortbl, xcolor}
\usepackage{helvet}
\usepackage[noae]{Sweave}

\begin{document}

% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Mon Apr 14 17:43:00 2014
\begin{table}[ht]
\centering
{\small
\begin{tabular}{|l|c|}
  \toprule
Statistics & Value \\ 
  \midrule
Annualized Return (\%) & 0.09 \\ 
   \rowcolor{blue!10}Standard Deviation (\%) & 6.77 \\ 
  Skewness & 0.11 \\ 
   \rowcolor{blue!10}Kurtosis & 3.18 \\ 
  Sharpe Ratio & 1.34 \\ 
   \rowcolor{blue!10}Maximum Drawdown & 5.20 \\ 
  Percent of losing months & 31.84 \\ 
   \rowcolor{blue!10}Worst Monthly Return & -4.09 \\ 
  Worst Yearly Return & -0.33 \\ 
   \rowcolor{blue!10} \bottomrule
\end{tabular}
}
\end{table}% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Mon Apr 14 17:43:00 2014
\begin{table}[ht]
\centering
{\small
\begin{tabular}{|l|c|}
  \toprule
Statistics & Value \\ 
  \midrule
Annualized Return (\%) & 0.09 \\ 
  Standard Deviation (\%) & 6.77 \\ 
  Skewness & 0.11 \\ 
  Kurtosis & 3.18 \\ 
  Sharpe Ratio & 1.34 \\ 
  Maximum Drawdown & 5.20 \\ 
  Percent of losing months & 31.84 \\ 
  Worst Monthly Return & -4.09 \\ 
  Worst Yearly Return & -0.33 \\ 
   \bottomrule
\end{tabular}
}
\end{table}

\end{document}
Carl
  • 4,232
  • 2
  • 12
  • 24
john_zac
  • 1,241
  • 2
  • 11
  • 16
  • Can you provide [a minimal example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including some sample data that reproduces this? It's hard to work with this when we don't have the data or a full Sweave/knitr example document that shows what LaTeX packages you have. – Thomas Apr 14 '14 at 11:59
  • @Thomas Added a complete example as asked – john_zac Apr 14 '14 at 12:19
  • 1
    This is a `booktabs` problem. See [here](http://tex.stackexchange.com/questions/18092/vertical-lines-are-being-broken-up-disrupted-by-the-horizontal-booktabs-lines?rq=1) and [here](http://tex.stackexchange.com/questions/88929/vertical-table-lines-are-discontinuous-with-booktabs?rq=1). – Thomas Apr 14 '14 at 14:24
  • Thanks, thats what I was looking for. Based on the links it is recommended not to use vertical rules. I guess considering how simple the table is that's the way to go. If that didn't work out I will go for array and bigstrut packages. Why don't you elaborate and move the comment to answer section. I will accept it as answer. For now, I have added +1 to the comment – john_zac Apr 14 '14 at 14:47

2 Answers2

3

The key here is that booktabs does not cooperate well with vertical lines. You can either switch away from vertical rules in your tables or adopt one of the solutions described here or here on TeX SE. Per the comments and answers on those questions, the consensus advice seems to be to avoid vertical rules entirely.

Community
  • 1
  • 1
Thomas
  • 43,637
  • 12
  • 109
  • 140
-2

I cannot replicate the problem, but the table borders are continuous and have kept the package options to the minimum. Using knitr for compilation:

xtable_img

The issue probably is related to package multicol which has been commented out below:

LaTex Code:

\documentclass{article}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{booktabs}
%\usepackage{multicol}
%\usepackage[skip=12pt]{caption}
\usepackage{colortbl, xcolor}
%\usepackage{helvet}
%\usepackage[noae]{Sweave}

\begin{document}

% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Mon Apr 14 17:43:00 2014
\begin{table}
\centering
{
\begin{tabular}{|l|c|}
  \toprule
Statistics & Value \\ 
  \midrule
Annualized Return (\%) & 0.09 \\ 
   \rowcolor{blue!10}Standard Deviation (\%) & 6.77 \\ 
  Skewness & 0.11 \\ 
   \rowcolor{blue!10}Kurtosis & 3.18 \\ 
  Sharpe Ratio & 1.34 \\ 
   \rowcolor{blue!10}Maximum Drawdown & 5.20 \\ 
  Percent of losing months & 31.84 \\ 
   \rowcolor{blue!10}Worst Monthly Return & -4.09 \\ 
  Worst Yearly Return & -0.33 \\ 
   \rowcolor{blue!10} \bottomrule
\end{tabular}


}
\end{table}

% \begin{table}
% \centering
% {
% \begin{tabular}{|l|c|}
  % \toprule
% Statistics & Value \\ 
  % \midrule
% Annualized Return (\%) & 0.09 \\ 
  % Standard Deviation (\%) & 6.77 \\ 
  % Skewness & 0.11 \\ 
  % Kurtosis & 3.18 \\ 
  % Sharpe Ratio & 1.34 \\ 
  % Maximum Drawdown & 5.20 \\ 
  % Percent of losing months & 31.84 \\ 
  % Worst Monthly Return & -4.09 \\ 
  % Worst Yearly Return & -0.33 \\ 
   % \bottomrule
% \end{tabular}
% }
% \end{table}

\end{document}
Silence Dogood
  • 3,587
  • 1
  • 13
  • 17
  • 2
    I don't get it. In the affixed image, there seems to be discontinuity between between the vertical lines that separate the statistics and value columns. It is alternating in each row. Also the outside borders are not touching each other. I wanted to fix that – john_zac Apr 14 '14 at 13:26