在指定大小之后,使用xtable和knitr时需要额外的花括号 [英] Extra curly braces when using xtable and knitr, after specifiying size

查看:84
本文介绍了在指定大小之后,使用xtable和knitr时需要额外的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用knitr创建R Markdown文档,并在使用xtable创建表时遇到麻烦.我的表很大,我正在尝试使用print语句中的size命令减小大小.我遇到的问题是该命令似乎添加了两个额外的花括号,这些花括号显示在PDF中,一个在表格前,一个在表格后.

I'm creating a R Markdown document using knitr and am running into trouble using xtable to create a table. My table is very large and I'm trying to reduce the size using the size command in the print statement. The issue I'm running into is that the command seems to add two extra curly braces which show up in the PDF, one before the table and one after.

有人知道解决此问题的方法吗?

Does anyone know a way to fix this?

MWE:

---
output:
  pdf_document:
    keep_tex: yes
tables: true
---

```{r, results='asis', echo=FALSE}

library(xtable)

my.df <- data.frame(matrix(c(1:18),nrow=2))

glossaryprint <- xtable(my.df, caption="Summary of Terms")

print(glossaryprint,
      comment=FALSE,
      floating=FALSE,
      size="footnotesize"
)

```

推荐答案

注意:该问题的主题和此答案已在

Note: The issue that is subject of the question and this answer has been resolved in xtable 1.8-2.

尽管该问题已通过解决方法得到了自我解答,但我认为对于其他用户而言,更多详细信息可能会有所帮助.

Although the question has been self-answered with a workaround, I think for other users some more details might be helpful.

要了解这里发生的情况,我们需要仔细查看文档从RMD到PDF的转换步骤.这些步骤是:

To understand what is happening here, we need to take a close look at the conversion steps the document undergoes on its way from RMD to PDF. The steps are:

RMD --> MD --> TEX --> PDF

让我们以相反的顺序查看文件:

Let's look at the files in reversed order:

  • PDF :(由pdflatexTEX生成)
  • PDF: (generated from TEX by pdflatex)

  • TEX :(由MDpandoc生成)

