0

尝试创建包含booktabs表格的 pdf 时出现错误。如果我删除表格代码,则可以毫无问题地创建 pdf。

RStudio: 最新版本

R: 最新版本

tinytex: 最新版本

下面是我的Rmd文件:

---
title: "table"
author: "Author"
date: "2/13/2021"
output:
  pdf_document:
    latex_engine: xelatex
  html_document:
    df_print: paged
includes:
  in_header: preamble.tex
---

Something here

\begin{table}[h]
    \centering
    \caption{Descriptive Statistics}
    \label{tab:descriptiveStatistics}
    {
        \begin{tabular}{lr}
            \toprule
             & Weight  \\
            \cmidrule[0.4pt]{1-2}
            Valid & 13  \\
            Missing & 0  \\
            Mean & 170.769  \\
            Std. Deviation & 29.255  \\
            Minimum & 140.000  \\
            Maximum & 235.000  \\
            \bottomrule
        \end{tabular}
    }
\end{table}

下面是我的preamble.tex文件:

\usepackage{booktabs}
\usepackage{makecell}

我得到的错误如下:

output file: test.knit.md

! Undefined control sequence.
l.81             \toprule

Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info.
In addition: Warning message:
In has_crop_tools() : 
Tool(s) not installed or not in PATH: pdfcrop, ghostcript
-> As a result, figure cropping will be disabled.
Execution halted
4

1 回答 1

1

preamble.tex的不包括在内,因为您的 yaml 标头错误。应该:

---
title: "table"
author: "Author"
date: "2/13/2021"
output:
  pdf_document:
    latex_engine: xelatex
    includes:
      in_header: preamble.tex
  html_document:
    df_print: paged
---

然后它工作。

我也建议使用kableExtra. 根据我的经验,它基本上会生成 LaTeX 代码并为您节省大量制作表格的时间。您可以设置keep_tex: yes(也在 下pdf_document:)以仔细检查正在生成的内容。

于 2021-02-14T08:44:12.437 回答