指定大小后,使用 xtable 和 knitr 时的额外花括号 [英] Extra curly braces when using xtable and knitr, after specifiying size
问题描述
我正在使用 knitr
创建 R Markdown 文档,但在使用 xtable
创建表格时遇到了麻烦.我的表非常大,我正在尝试使用 print
语句中的 size
命令减小大小.我遇到的问题是该命令似乎添加了两个额外的大括号,它们显示在 PDF 中,一个在表格之前,一个在表格之后.
有人知道解决这个问题的方法吗?
MWE:
---输出:pdf_文档:keep_tex: 是的表:真---```{r,结果='asis',回声=假}库(xtable)my.df <- data.frame(矩阵(c(1:18),nrow=2))词汇表 <- xtable(my.df, caption="术语摘要")打印(词汇表打印,评论=假,浮动=假,大小="脚注大小")```
注意:问题的主题和此答案已在
TEX
:(由pandoc
从MD
生成)<代码>% …{脚注大小开始{表格}{rrrr}hline&X1&X2&X3 \hline1&1&3&5 \2&2&4&6 \hlineend{表格}}% …
MD
(由knitr
从RMD
生成)---输出:pdf_文档:keep_tex: 是的---{脚注大小开始{表格}{rrrr}hline&X1&X2&X3 \hline1&1&3&5 \2&2&4&6 \hlineend{表格}}
RMD
:(源文件)---输出:pdf_文档:keep_tex: 是的---```{r,结果='asis',回声=假}库(xtable)mytable <- xtable(data.frame(matrix(c(1:6), nrow = 2)))打印(我的表,评论 = 假,浮动=假,大小 = "脚注大小")```
回想一下:问题在于,PDF
中有可见的花括号.他们来自哪里?
- 它们是
TEX
文件({
和}
)中转义大括号的结果. - 这些花括号也存在于
MD
文件中,但没有转义.
所以我们现在知道两件事:我们看到花括号是因为它们被转义了,并且它们被 pandoc
转义了.
但是为什么这些花括号存在呢?print.xtable
在指定 size
时输出它们.目标是创建一个组并仅在该组内应用 size
.(使用 floating = TRUE
,不需要通过花括号进行分组,因为在 figure
环境中设置了 size
.花括号反正都是打印出来的.)
为什么 pandoc
会转义这对花括号,但保留所有其他花括号(例如在 egin{tabular}
中)未转义?这是因为 pandoc
应该转义字面意思的特殊字符,但留下 原始 LaTeX 未转义.然而,pandoc
不知道外面的花括号是 LaTeX 命令而不是文本.
(使用 floating = TRUE
不会出现问题,因为花括号位于被识别为 LaTeX 的 figure
环境中.)
解决方案
在了解了问题之后,我们能做些什么呢?OP 已经发布了一种解决方案:避免在 print 中指定
并手动插入 size
.xtablefootnotesize
命令:
---输出:pdf_文档:keep_tex: 是的---```{r,结果='asis',回声=假}库(xtable)mytable <- xtable(data.frame(matrix(c(1:6), nrow = 2)))cat("\begin{footnotesize}")打印(我的表,评论 = 假,浮动=假)cat("\end{footnotesize}")```
但是,从长远来看,如果 xtable
(当前版本:1.8-0
)生成的 LaTeX 代码能够在 pandoc
转换.这很简单:print.xtable
检查是否设置了 size
,如果设置了,则在尺寸规范和 }<之前插入
{
/code> 在表格末尾:
if (is.null(size) || !is.character(size)) {BSIZE <-""ESIZE <-""}别的 {if (length(grep("^\\", size)) == 0) {尺寸 <- 粘贴("\", 尺寸, sep = "")}BSIZE <- paste("{", size, "
", sep = "")ESIZE <- "}
"}
这个小修改将 {
替换为 egingroup
和 }
替换为 endgroup
:
if (is.null(size) || !is.character(size)) {BSIZE <-""ESIZE <-""}别的 {if (length(grep("^\\", size)) == 0) {尺寸 <- 粘贴("\", 尺寸, sep = "")}BSIZE <- paste("\begingroup", size, "
", sep = "")ESIZE <- "\endgroup
"}
对于 LaTeX,这没有什么区别,但是当 pandoc
识别 egingroup
(与 {
相对)它应该可以解决问题.我报告了这个 作为 xtable
中的一个错误,希望该问题将在未来的版本中得到修复.
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.
What happens?
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
: (generated fromTEX
bypdflatex
)
TEX
: (generated fromMD
bypandoc
)% … {footnotesize egin{tabular}{rrrr} hline & X1 & X2 & X3 \ hline 1 & 1 & 3 & 5 \ 2 & 2 & 4 & 6 \ hline end{tabular} } % …
MD
(generated fromRMD
byknitr
)--- output: pdf_document: keep_tex: yes --- {footnotesize egin{tabular}{rrrr} hline & X1 & X2 & X3 \ hline 1 & 1 & 3 & 5 \ 2 & 2 & 4 & 6 \ hline end{tabular} }
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" ) ```
Recall: The problem is, that there are visible curly braces in the PDF
. Where do they come from?
- 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.
So we know two things by now: We see the curly braces because they are escaped and they are escaped by pandoc
.
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.)
And why does pandoc
escape that pair of curly braces but leaves all the other curly braces (e.g. in egin{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.
(With floating = TRUE
the problem does not occur because the curly braces are within a figure
environment which is recognized as LaTeX.)
Solutions
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}")
```
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, "
", sep = "")
ESIZE <- "}
"
}
This small modification replaces {
with egingroup
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, "
", sep = "")
ESIZE <- "\endgroup
"
}
For LaTeX, this makes no difference, but as pandoc
recognizes egingroup
(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屋!