我是 LATEX/R/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}
<<echo=FALSE, results=tex>>=
# Data
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))
#Table 1
createdXTable <- xtable(tabulatedVal, digits = 2)
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")
# Table 2
createdXTable <- xtable(tabulatedVal, digits = 2)
rws <- seq(1, nrow(tabulatedVal), by=2)
print(createdXTable, booktabs=TRUE, add.to.row=list(pos=as.list(rws), command=col), include.rownames=FALSE, size="\\small")
@
\end{document}
生成的.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 20:29:43 2014
\begin{table}[ht]
\centering
{\small
\begin{tabular}{lr}
\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 20:29:43 2014
\begin{table}[ht]
\centering
{\small
\begin{tabular}{lr}
\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}
\end{document}
任何帮助都值得感激。谢谢
答案1
问题是你有一个额外的
\rowcolor{blue!10} \bottomrule
在表格的底部;这会在某种程度上影响接下来的内容。
实际上,您在 rep 命令中编写了太多的\\rowcolor{blue!10}
;它会将其添加到行尾,但会影响下一行。要修复,只需rws
像这样少写一个。
rws <- seq(1, nrow(tabulatedVal)-1, by=2)
此外,为了便于将来参考,仅包含重现问题所需的软件包会很有帮助。这也可以帮助您找出问题所在(尽管这里没有)。在这种情况下,一个较小的示例应该是
\documentclass{article}
\usepackage{colortbl}
\begin{document}
<<echo=FALSE, results=tex>>=
library(xtable)
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)
rws <- seq(1, nrow(tabulatedVal), by=2)
col <- rep("\\rowcolor{cyan}", length(rws))
print(createdXTable, add.to.row=list(pos=as.list(rws), command=col), include.rownames=FALSE)
print(createdXTable, add.to.row=list(pos=as.list(rws), command=col), include.rownames=FALSE)
@
\end{document}
答案2
问题是由于第一个表格底部的 \rowcolor{blue!10}\bottomrule 引起的。作为一种解决方法,您可以手动删除最后一个 \rowcolor{} 命令,但恐怕我不知道如何自动完成。我本来想将此作为评论发布,但没有回复。
\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}
\begin{table}[ht]
\centering
{\small
\begin{tabular}{lr}
\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 \\
\bottomrule % This line was changed
\end{tabular}
}
\end{table}
\begin{table}[ht]
\centering
{\small
\begin{tabular}{lr}
\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 \\
\bottomrule % This line changed too for good measure
\end{tabular}
}
\end{table}
\end{document}