【问题标题】:Align xtable caption left in knitr在knitr中对齐xtable标题
【发布时间】:2018-06-29 10:18:07
【问题描述】:

我已经阅读了一堆关于左对齐 xtable 表格的不同帖子,但我找不到详细信息/找出如何使标题左对齐。下面是一个可重复的示例,它证明了表格左侧但标题居中。

\documentclass{article}
\begin{document}

<<echo = F, results = "asis">>=
df = data.frame(x = c(1,2), y = c(4,6))
library(xtable)
print(xtable(df,digits=0, caption="Caption Left?"), include.colnames=TRUE, size = "small", comment=FALSE,   latex.environments="flushleft")
@
\end{document}

【问题讨论】:

  • 你看过align参数吗?
  • 我可能理解不正确,但我认为 align 参数是对齐表格中内容的位置。
  • 啊,对不起。看来我需要更多那种众所周知的咖啡。请参阅print.xtable 了解一些可能有助于您定位标题的参数。

标签: r latex knitr xtable


【解决方案1】:

如果您不介意使用替代包(我自己的):

library(huxtable)
ht <- as_huxtable(df)
caption_pos(ht) <- "bottomleft"
print_latex(ht)    # or just do `ht` if within a knitr or Rmd document

【讨论】:

    【解决方案2】:

    此操作对于希望以 APA 格式生成 Markdown 文件的任何人来说都是关键,因为表格标题通常是左对齐的。

    我将稍微澄清一下 Robin 的答案,因为对于 Markdown/Latex 界面来说是全新的,我花了一些时间才弄清楚。

    通过包含

    \usepackage{caption}
    

    文档的 YAML(标题)部分中的命令,前面为

    header-includes:
    

    因此,文档标题中的整个 YAML 部分可能如下所示:

    ---
    title: "Supplementary Materials"
    author: ""
    date: "3/30/2018"
    output:
      pdf_document: default
    editor_options:
      chunk_output_type: inline
    header-includes:
       - \usepackage{caption}
    
    ---
    

    然后,在文档中的任何位置,在代码块之前(在它自己的空白处),您可以插入代码:

    \captionsetup{justification = raggedright, singlelinecheck = false}
    

    要再次更改设置,您可以在 Markdown 文件的空白处(而不是代码块!)中的任何位置重新插入此代码。

    例如

    \captionsetup{justification = centering, singlelinecheck = false}
    

    【讨论】:

      【解决方案3】:

      我已经找到了如何做到这一点。只需导入 LaTex Caption 包并使用标题设置参数:

      \captionsetup{justification = raggedright, singlelinecheck = false}
      

      这将使左边的标题对齐。通过在附加表格/图形之前重复具有以下修改的功能,可以将标题返回到附加表格或图形的默认居中位置。

      \captionsetup{justification = centering, singlelinecheck = false}
      

      答案是:

      \documentclass{article}
      \usepackage{caption}
      \begin{document}
      \captionsetup{justification = raggedright, singlelinecheck = false}
      <<echo = F, results = "asis">>=
      df = data.frame(x = c(1,2), y = c(4,6))
      library(xtable)
      print(xtable(df,digits=0, caption="Caption Left?"),include.colnames=TRUE, size = "small", comment=FALSE,   latex.environments="flushleft")
      @
      \end{document}
      

      返回:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-04
        • 1970-01-01
        相关资源
        最近更新 更多