【问题标题】:Booktabs table does not show in pdf or docBooktabs 表未在 pdf 或 doc 中显示
【发布时间】:2021-05-17 07:47:57
【问题描述】:

尝试创建包含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

【问题讨论】:

  • LaTeX 中表格的另一种解决方案是:haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
  • 感谢您的建议。然而,我正在写一本关于如何用JASP 教授统计数据的书。 JASP 允许将表格复制为 LaTex(需要 booktabs 包),其格式如上所示。因此,使用上述格式对我​​来说耗时更少。

标签: r latex rstudio tex tinytex


【解决方案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: 下)仔细检查正在生成的内容。

【讨论】:

  • 非常感谢。我注意到它适用于 pdf 文件,但不适用于 doc 或 html。我是否需要在 yaml 标头中添加任何内容才能使其正常工作?提前致谢。
  • 这是我对 R Markdown 最大的问题:表格不会转换为所有格式。 knitr::kablekableExtra 适用于 PDF 和 HTML。但如果你想在 doc 和 docx 中有漂亮的表格,你需要 flextable 或类似的东西。
  • 我个人编写了一个函数来让我的表格正确。我使用knitr:::pandoc_to() == "latex" 来检查是否有东西被编织到PDF 和knitr:::pandoc_to() == "docx" 用于word。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 1970-01-01
  • 2012-03-23
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
相关资源
最近更新 更多