gpt4 book ai didi

r - 在循环中的 xtable 图形之间添加节标题

转载 作者:行者123 更新时间:2023-12-02 04:13:48 25 4
gpt4 key购买 nike

我正在使用 knitr 生成 PDF 文章。我想打印一系列表格,其间带有节标题。我正在 R 代码块中执行此操作。但不幸的是,发生的情况是第一个标题打印出来,然后是一个数字,然后其余的标题适合该页面,其余的表格在后面,而不是根据需要散布在标题中。

screenshot of the pdf output

在此页面之后,还有一系列 5 个表格,位于各自的页面上。

这是我正在使用的代码:

dfList <- list(alc_top, alc_bottom, cpg_home_top, cpg_home_bottom, electronics_top, electronics_bottom)
labels <- c("Premium Liquor Brand - Top Performers", "Premium Liquor Brand- Bottom Performers", "CPG Home - Top Performers", "CPG Home - Bottom Performers", "Electronics - Top Performers", "CPG Home - Bottom Performers")

for (i in 1:length(dfList)) {
df <- dfList[[i]]
product = "test"
cat(paste("\\section{",labels[i],"}", sep=""))
print(xtable(df,size="\\tiny"))
}

我尝试在循环内添加一个新行 cat("\\newpage") 。这会为每个标签添加一个新页面,但所有图表都再次位于新部分之后。

我认为我需要为表格指定一个定位值(H或h或类似LaTex中的值),但我不太确定如何使用xtable和knitr来做到这一点。

最佳答案

这里的问题不在于元素写入 TEX 文件的顺序。 PDF 中的“错误顺序”是由于表格被包装在 float 环境中,因此源文件中的 TEX 代码位置不一定对应于 PDF 中表格的位置。

以下是使 table 保持在固定位置的三个选项。每一种都有其优点和缺点:

选项 1:不要使用 float

print.xtable 有一个floating 参数(默认为TRUE)。将此参数设置为 FALSE 会导致表格未包装在 float 环境中(默认值:table)。

  • 优点:简单有效。
  • 缺点:非 float 没有编号,没有标题,也没有标签。如果 floating = FALSEprint.xtable 会忽略 xtable 上的 captionlabel 参数.

选项 2:位置“H”

print.xtable 有一个 table.placement 参数,可用于将自定义 float 放置说明符传递到 float 环境。说明符 H “将 float 精确地放置在 LaTeX 代码中的位置”(来源: Wikibooks )。请注意,这需要 \usepackage{float}

  • 优点:保留标题、编号和标签。
  • 缺点:需要额外的软件包(几乎不相关)。

选项 3:\FloatBarrier

LaTeX 包 placeins 提供了一个 \FloatBarrier 命令,该命令强制打印到目前为止尚未显示的所有 float 。

  • 优点和缺点:如选项 2。
  • 此外,由于需要在每个表之后插入 \FloatBarrier 命令,它会使代码有点困惑 - 除非(至少在这个问题的特定情况下)以下功能是使用:

The package even provides an option to change the definition of \section to automatically include a \FloatBarrier. This can be set by loading the package with the option [section] (\usepackage[section]{placeins}). [source: Wikibooks]

演示

\documentclass{article}
\usepackage{float}
\usepackage{placeins}
\begin{document}

<<results = "asis", echo = FALSE>>=
library(xtable)

# This table floats.
print(
xtable(head(cars),
caption = "Floating",
label = "tab:floating"), table.placement = "b"
)

# This table won't float but caption and label are ignored.
print(
xtable(head(cars),
caption = "Not floating",
label = "tab:not-floating"),
floating = FALSE)

# Placement "H". (requires "float" package)
print(
xtable(head(cars),
caption = "Non-floating float",
label = "tab:not-actually-floating"),
table.placement = "H")

cat("Text before the barrier. (text 1)")
# Floats won't float beyond this barrier (requires "placeins" package)
cat("\\FloatBarrier\n")
cat("Text after the barrier. (text 2)")
@

Add \texttt{table.placement = "b"} to the first table to see that it will be located at the bottom of page 1 (after `text 1') and `text 2` will come \emph{after} it (on page 2), althogh there would be plenty of space on page 1. This is because the float cannot `pass' the barrier.

\end{document}

关于r - 在循环中的 xtable 图形之间添加节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35003802/

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