我在 R 脚本中生成表格的独立 pdf 时遇到了一些问题。我使用 huxtable R 包从常规 R 矩阵或数据框创建“latex”表。下面是我的脚本的一个非常简化的版本,它重现了“+4pt”问题:
library(dplyr)
library(Hmisc)
library(huxtable)
age <- c(62, 60, 63, 59, 63, 67, 71, 64, 65, 66)
L2 <- factor(rep(paste(LETTERS[3:6], LETTERS[3:6], sep=""), c(1,3,2,4)))
L1 <- factor(rep(LETTERS[1:2], c(4,6)))
df <- data.frame(Grp=L1, SubGrp=L2, Age=age, stringsAsFactors=F)
Stats <- df %>%
group_by(Grp) %>% mutate(NB_Grp = n()) %>%
group_by(SubGrp, add = TRUE) %>% mutate(NB_SubGrp = n()) %>%
select(Grp, NB_Grp, SubGrp, NB_SubGrp, Age)
hux_tab <- as_hux(Stats, add_rownames=F, add_colnames=T)
rowspan(hux_tab)[2, 1:2] <- 4
rowspan(hux_tab)[6, 1:2] <- 6
rowspan(hux_tab)[3, 3:4] <- 3
rowspan(hux_tab)[6, 3:4] <- 2
rowspan(hux_tab)[8, 3:4] <- 4
number_format(hux_tab)[, 5] <- 0
file_name = "Tab"
tex_file <- paste(file_name,".tex",sep="")
sink(file=tex_file, append=F)
cat('\\documentclass[preview, varwidth=true]{standalone}
\\usepackage[a2paper]{geometry}
\\usepackage{tabularx}
\\usepackage{multirow}
\\begin{document}
')
print_latex(hux_tab, tabular_only=F)
cat('\\end{document}
')
sink()
cmd <- 'pdflatex.exe -output-format=pdf ./Tab.tex'
invisible(sys(cmd))
通过该代码,我得到以下 tex 文件:
\documentclass[preview, varwidth=true]{standalone}
\usepackage[a2paper]{geometry}
\usepackage{tabularx}
\usepackage{multirow}
\begin{document}
\begin{table}[h]
\centering\begin{tabularx}{0.5\textwidth}{p{0.1\textwidth} p{0.1\textwidth} p{0.1\textwidth} p{0.1\textwidth} p{0.1\textwidth}}
\multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright Grp\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright NB\_Grp\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright SubGrp\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright NB\_SubGrp\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright Age\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright CC\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 1.00\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 62\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 60\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 63\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{\multirow{-4}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright A\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\multirow{-4}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 4.00\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\multirow{-3}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright DD\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\multirow{-3}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 3.00\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 59\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 63\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{\multirow{-2}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright EE\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\multirow{-2}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 2.00\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 67\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 71\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 64\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 65\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\multicolumn{1}{l}{\multirow{-6}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright B\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\multirow{-6}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 6.00\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\multirow{-4}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright FF\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\multirow{-4}{*}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 4.00\rule[-4pt]{0pt}{4pt}\hspace*{4pt}}} & \multicolumn{1}{l}{\hspace*{4pt}\rule{0pt}{\baselineskip+4pt}\raggedright 66\rule[-4pt]{0pt}{4pt}\hspace*{4pt}} \tabularnewline[-0.5pt]
\end{tabularx}
\end{table}
\end{document}
但结果并不如预期:
表格中每个值前面都有“+4pt”额外文本。这似乎是由于对“baselineskip”tex 命令处理不当造成的。
表格的代码部分由“huxtable”R 包的函数自动生成。
您是否看到我必须在 R 代码中进行哪些更改才能使表格中不再到处都出现“+4pt”?
谢谢
答案1
您没有使用calc
LaTeX 包。要查找 huxtable 所需的包列表,您可以report_latex_dependencies()
在 R 中使用,然后将其粘贴或打印到您的 TeX 文件中。