簡體   English   中英

生成的表格中的垂直線/邊框不連續(R+Latex+Sweave+xtable)

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

我正在使用 R 和 Sweave 在乳膠中創建報告。 該表是使用 xtable 生成的。 以下是用於生成表格的代碼

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

這些值生成良好,但分隔列(以及邊框)的垂直線不連續。

顏色表

我試圖刪除顏色,它看起來更好,但垂直線/邊框之間仍然有間隙

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

桌子

關於如何解決這個問題的任何想法

編輯:按要求提供更完整的示例

\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}

結果是一樣的。

運行 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}

這里的關鍵是booktabs不能很好地與垂直線配合。 您可以從表格中的垂直規則中切換出來,或者采用此處此處TeX SE上描述的解決方案之一。 根據對這些問題的評論和回答,共識建議似乎是完全避免垂直規則。

我無法復制這個問題,但表格邊框是連續的,並且將包選項保持在最低限度。 使用 knitr 進行編譯:

xtable_img

該問題可能與下面已注釋掉的包multicol有關:

乳膠代碼:

\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