【问题标题】:Adding section headers in between xtable figures in a loop在循环中的 xtable 图形之间添加节标题
【发布时间】:2016-01-25 22:39:00
【问题描述】:

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

在这个页面之后,他们自己的页面上还有一系列 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 来执行此操作。

【问题讨论】:

  • 如果您不想要浮动表,请在print.xtable 中使用floating = FALSE。要使用 LaTeX 位置“H”,可以使用参数table.placement,但没有必要这样做。在其他(类似)情况下,placeins 包中的\FloatBarrier 可能会有所帮助。
  • 是的,浮动 false 选项有效,谢谢!

标签: r latex knitr xtable


【解决方案1】:

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

这里有三个选项可以将表格保持在固定位置。各有优缺点:

选项 1:不要使用浮点数

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

  • 专业版:简单有效。
  • Con:非浮动没有编号,没有标题和标签。如果floating = FALSE,print.xtable 将忽略xtable 上的caption 和label 参数。

选项 2:位置“H”

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

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

选项 3:\FloatBarrier

LaTeX 包placeins 提供了一个\FloatBarrier 命令,该命令强制打印所有未显示的浮点数。

  • 利弊:作为选项 2。
  • 此外,由于需要在每个表之后插入\FloatBarrier 命令,因此代码有点混乱 - 除非(至少在此问题的特定情况下)使用了以下功能:

该包甚至提供了一个选项来更改\section 的定义以自动包含\FloatBarrier。这可以通过使用选项[section] (\usepackage[section]{placeins}). [来源: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}

【讨论】:

    猜你喜欢
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多