【问题标题】:R: knitr: pander: aligning caption to tableR:knitr:pander:将标题与表格对齐
【发布时间】:2016-04-27 11:28:41
【问题描述】:

我正在尝试将标题与 Rmarkdown 文件中的 pandoc 表居中对齐(输出为 PDF)。表格以页面为中心,我希望标题同样居中。我似乎无法在 pander/pandoc.table 中找到一个选项,它可以让我在 Rstudio/Knitr 中编写的 Rmarkdown 中执行此操作。我目前唯一的解决方案是包含一串 ...nbsp;

我已经检查了 panderOptions,并且通过在 yaml 标头中插入代码,我还关注了 xtable caption alignment left aligned with table or centered (using knitr) 中的 cmets(没有成功)。而且,我已按照此处的建议添加了换行符:Issue with creating PDf file with Pander+Knitr: Error when putting table with caption and plot directly next to each other。

非常欢迎任何建议。

谢谢, 理查德

Rmarkdown 文档:

header-includes:
 - \usepackage[
    singlelinecheck=false,
    justification=centering
    ]{caption}
output:
    pdf_document 
---

```{r table2, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
library(pander)
panderOptions('keep.line.breaks', TRUE )
table2 <- "HHHH member | Description of future    
            H11111                | standard model    
            H22222                | low model      
            H33333                | decreasing projection"
df2 <- read.delim(textConnection(table2), header=FALSE, sep="|", strip.white=TRUE, stringsAsFactors=FALSE)
names(df2) <- unname(as.list(df2[1,]))
df2 <- df2[-1,] # remove first row
row.names(df2) <- NULL
pandoc.table(df2, 
             caption= "Table 2. Insert title here\n", style = "multiline", split.cells = c(20, 25))
```

系统: Linux Mint (17.1)/Ubuntu Trusty RStudio:0.98.1103 潘德:0.6.0 针织:1.12.3

【问题讨论】:

  • off-topic: header=T and check.names=F in read.delim 然后你可以省略接下来的 3 行代码。

标签: r knitr pander


【解决方案1】:

只需在顶部添加--- 即可解决我机器上的问题。

---
header-includes:
 - \usepackage[
    singlelinecheck=false,
    justification=centering
    ]{caption}
output:
    pdf_document 
---

```{r table2, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
library(pander)
panderOptions('keep.line.breaks', TRUE )
table2 <- "HHHH member | Description of future    
            H11111                | standard model    
            H22222                | low model      
            H33333                | decreasing projection"
df2 <- read.delim(textConnection(table2), header=FALSE, sep="|", strip.white=TRUE, stringsAsFactors=FALSE)
names(df2) <- unname(as.list(df2[1,]))
df2 <- df2[-1,] # remove first row
row.names(df2) <- NULL
pandoc.table(df2, 
             caption= "\\label{tab:MyLabel}Insert title here\n", style = "multiline", split.cells = c(20, 25))
```

In table&nbsp;\ref{tab:MyLabel} blabla

\autoref{tab:MyLabel}

编辑:添加标签并引用 LaTeX 样式的表格

【讨论】:

  • 感谢测试。看来我已经在 rmarkdown 文件的第一个表中包含了以下行: panderOptions('table.caption.prefix', '') ,并且这已经通过所有后续表传播,弄乱了标题。我需要弄清楚如何更好地手动标记表以避免,例如,表 2. 表 3. ... 对于给定的表。
  • @Richard 如果你更改table.caption.prefix,那么它可能会搞砸降价语法,请参阅相关man page 的警告。如果您想在表格中添加数字前缀,请在 caption 部分内执行此操作。或者在 GH 上打开一张票以添加另一个增加表数的选项,我会对此进行研究——尽管无法提供适用于所有文档格式的全局解决方案。
  • 让 LaTeX 做表格编号。
  • 我在表格中添加了一个内部链接示例。
  • 这很有帮助。连同在 header-includes 中插入 \captionsetup[table]{labelformat=empty} 让我可以灵活地修改文本中的表格编号和表格引用。干杯。
猜你喜欢
  • 2021-07-22
  • 2014-02-17
  • 2018-07-07
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多