gpt4 book ai didi

格式化 Sweave 表,两个挑战

转载 作者:行者123 更新时间:2023-12-01 01:23:44 25 4
gpt4 key购买 nike

我正在努力应对 Sweave 中的两个格式挑战。一是表格中文字的斜体,二是如何在表格底部添加一列求和行。有关详细信息,请参阅以下 MWE。

\documentclass[12pt,letterpaper,oneside]{amsart}
\usepackage{graphicx}
\usepackage{longtable}
\SweaveOpts{keep.source=TRUE} % Keeps comments in the R code.
%
\begin{document}
% \maketitle
\section*{The Challenge}
Italicize the taxa in the following table and add a row at the bottom which gives the sum of the sites column.

<<echo=TRUE>>=
# Creating Data
taxa <- c("Arabidopsis thaliana",
"Populus trichocarpa",
"Brachypodium distachyon")
sites <- c(270,320,240)
data.df <- data.frame(taxa,sites)
@
~
<<label=tab1,echo=TRUE,results=tex>>=
# Creating Table
library(xtable)
data.table <- xtable(data.df,
caption="Sweave Output")
print(data.table,
caption.placement="top")
@

The results should look something like table 2, which was hand coded in \LaTeX.
\begin{table}[ht]
\caption{Manually coded \LaTeX}
\begin{tabular}{l l r}
\hline
& taxa & sites \\
\hline
1 & \emph{Arabidopsis thaliana} & 270 \\
2 & \emph{Populus trichocarpa} & 320 \\
3 & \emph{Brachypodium distachyon} & 240 \\
\hline
& Total Sites & 830 \\
\hline
\end{tabular}
\end{table}
\end{document}

最佳答案

添加 \emph手动使用 paste ,并添加新行 rbind ,将新行的名称设置为一个空格。另外,使用 stringsAsFactors=FALSE在原始数据框创建中,使向分类群列添加新值成为可能。

然后使用 sanitize.text.function=identityxtable保留 \emph 中的反斜杠命令,和 hline.after得到你想要的线条。

\documentclass[12pt,letterpaper,oneside]{amsart}
\usepackage{graphicx}
\usepackage{longtable}
\SweaveOpts{keep.source=TRUE} % Keeps formatting of the R code.
\begin{document}
<<echo=TRUE>>=
taxa <- c("Arabidopsis thaliana",
"Populus trichocarpa",
"Brachypodium distachyon")
sites <- c(270,320,240)
data.df <- data.frame(taxa,sites, stringsAsFactors=FALSE)
@
~
<<label=tab2, echo=TRUE, results=tex>>=
library(xtable)
data.df$taxa <- paste("\\emph{",taxa,"}", sep="")
data.df <- rbind(data.df, ` `=c("Total Sites", sum(data.df$sites)))
data.table <- xtable(data.df,
caption="Sweave Output")
print(data.table,
caption.placement="top",
sanitize.text.function=identity,
hline.after=c(-1,0,nrow(data.df)-1, nrow(data.df)))
@
\end{document}

关于格式化 Sweave 表,两个挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com