% …
\{\footnotesize

\begin{tabular}{rrrr}
  \hline
 & X1 & X2 & X3 \\ 
  \hline
1 &   1 &   3 &   5 \\ 
  2 &   2 &   4 &   6 \\ 
   \hline
\end{tabular}

\}
 % …

  • MD(由knitrRMD生成)

  • MD (generated from RMD by knitr)

    ---
    output:
      pdf_document:
        keep_tex: yes
    ---
    
    {\footnotesize
    \begin{tabular}{rrrr}
      \hline
     & X1 & X2 & X3 \\ 
      \hline
    1 &   1 &   3 &   5 \\ 
      2 &   2 &   4 &   6 \\ 
       \hline
    \end{tabular}
    }
    

  • RMD :(源文件)

  • RMD: (source file)

    ---
    output:
      pdf_document:
        keep_tex: yes
    ---
    
    ```{r, results='asis', echo=FALSE}
    
    library(xtable)
    
    mytable <- xtable(data.frame(matrix(c(1:6), nrow = 2)))
    
    print(mytable,
          comment = FALSE,
          floating = FALSE,
          size = "footnotesize"
    )
    ```
    

  • 回想:问题是,在PDF中有可见的花括号.它们来自哪里?

    Recall: The problem is, that there are visible curly braces in the PDF. Where do they come from?

    • 它们是TEX文件(\{\})中转义的花括号的结果.
    • 这些花括号也存在于MD文件中,但是没有被转义.
    • They are the result of the escaped curly braces in the TEX file (\{ and \}).
    • These curly braces also exist in the MD file, but there they are not escaped.

    所以我们现在知道两件事:我们看到花括号,因为它们被转义了,并且被pandoc转义了.

    So we know two things by now: We see the curly braces because they are escaped and they are escaped by pandoc.

    但是为什么这些花括号完全存在?当指定size时,print.xtable输出它们.目标是创建一个组,并仅在该组中应用size. (对于floating = TRUE,不需要使用花括号进行分组,因为在其中设置了sizefigure环境中.无论如何都将花括号打印出来.)

    But why do these curly braces exist at all? print.xtable outputs them when a size is specified. The goal is to create a group and to apply size only within that group. (With floating = TRUE, no grouping by curly braces is required because there is a figure environment whithin which the size is set. The curly braces are printed anyways.)

    为什么pandoc逃避那对花括号,却不保留所有其他花括号(例如,在\begin{tabular}中)?这是因为pandoc应该转义按原义输入的特殊字符,但保留原始LaTeX逃脱.但是,pandoc 不知道外花括号是LaTeX命令而不是文本.

    And why does pandoc escape that pair of curly braces but leaves all the other curly braces (e.g. in \begin{tabular}) unescaped? This is because pandoc is supposed to escape special characters that are meant literally but leave raw LaTeX unescaped. However, pandoc does not know that the outer curly braces are LaTeX commands and not text.

    (使用floating = TRUE时不会发生此问题,因为花括号位于被识别为LaTeX的figure环境中.)

    (With floating = TRUE the problem does not occur because the curly braces are within a figure environment which is recognized as LaTeX.)

    了解了问题之后,我们该怎么办?一种解决方案已经由OP发布 :放弃在print.xtable中撒小size并插入footnotesize手动命令:

    After having understood the problem, what can we do about it? One solution has already been posted by the OP: Abstain from spefifying size in print.xtable and insert the footnotesize command manually:

        ---
        output:
          pdf_document:
            keep_tex: yes
        ---
    
        ```{r, results='asis', echo=FALSE}
    
        library(xtable)
    
        mytable <- xtable(data.frame(matrix(c(1:6), nrow = 2)))
    
        cat("\\begin{footnotesize}")
    
        print(mytable,
              comment = FALSE,
              floating = FALSE
        )
    
        cat("\\end{footnotesize}")
        ```
    

    但是,从长远来看,如果xtable(当前版本:1.8-0)生成的LaTeX代码在pandoc转换后仍然有效,那就太好了.这很简单:print.xtable检查是否设置了size,如果已设置,则在大小说明之前插入{,并在表的末尾插入}:

    However, on the long run it would be nice if xtable (current version: 1.8-0) generated LaTeX code that survives the pandoc conversion. This is quite simple: print.xtable checks if size is set and if so, inserts { before the size specification and } at the end of the table:

    if (is.null(size) || !is.character(size)) {
      BSIZE <- ""
      ESIZE <- ""
    }
    else {
      if (length(grep("^\\\\", size)) == 0) {
        size <- paste("\\", size, sep = "")
      }
      BSIZE <- paste("{", size, "\n", sep = "")
      ESIZE <- "}\n"
    }
    

    此小修改将{替换为\begingroup,将}替换为\endgroup:

    This small modification replaces { with \begingroup and } with \endgroup:

    if (is.null(size) || !is.character(size)) {
      BSIZE <- ""
      ESIZE <- ""
    }
    else {
      if (length(grep("^\\\\", size)) == 0) {
        size <- paste("\\", size, sep = "")
      }
      BSIZE <- paste("\\begingroup", size, "\n", sep = "")
      ESIZE <- "\\endgroup\n"
    }
    

    对于LaTeX,这没有什么区别,但是随着pandoc识别\begingroup(与{相对应),它应该可以解决问题.我报告了此xtable中的一个错误,希望该问题将在以后的版本中解决.

    For LaTeX, this makes no difference, but as pandoc recognizes \begingroup (as oppsed to {) it should solve the problem. I reported this as a bug in xtable and hopefully the issue will be fixed in future versions.

    这篇关于在指定大小之后,使用xtable和knitr时需要额外的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