简体   繁体   English

生成的表格中的垂直线/边框不连续(R+Latex+Sweave+xtable)

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

I am creating a report in latex using R and Sweave.我正在使用 R 和 Sweave 在乳胶中创建报告。 The table is generated using xtable.该表是使用 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运行 Sweave 后还添加生成的 tex 文件

\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.这里的关键是booktabs不能很好地与垂直线配合。 You can either switch away from vertical rules in your tables or adopt one of the solutions described here or here on TeX SE .您可以从表格中的垂直规则中切换出来,或者采用此处此处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:使用 knitr 进行编译:

xtable_img

The issue probably is related to package multicol which has been commented out below:该问题可能与下面已注释掉的包multicol有关:

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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