1

我正在尝试在 tex 文件中包含使用 gt() 函数生成的表。我创建了一个 .Rnw 文件,然后用 knitr 编织它并用 pdflatex 编译。在编译过程中,我收到一个错误:“这里没有要结束的行”,这是由 gt() 在表头中插入的换行符引起的。这是一个 MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{caption}

\begin{document}
<<setup, include=FALSE>>=
library(knitr)
library(tidyverse)
library(gt)
opts_chunk$set(echo=FALSE)
@

This is a dataframe formatted with the \texttt{gt} package.

<<>>=
tibble(
  group=rep(c("A", "B"), each=5),
  age = c(20, 24, 22, 27, 29, 21, 24, 23, 30, 31)) %>%
    gt %>%
      tab_header(title="Some title")
@

\end{document}

tex文件中的结果表是:

\begin{longtable}{lr}
\caption*{
\large Some title\\ 
\small \\ % This newline causes the error
} \\ 
\toprule
group & age \\ 
\midrule
A & 20 \\ 
A & 24 \\ 
A & 22 \\ 
A & 27 \\ 
A & 29 \\ 
B & 21 \\ 
B & 24 \\ 
B & 23 \\ 
B & 30 \\ 
B & 31 \\ 
\bottomrule
\end{longtable}

(我在编织后手动添加了注释)

由于标题中的换行符(\\),我得到:./mwe.tex:69: LaTeX Error: There's no line here to end。如果没有该换行符,PDF 将按预期创建。有没有办法解决这个问题而无需手动编辑 tex 文件?

4

1 回答 1

2

我发现这是没有定义字幕时发生的 gt 错误。解决方法是 1)添加一个空格作为字幕,或者

tab_header(title = md("Data listing from **gtcars**"), subtitle = md("&nbsp;"))

2)手动编辑删除\small \\logntable中的tex文件。

在https://github.com/rstudio/gt/issues/197检查问题

于 2020-05-07T16:01:50.377 回答