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

Mic*_*nza 2 latex r xtable knitr

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

pdf 输出的屏幕截图

此页之后还有一系列 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"))
}
Run Code Online (Sandbox Code Playgroud)

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

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

CL.*_*CL. 5

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

\n

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

\n

选项 1:不使用浮动

\n

print.xtable有一个floating参数(默认为TRUE)。将此参数设置为FALSE会产生未包装在浮动环境中的表(默认值:table)。

\n
    \n
  • 优点:简单有效。
  • \n
  • 缺点:非浮点数没有编号,没有标题,也没有标签。print.xtable忽略if上的caption和参数。labelxtablefloating = FALSE
  • \n
\n

选项 2:位置“H”

\n

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

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

选项 3:\\FloatBarrier

\n

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

\n
    \n
  • 优点和缺点:如选项 2。
  • \n
  • 此外,它使代码有点混乱,因为\\FloatBarrier需要在每个表 \xe2\x80\x93 之后插入命令,除非(至少在这个问题的特定情况下)使用以下功能:
  • \n
\n
\n

该包甚至提供了一个选项来更改 的定义\\section以自动包含\\FloatBarrier. 这可以通过使用选项加载包来设置[section]\\usepackage[section]{placeins})。[来源:维基教科书]

\n
\n

演示

\n
\\documentclass{article}\n\\usepackage{float}\n\\usepackage{placeins}\n\\begin{document}\n\n<<results = "asis", echo = FALSE>>=\nlibrary(xtable)\n\n# This table floats.\nprint(\n  xtable(head(cars),\n         caption = "Floating",\n         label = "tab:floating"), table.placement = "b"\n  )\n\n# This table won\'t float but caption and label are ignored.\nprint(\n  xtable(head(cars),\n         caption = "Not floating",\n         label = "tab:not-floating"),\n  floating = FALSE)\n\n# Placement "H". (requires "float" package)\nprint(\n  xtable(head(cars),\n         caption = "Non-floating float",\n         label = "tab:not-actually-floating"),\n  table.placement = "H")\n\ncat("Text before the barrier. (text 1)")\n# Floats won\'t float beyond this barrier (requires "placeins" package)\ncat("\\\\FloatBarrier\\n")\ncat("Text after the barrier. (text 2)")\n@\n\nAdd \\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.\n\n\\end{document}\n
Run Code Online (Sandbox Code Playgroud)\n