简体   繁体   中英

Vertical lines/borders in generated table is not continuous (R+Latex+Sweave+xtable)

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.

颜色表

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")
@

桌子

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}

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.

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM